1 /* { dg-skip-if "requires io" { freestanding } { "*" } { "" } }  */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 
7 volatile int should_optimize;
8 
9 int
10 __attribute__((noinline))
__fprintf_chk(FILE * f,int flag,const char * fmt,...)11 __fprintf_chk (FILE *f, int flag, const char *fmt, ...)
12 {
13   va_list ap;
14   int ret;
15 #ifdef __OPTIMIZE__
16   if (should_optimize)
17     abort ();
18 #endif
19   should_optimize = 1;
20   va_start (ap, fmt);
21   ret = vfprintf (f, fmt, ap);
22   va_end (ap);
23   return ret;
24 }
25 
26 int
main(void)27 main (void)
28 {
29 #define test(ret, opt, args...) \
30   should_optimize = opt;			\
31   __fprintf_chk (stdout, 1, args); 		\
32   if (!should_optimize)				\
33     abort ();					\
34   should_optimize = 0;				\
35   if (__fprintf_chk (stdout, 1, args) != ret)	\
36     abort ();					\
37   if (!should_optimize)				\
38     abort ();
39   test (5, 1, "hello");
40   test (6, 1, "hello\n");
41   test (1, 1, "a");
42   test (0, 1, "");
43   test (5, 1, "%s", "hello");
44   test (6, 1, "%s", "hello\n");
45   test (1, 1, "%s", "a");
46   test (0, 1, "%s", "");
47   test (1, 1, "%c", 'x');
48   test (7, 0, "%s\n", "hello\n");
49   test (2, 0, "%d\n", 0);
50   return 0;
51 }
52