modernc.org/ccgo/v3@v3.16.14/lib/testdata/gcc-9.1.0/gcc/testsuite/gcc.c-torture/execute/printf-2.c (about)

     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-skip-if "requires io" { freestanding } } */
     6  
     7  #include <stdio.h>
     8  #include <stdlib.h>
     9  #include <string.h>
    10  
    11  __attribute__ ((noipa)) void
    12  write_file (void)
    13  {
    14    printf ("1");
    15    printf ("%c", '2');
    16    printf ("%c%c", '3', '4');
    17    printf ("%s", "5");
    18    printf ("%s%s", "6", "7");
    19    printf ("%i", 8);
    20    printf ("%.1s\n", "9x");
    21  }
    22  
    23  
    24  int main (void)
    25  {
    26    char *tmpfname = tmpnam (0);
    27    FILE *f = freopen (tmpfname, "w", stdout);
    28    if (!f)
    29      {
    30        perror ("fopen for writing");
    31        return 1;
    32      }
    33  
    34    write_file ();
    35    fclose (f);
    36  
    37    f = fopen (tmpfname, "r");
    38    if (!f)
    39      {
    40        perror ("fopen for reading");
    41        remove (tmpfname);
    42        return 1;
    43      }
    44  
    45    char buf[12] = "";
    46    if (1 != fscanf (f, "%s", buf))
    47      {
    48        perror ("fscanf");
    49        fclose (f);
    50        remove (tmpfname);
    51        return 1;
    52      }
    53  
    54    fclose (f);
    55    remove (tmpfname);
    56  
    57    if (strcmp (buf, "123456789"))
    58      abort ();
    59  
    60    return 0;
    61  }