</>EmbeddedInterview

Implement memmove() with overlap handling.

Easy"Coding: Memory & String Functions"
#include <stddef.h>
#include <stdint.h>

void *my_memmove(void *dst, const void *src, size_t n)
{
    uint8_t *d = dst;
    const uint8_t *s = src;
    if (d == s || n == 0) return dst;
    if ((uintptr_t)d < (uintptr_t)s || (uintptr_t)d >= (uintptr_t)s + n) {
        while (n--) *d++ = *s++;          /* no harmful overlap: copy forward */
    } else {
        d += n; s += n;                   /* dst overlaps the tail of src: copy backward */
        while (n--) *--d = *--s;
    }
    return dst;
}

Points: copy backwards when the destination starts inside the source range, so source bytes are read before they are overwritten. Comparing via uintptr_t avoids the technically unspecified comparison of unrelated pointers.