(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.c-torture/
execute/
printf-2.c
       1  /* Verify that calls to printf don't get eliminated even if their
       2     result on success can be computed at compile time (they can fail).
       3     The calls can still be transformed into those of other functions.
       4     { dg-require-effective-target unwrapped }
       5     { dg-require-effective-target fileio }
       6     { dg-prune-output "warning: warning: \[^\n\r\]* possibly used unsafely" }
       7     { dg-skip-if "requires io" { avr-*-* } }
       8     { dg-skip-if "requires io" { freestanding } } */
       9  
      10  #include <stdio.h>
      11  #include <stdlib.h>
      12  #include <string.h>
      13  #include "gcc_tmpnam.h"
      14  
      15  __attribute__ ((noipa)) void
      16  write_file (void)
      17  {
      18    printf ("1");
      19    printf ("%c", '2');
      20    printf ("%c%c", '3', '4');
      21    printf ("%s", "5");
      22    printf ("%s%s", "6", "7");
      23    printf ("%i", 8);
      24    printf ("%.1s\n", "9x");
      25  }
      26  
      27  
      28  int main (void)
      29  {
      30    char *tmpfname = gcc_tmpnam (0);
      31    FILE *f = freopen (tmpfname, "w", stdout);
      32    if (!f)
      33      {
      34        perror ("fopen for writing");
      35        return 1;
      36      }
      37  
      38    write_file ();
      39    fclose (f);
      40  
      41    f = fopen (tmpfname, "r");
      42    if (!f)
      43      {
      44        perror ("fopen for reading");
      45        remove (tmpfname);
      46        return 1;
      47      }
      48  
      49    char buf[12] = "";
      50    if (1 != fscanf (f, "%s", buf))
      51      {
      52        perror ("fscanf");
      53        fclose (f);
      54        remove (tmpfname);
      55        return 1;
      56      }
      57  
      58    fclose (f);
      59    remove (tmpfname);
      60  
      61    if (strcmp (buf, "123456789"))
      62      abort ();
      63  
      64    return 0;
      65  }