(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-15.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2 -fdump-tree-strlen" } */
       3  
       4  #include "strlenopt.h"
       5  
       6  __attribute__((noinline, noclone)) size_t
       7  fn1 (char *p, size_t l)
       8  {
       9    memcpy (p, "abcdef", l);
      10    /* This strlen can't be optimized, as l is unknown.  */
      11    return strlen (p);
      12  }
      13  
      14  __attribute__((noinline, noclone)) size_t
      15  fn2 (char *p, const char *q, size_t *lp)
      16  {
      17    size_t l = strlen (q), l2;
      18    memcpy (p, q, 7);
      19    /* This strlen can't be optimized, as l might be bigger than 7.  */
      20    l2 = strlen (p);
      21    *lp = l;
      22    return l2;
      23  }
      24  
      25  __attribute__((noinline, noclone)) char *
      26  fn3 (char *p)
      27  {
      28    *p = 0;
      29    return p + 1;
      30  }
      31  
      32  int
      33  main ()
      34  {
      35    char buf[64];
      36    const char *volatile q = "ABCDEFGH";
      37    const char *volatile q2 = "IJ\0KLMNOPQRS";
      38    size_t l;
      39    memset (buf, '\0', sizeof buf);
      40    memset (buf + 2, 'a', 7);
      41    if (fn1 (buf, 3) != 9 || memcmp (buf, "abcaaaaaa", 10) != 0)
      42      abort ();
      43    if (fn1 (buf, 7) != 6 || memcmp (buf, "abcdef\0aa", 10) != 0)
      44      abort ();
      45    if (fn2 (buf, q, &l) != 9 || l != 8 || memcmp (buf, "ABCDEFGaa", 10) != 0)
      46      abort ();
      47    if (fn2 (buf, q2, &l) != 2 || l != 2 || memcmp (buf, "IJ\0KLMNaa", 10) != 0)
      48      abort ();
      49    if (fn3 (buf) != buf + 1 || memcmp (buf, "\0J\0KLMNaa", 10) != 0)
      50      abort ();
      51    return 0;
      52  }
      53  
      54  /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen1" } } */
      55  /* { dg-final { scan-tree-dump-times "memcpy \\(" 2 "strlen1" } } */
      56  /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen1" } } */
      57  /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen1" } } */
      58  /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
      59  /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */