1  /* PR tree-optimization/83821 - local aggregate initialization defeats
       2     strlen optimization
       3     Verify that a memset() call to zero out a subregion of memory
       4     allocated by calloc() is eliminated even if a zero byte is written
       5     into it in between the two calls.  See the calloc-2.c test that
       6     verifies that the memset() calls isn't eliminated if the written
       7     value is non-zero.
       8     { dg-do compile }
       9     { dg-options "-O2 -fdump-tree-optimized" } */
      10  
      11  void* elim_calloc_store_memset_1 (unsigned a, unsigned b)
      12  {
      13    char *p = __builtin_calloc (a, 1);
      14  
      15    p[1] = '\0';
      16  
      17    __builtin_memset (p, 0, b);   // should be eliminated
      18  
      19    return p;
      20  }
      21  
      22  void* elim_calloc_store_memset_2 (unsigned a, unsigned b, unsigned c)
      23  {
      24    char *p = __builtin_calloc (a, 1);
      25  
      26    p[1] = '\0';
      27    __builtin_memset (p, 0, b);   // should be eliminated
      28  
      29    p[3] = '\0';
      30    __builtin_memset (p, 0, c);   // should also be eliminated
      31  
      32    return p;
      33  }
      34  
      35  /* { dg-final { scan-tree-dump-not "malloc" "optimized" } }
      36     { dg-final { scan-tree-dump-times "_calloc \\\(" 2 "optimized" } }
      37     { dg-final { scan-tree-dump-not "_memset \\\(" "optimized" } } */