1 /* PR tree-optimization/83821 - local aggregate initialization defeats
2    strlen optimization
3    Verify that a strlen() call is eliminated for a pointer to a region
4    of memory allocated by calloc() even if one or more nul bytes are
5    written into it.
6    { dg-do compile }
7    { dg-options "-O2 -fdump-tree-optimized" } */
8 
9 unsigned n0, n1;
10 
elim_strlen_calloc_store_memset_1(unsigned a,unsigned b)11 void* elim_strlen_calloc_store_memset_1 (unsigned a, unsigned b)
12 {
13   char *p = __builtin_calloc (a, 1);
14 
15   p[0] = '\0';
16   p[1] = '\0';
17   p[2] = '\0';
18   p[3] = '\0';
19 
20   __builtin_memset (p, 0, b);
21 
22   n0 = __builtin_strlen (p);
23 
24   return p;
25 }
26 
elim_strlen_calloc_store_memset_2(unsigned a,unsigned b,unsigned c)27 void* elim_strlen_calloc_store_memset_2 (unsigned a, unsigned b, unsigned c)
28 {
29   char *p = __builtin_calloc (a, 1);
30 
31   p[1] = '\0';
32   __builtin_memset (p, 0, b);
33 
34   n0 = __builtin_strlen (p);
35 
36   p[3] = 0;
37   __builtin_memset (p, 0, c);
38 
39   n1 = __builtin_strlen (p);
40 
41   return p;
42 }
43 
44 /* { dg-final { scan-tree-dump-not "__builtin_strlen" "optimized" } } */
45