1  /* { dg-skip-if "requires io" { freestanding } }  */
       2  
       3  #ifndef test
       4  #include <stdio.h>
       5  #include <stdlib.h>
       6  #include <stdarg.h>
       7  
       8  volatile int should_optimize;
       9  
      10  int
      11  __attribute__((noinline))
      12  __vfprintf_chk (FILE *f, int flag, const char *fmt, va_list ap)
      13  {
      14  #ifdef __OPTIMIZE__
      15    if (should_optimize)
      16      abort ();
      17  #endif
      18    should_optimize = 1;
      19    return vfprintf (f, fmt, ap);
      20  }
      21  
      22  void
      23  inner (int x, ...)
      24  {
      25    va_list ap, ap2;
      26    va_start (ap, x);
      27    va_start (ap2, x);
      28  
      29    switch (x)
      30      {
      31  #define test(n, ret, opt, fmt, args) \
      32      case n:						\
      33        should_optimize = opt;				\
      34        __vfprintf_chk (stdout, 1, fmt, ap);		\
      35        if (! should_optimize)				\
      36  	abort ();					\
      37        should_optimize = 0;				\
      38        if (__vfprintf_chk (stdout, 1, fmt, ap2) != ret)	\
      39  	abort ();					\
      40        if (! should_optimize)				\
      41  	abort ();					\
      42        break;
      43  #include "vfprintf-chk-1.c"
      44  #undef test
      45      default:
      46        abort ();
      47      }
      48  
      49    va_end (ap);
      50    va_end (ap2);
      51  }
      52  
      53  int
      54  main (void)
      55  {
      56  #define test(n, ret, opt, fmt, args) \
      57    inner args;
      58  #include "vfprintf-chk-1.c"
      59  #undef test
      60    return 0;
      61  }
      62  
      63  #else
      64    test (0, 5, 1, "hello", (0));
      65    test (1, 6, 1, "hello\n", (1));
      66    test (2, 1, 1, "a", (2));
      67    test (3, 0, 1, "", (3));
      68    test (4, 5, 0, "%s", (4, "hello"));
      69    test (5, 6, 0, "%s", (5, "hello\n"));
      70    test (6, 1, 0, "%s", (6, "a"));
      71    test (7, 0, 0, "%s", (7, ""));
      72    test (8, 1, 0, "%c", (8, 'x'));
      73    test (9, 7, 0, "%s\n", (9, "hello\n"));
      74    test (10, 2, 0, "%d\n", (10, 0));
      75  #endif