(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-12.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2" } */
       3  
       4  #include "strlenopt.h"
       5  
       6  __attribute__((noinline, noclone)) char *
       7  fn1 (char *p, size_t *l)
       8  {
       9    char *q = strcat (p, "abcde");
      10    *l = strlen (p);
      11    return q;
      12  }
      13  
      14  __attribute__((noinline, noclone)) char *
      15  fn2 (char *p, const char *q, size_t *l1, size_t *l2)
      16  {
      17    size_t l = strlen (q);
      18    char *r = strcat (p, q);
      19    *l1 = l;
      20    *l2 = strlen (p);
      21    return r;
      22  }
      23  
      24  __attribute__((noinline, noclone)) char *
      25  fn3 (char *p, const char *q, size_t *l)
      26  {
      27    char *r = strcpy (p, q);
      28    *l = strlen (p);
      29    return r;
      30  }
      31  
      32  __attribute__((noinline, noclone)) char *
      33  fn4 (char *p, const char *q, size_t *l)
      34  {
      35    char *r = strcat (p, q);
      36    *l = strlen (p);
      37    return r;
      38  }
      39  
      40  __attribute__((noinline, noclone)) char *
      41  fn5 (char *p, const char *q, size_t *l1, size_t *l2, size_t *l3)
      42  {
      43    size_t l = strlen (q);
      44    size_t ll = strlen (p);
      45    char *r = strcat (p, q);
      46    *l1 = l;
      47    *l2 = strlen (p);
      48    *l3 = ll;
      49    return r;
      50  }
      51  
      52  __attribute__((noinline, noclone)) char *
      53  fn6 (char *p, const char *q, size_t *l1, size_t *l2)
      54  {
      55    size_t l = strlen (p);
      56    char *r = strcat (p, q);
      57    *l1 = strlen (p);
      58    *l2 = l;
      59    return r;
      60  }
      61  
      62  int
      63  main ()
      64  {
      65    char buf[64];
      66    const char *volatile q = "fgh";
      67    size_t l, l1, l2, l3;
      68    memset (buf, '\0', sizeof buf);
      69    memset (buf, 'a', 3);
      70    if (fn1 (buf, &l) != buf || l != 8 || memcmp (buf, "aaaabcde", 9) != 0)
      71      abort ();
      72    if (fn2 (buf, q, &l1, &l2) != buf || l1 != 3 || l2 != 11
      73        || memcmp (buf, "aaaabcdefgh", 12) != 0)
      74      abort ();
      75    if (fn3 (buf, q, &l) != buf || l != 3
      76        || memcmp (buf, "fgh\0bcdefgh", 12) != 0)
      77      abort ();
      78    if (fn4 (buf, q, &l) != buf || l != 6
      79        || memcmp (buf, "fghfgh\0efgh", 12) != 0)
      80      abort ();
      81    l1 = 0;
      82    l2 = 0;
      83    if (fn5 (buf, q, &l1, &l2, &l3) != buf || l1 != 3 || l2 != 9 || l3 != 6
      84        || memcmp (buf, "fghfghfgh\0h", 12) != 0)
      85      abort ();
      86    if (fn6 (buf, q, &l1, &l2) != buf || l1 != 12 || l2 != 9
      87        || memcmp (buf, "fghfghfghfgh", 13) != 0)
      88      abort ();
      89    return 0;
      90  }