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