Implement memcpy with misaligned addresses
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Implement a C-style `memcpy` that correctly handles misaligned source/destination addresses.
## Task
Write a function:
```c
void* my_memcpy(void* dst, const void* src, size_t n);
```
It must copy exactly `n` bytes from `src` to `dst` and return `dst`.
## Requirements
- Assume `src` and `dst` memory ranges **do not overlap** (i.e., `memcpy` semantics, not `memmove`).
- `src` and/or `dst` may be **arbitrarily aligned** (not necessarily word-aligned).
- Your implementation should be safe and correct for all `n >= 0`.
- Optimize the copy by using wider loads/stores (e.g., `sizeof(size_t)` chunks) when possible, but still handle leading/trailing misaligned bytes correctly.
## Constraints / Notes
- You may not call the standard library `memcpy`.
- You may assume a typical flat address space and that unaligned access may be slower (or potentially illegal on some platforms), so alignment handling matters.
Quick Answer: This question evaluates a candidate's understanding of low-level memory manipulation, pointer alignment, and the implementation of efficient byte-copy routines in C.