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

     1  /* Verify that calls to a function declared wiith attribute format (printf)
     2     don't get eliminated even if their result on success can be computed at
     3     compile time (they can fail).
     4     { dg-require-effective-target unwrapped }
     5     { dg-skip-if "requires io" { freestanding } } */
     6  
     7  #include <stdarg.h>
     8  #include <stdio.h>
     9  #include <stdlib.h>
    10  #include <string.h>
    11  
    12  void __attribute__ ((format (printf, 1, 2), noipa))
    13  user_print (const char *fmt, ...)
    14  {
    15    va_list va;
    16    va_start (va, fmt);
    17    vfprintf (stdout, fmt, va);
    18    va_end (va);
    19  }
    20  
    21  int main (void)
    22  {
    23    char *tmpfname = tmpnam (0);
    24    FILE *f = freopen (tmpfname, "w", stdout);
    25    if (!f)
    26      {
    27        perror ("fopen for writing");
    28        return 1;
    29      }
    30  
    31    user_print ("1");
    32    user_print ("%c", '2');
    33    user_print ("%c%c", '3', '4');
    34    user_print ("%s", "5");
    35    user_print ("%s%s", "6", "7");
    36    user_print ("%i", 8);
    37    user_print ("%.1s\n", "9x");
    38  
    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  }