1  /* Copyright (C) 2003  Free Software Foundation.
       2  
       3     Ensure builtin mempcpy performs correctly.
       4  
       5     Written by Kaveh Ghazi, 4/11/2003.  */
       6  
       7  extern void abort (void);
       8  typedef __SIZE_TYPE__ size_t;
       9  extern size_t strlen(const char *);
      10  extern void *memcpy (void *, const void *, size_t);
      11  extern void *mempcpy (void *, const void *, size_t);
      12  extern int memcmp (const void *, const void *, size_t);
      13  extern int inside_main;
      14  
      15  const char s1[] = "123";
      16  char p[32] = "";
      17  char *s2 = "defg";
      18  char *s3 = "FGH";
      19  size_t l1 = 1;
      20  
      21  void
      22  main_test (void)
      23  {
      24    int i;
      25  
      26  #if !defined __i386__ && !defined __x86_64__
      27    /* The functions below might not be optimized into direct stores on all
      28       arches.  It depends on how many instructions would be generated and
      29       what limits the architecture chooses in STORE_BY_PIECES_P.  */
      30    inside_main = 0;
      31  #endif
      32  
      33    if (mempcpy (p, "ABCDE", 6) != p + 6 || memcmp (p, "ABCDE", 6))
      34      abort ();
      35    if (mempcpy (p + 16, "VWX" + 1, 2) != p + 16 + 2
      36        || memcmp (p + 16, "WX\0\0", 5))
      37      abort ();
      38    if (mempcpy (p + 1, "", 1) != p + 1 + 1 || memcmp (p, "A\0CDE", 6))
      39      abort ();
      40    if (mempcpy (p + 3, "FGHI", 4) != p + 3 + 4 || memcmp (p, "A\0CFGHI", 8))
      41      abort ();
      42  
      43    i = 8;
      44    memcpy (p + 20, "qrstu", 6);
      45    memcpy (p + 25, "QRSTU", 6);
      46    if (mempcpy (p + 25 + 1, s1, 3) != (p + 25 + 1 + 3)
      47        || memcmp (p + 25, "Q123U", 6))
      48      abort ();
      49  
      50    if (mempcpy (mempcpy (p, "abcdEFG", 4), "efg", 4) != p + 8
      51        || memcmp (p, "abcdefg", 8))
      52      abort();
      53  
      54    /* Test at least one instance of the __builtin_ style.  We do this
      55       to ensure that it works and that the prototype is correct.  */
      56    if (__builtin_mempcpy (p, "ABCDE", 6) != p + 6 || memcmp (p, "ABCDE", 6))
      57      abort ();
      58  
      59    /* If the result of mempcpy is ignored, gcc should use memcpy.
      60       This should be optimized always, so set inside_main again.  */
      61    inside_main = 1;
      62    mempcpy (p + 5, s3, 1);
      63    if (memcmp (p, "ABCDEFg", 8))
      64      abort ();
      65    mempcpy (p + 6, s1 + 1, l1);
      66    if (memcmp (p, "ABCDEF2", 8))
      67      abort ();
      68  }