#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ small;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ fwdprintf(char *, size_t *, int, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int strcmp(char *, char *);
int test_fwdprintf(void) {
  char buf[512];
  char small[5];
  size_t off;
  off = 0;
  memset(buf, 0, sizeof(buf));
  fwdprintf(buf, &off, sizeof(buf), "foo");
  fwdprintf(buf, &off, sizeof(buf), "bar");
  fwdprintf(buf, &off, sizeof(buf), "%d", 5);
  do {
    int __my_ret__ = strcmp(buf, "foobar5");
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 179,
              "strcmp(buf, \"foobar5\")");
      return __my_ret__;
    }
  } while (0);
  ;
  off = 0;
  memset(small, 0, sizeof(small));
  fwdprintf(small, &off, sizeof(small), "abc");
  fwdprintf(small, &off, sizeof(small), "123");
  do {
    int __my_ret__ = strcmp(small, "abc1");
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 185,
              "strcmp(small, \"abc1\")");
      return __my_ret__;
    }
  } while (0);
  ;
  return 0;
}


x86 assembly (clang, -O0):
<test_fwdprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movq   $0x0,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1030 <memset@plt>
lea    -0x210(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
lea    0xe7d(%rip),%rcx
mov    $0x0,%al
call   1060 <fwdprintf@plt>
lea    -0x210(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
lea    0xe60(%rip),%rcx
mov    $0x0,%al
call   1060 <fwdprintf@plt>
lea    -0x210(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
lea    0xe43(%rip),%rcx
mov    $0x5,%r8d
mov    $0x0,%al
call   1060 <fwdprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1040 <strcmp@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
je     1227 <test_fwdprintf+0xe7>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0xb3,%edx
lea    0xe16(%rip),%rcx
call   1050 <fprintf@plt>
mov    -0x224(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ed <test_fwdprintf+0x1ad>
jmp    122c <test_fwdprintf+0xec>
movq   $0x0,-0x220(%rbp)
lea    -0x215(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1030 <memset@plt>
lea    -0x215(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x5,%edx
lea    0xddd(%rip),%rcx
mov    $0x0,%al
call   1060 <fwdprintf@plt>
lea    -0x215(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x5,%edx
lea    0xdc0(%rip),%rcx
mov    $0x0,%al
call   1060 <fwdprintf@plt>
lea    -0x215(%rbp),%rdi
lea    0xdaf(%rip),%rsi
call   1040 <strcmp@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
je     12e1 <test_fwdprintf+0x1a1>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
mov    $0xb9,%edx
lea    0xd80(%rip),%rcx
call   1050 <fprintf@plt>
mov    -0x228(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ed <test_fwdprintf+0x1ad>
jmp    12e6 <test_fwdprintf+0x1a6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fwdprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
movq   $0x0,(%rsp)
lea    0x10(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1030 <memset@plt>
lea    0xe8f(%rip),%rcx
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %eax,%eax
call   1060 <fwdprintf@plt>
lea    0xe77(%rip),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %eax,%eax
call   1060 <fwdprintf@plt>
lea    0xe62(%rip),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x200,%edx
mov    $0x5,%r8d
xor    %eax,%eax
call   1060 <fwdprintf@plt>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11f4 <test_fwdprintf+0xb4>
mov    %eax,%ebx
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
lea    0xe40(%rip),%rcx
mov    $0xb3,%edx
jmp    1276 <test_fwdprintf+0x136>
movq   $0x0,(%rsp)
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1030 <memset@plt>
lea    0xe2a(%rip),%rcx
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x5,%edx
xor    %eax,%eax
call   1060 <fwdprintf@plt>
lea    0xe12(%rip),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x5,%edx
xor    %eax,%eax
call   1060 <fwdprintf@plt>
lea    0xdfd(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     127e <test_fwdprintf+0x13e>
mov    %eax,%ebx
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
lea    0xddd(%rip),%rcx
mov    $0xb9,%edx
call   1050 <fprintf@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [4 x i8] c"bar\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [8 x i8] c"foobar5\00"
@stderr = global i32 0
@.str.4 = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.5 = [23 x i8] c"strcmp(buf, \22foobar5\22)\00"
@.str.6 = [4 x i8] c"abc\00"
@.str.7 = [4 x i8] c"123\00"
@.str.8 = [5 x i8] c"abc1\00"
@.str.9 = [22 x i8] c"strcmp(small, \22abc1\22)\00"
define i32 @test_fwdprintf() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca [5 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i64 0, i64* %4
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 512)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%10 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %9, i64* %4, i32 512, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%12 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %11, i64* %4, i32 512, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%14 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %13, i64* %4, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 5)
br label %15
15: ; preds = %0
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 179, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%23 = load i32, i32* %5
store i32 %23, i32* %1
br label %43
24: ; preds = %15
br label %25
25: ; preds = %24
store i64 0, i64* %4
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 5)
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%29 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %28, i64* %4, i32 5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%31 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %30, i64* %4, i32 5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
br label %32
32: ; preds = %25
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 185, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
%40 = load i32, i32* %6
store i32 %40, i32* %1
br label %43
41: ; preds = %32
br label %42
42: ; preds = %41
store i32 0, i32* %1
br label %43
43: ; preds = %42, %37, %20
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [4 x i8] c"bar\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [8 x i8] c"foobar5\00"
@stderr = global i32 0
@.str.4 = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.5 = [23 x i8] c"strcmp(buf, \22foobar5\22)\00"
@.str.6 = [4 x i8] c"abc\00"
@.str.7 = [4 x i8] c"123\00"
@.str.8 = [5 x i8] c"abc1\00"
@.str.9 = [22 x i8] c"strcmp(small, \22abc1\22)\00"
define i32 @test_fwdprintf() {
%1 = alloca [512 x i8]
%2 = alloca [5 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = call i32 @memset(i8* %4, i32 0, i32 512)
%8 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %4, i64* %3, i32 512, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %4, i64* %3, i32 512, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %4, i64* %3, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 5)
%11 = call i32 @strcmp(i8* %4, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 179, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %25
16: ; preds = %0
store i64 0, i64* %3
%17 = call i32 @memset(i8* %5, i32 0, i32 5)
%18 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %5, i64* %3, i32 5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%19 = call i32 (i8*, i64*, i32, i8*, ...) @fwdprintf(i8* %5, i64* %3, i32 5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%20 = call i32 @strcmp(i8* %5, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 185, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
br label %25
25: ; preds = %22, %16, %13
%26 = phi i32 [ %11, %13 ], [ 0, %16 ], [ %20, %22 ]
ret i32 %26
}