Implement your own memcpy().
#include <stddef.h>
#include <stdint.h>
void *my_memcpy(void *restrict dst, const void *restrict src, size_t n)
{
uint8_t *d = dst;
const uint8_t *s = src;
/* fast path: copy words when both pointers share the same alignment */
if ((((uintptr_t)d ^ (uintptr_t)s) & (sizeof(uint32_t) - 1)) == 0) {
while (n && ((uintptr_t)d & (sizeof(uint32_t) - 1))) { *d++ = *s++; n--; }
uint32_t *dw = (uint32_t *)(void *)d;
const uint32_t *sw = (const uint32_t *)(const void *)s;
while (n >= 4) { *dw++ = *sw++; n -= 4; }
d = (uint8_t *)dw;
s = (const uint8_t *)sw;
}
while (n--) *d++ = *s++; /* remaining bytes */
return dst;
}
Points: behaviour is undefined for overlapping regions (that is memmove's job); restrict lets the compiler optimise; copy words only when both addresses are aligned (otherwise Cortex-M0 faults); return dst. When writing a real libc, build it with -fno-builtin (or -ffreestanding) so the compiler does not turn your loop back into a call to memcpy itself. Do not use it on device registers (access width and volatile).