1570af302Sopenharmony_ci#include <string.h>
2570af302Sopenharmony_ci#include <stdint.h>
3570af302Sopenharmony_ci#include <limits.h>
4570af302Sopenharmony_ci
5570af302Sopenharmony_ci#define ALIGN (sizeof(size_t)-1)
6570af302Sopenharmony_ci#define ONES ((size_t)-1/UCHAR_MAX)
7570af302Sopenharmony_ci#define HIGHS (ONES * (UCHAR_MAX/2+1))
8570af302Sopenharmony_ci#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9570af302Sopenharmony_ci
10570af302Sopenharmony_civoid *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
11570af302Sopenharmony_ci{
12570af302Sopenharmony_ci	unsigned char *d = dest;
13570af302Sopenharmony_ci	const unsigned char *s = src;
14570af302Sopenharmony_ci
15570af302Sopenharmony_ci	c = (unsigned char)c;
16570af302Sopenharmony_ci#ifdef __GNUC__
17570af302Sopenharmony_ci	typedef size_t __attribute__((__may_alias__)) word;
18570af302Sopenharmony_ci	word *wd;
19570af302Sopenharmony_ci	const word *ws;
20570af302Sopenharmony_ci	if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
21570af302Sopenharmony_ci		for (; ((uintptr_t)s & ALIGN) && n && (*d=*s)!=c; n--, s++, d++);
22570af302Sopenharmony_ci		if ((uintptr_t)s & ALIGN) goto tail;
23570af302Sopenharmony_ci		size_t k = ONES * c;
24570af302Sopenharmony_ci		wd=(void *)d; ws=(const void *)s;
25570af302Sopenharmony_ci		for (; n>=sizeof(size_t) && !HASZERO(*ws^k);
26570af302Sopenharmony_ci		       n-=sizeof(size_t), ws++, wd++) *wd = *ws;
27570af302Sopenharmony_ci		d=(void *)wd; s=(const void *)ws;
28570af302Sopenharmony_ci	}
29570af302Sopenharmony_ci#endif
30570af302Sopenharmony_ci	for (; n && (*d=*s)!=c; n--, s++, d++);
31570af302Sopenharmony_citail:
32570af302Sopenharmony_ci	if (n) return d+1;
33570af302Sopenharmony_ci	return 0;
34570af302Sopenharmony_ci}
35