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