(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-2.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2 -fdump-tree-strlen" } */
       3  
       4  #include "strlenopt.h"
       5  
       6  __attribute__((noinline, noclone)) char *
       7  foo (char *p, char *r)
       8  {
       9    char buf[26];
      10    if (strlen (p) + strlen (r) + 9 > 26)
      11      return NULL;
      12    /* This strcpy can be optimized into memcpy, using the remembered
      13       strlen (p).  */
      14    strcpy (buf, p);
      15    /* These two strcat can be optimized into memcpy.  The first one
      16       could be even optimized into a *ptr = '/'; store as the '\0'
      17       is immediately overwritten.  */
      18    strcat (buf, "/");
      19    strcat (buf, "abcde");
      20    /* This strcpy can be optimized into memcpy, using the remembered
      21       strlen (r).  */
      22    strcat (buf, r);
      23    /* And this can be optimized into memcpy too.  */
      24    strcat (buf, "fg");
      25    return strdup (buf);
      26  }
      27  
      28  int
      29  main ()
      30  {
      31    char *volatile p = "string1";
      32    char *volatile r = "string2";
      33    char *q = foo (p, r);
      34    if (q != NULL)
      35      {
      36        if (strcmp (q, "string1/abcdestring2fg"))
      37  	abort ();
      38        free (q);
      39      }
      40    return 0;
      41  }
      42  
      43  /* { dg-final { scan-tree-dump-times "strlen \\(" 2 "strlen1" } } */
      44  /* { dg-final { scan-tree-dump-times "memcpy \\(" 5 "strlen1" } } */
      45  /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen1" } } */
      46  /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen1" } } */
      47  /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
      48  /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */