/* #include <string.h> */
#include <stdio.h>
#include "apex_memmove.h"

// To allow usage of LD fct wrapping so we don't need to rename each single memcpy/memmove on the code
// Note: we force the used attribute due gcc discarding runtime functions (think those are not supposed to be LTOed)
// @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58203
void __attribute__ ((used))   *__real_memcpy(void *dst, const void *src, size_t size)
{
  /* printf("%s\n", __PRETTY_FUNCTION__); */
  if (size)
    do { --size; ((char *)dst)[size] = ((char *)src)[size]; } while (size);
  return dst;
}
void __attribute__ ((used))   *__real_memmove(void *dst, const void *src, size_t size)
{
  size_t i = 0;

  /* printf("%s\n", __PRETTY_FUNCTION__); */
  if (size)
  {
    if (src < dst)
      do { --size; ((char *)dst)[size] = ((char *)src)[size]; } while (size);
    else
      do { ((char *)dst)[i] = ((char *)src)[i]; } while (++i < size);
  }
  return dst;
}
