#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*/ va_list;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOVERFLOW;
size_t INT_MAX;
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseterr(int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
char *vasnprintf(char *, size_t *, char const *, int /*<<< orphan*/);
int vfprintf(FILE *fp, const char *format, va_list args) {
  char buf[2000];
  char *output;
  size_t len;
  size_t lenbuf = sizeof(buf);
  output = vasnprintf(buf, &lenbuf, format, args);
  len = lenbuf;
  if (!output) {
    fseterr(fp);
    return -1;
  }
  if (fwrite(output, 1, len, fp) < len) {
    if (output != buf) {
      int saved_errno = errno;
      free(output);
      errno = saved_errno;
    }
    return -1;
  }
  if (output != buf)
    free(output);
  if (len > INT_MAX) {
    errno = EOVERFLOW;
    fseterr(fp);
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<vfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x7d0,-0x808(%rbp)
lea    -0x7f0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x808(%rbp),%rsi
call   1050 <vasnprintf@plt>
mov    %rax,-0x7f8(%rbp)
mov    -0x808(%rbp),%rax
mov    %rax,-0x800(%rbp)
cmpq   $0x0,-0x7f8(%rbp)
jne    11b3 <vfprintf+0x73>
mov    -0x10(%rbp),%rdi
call   1030 <fseterr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <vfprintf+0x154>
mov    -0x7f8(%rbp),%rdi
mov    -0x800(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    -0x800(%rbp),%rax
jae    1229 <vfprintf+0xe9>
mov    -0x7f8(%rbp),%rax
lea    -0x7f0(%rbp),%rcx
cmp    %rcx,%rax
je     121d <vfprintf+0xdd>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80c(%rbp)
mov    -0x7f8(%rbp),%rdi
call   1040 <free@plt>
mov    -0x80c(%rbp),%ecx
mov    0x2db5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <vfprintf+0x154>
mov    -0x7f8(%rbp),%rax
lea    -0x7f0(%rbp),%rcx
cmp    %rcx,%rax
je     124c <vfprintf+0x10c>
mov    -0x7f8(%rbp),%rdi
call   1040 <free@plt>
mov    -0x800(%rbp),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jbe    128a <vfprintf+0x14a>
mov    0x2d86(%rip),%rax
mov    (%rax),%ecx
mov    0x2d5d(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <fseterr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <vfprintf+0x154>
mov    -0x800(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vfprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x7e0,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%r14
movq   $0x7d0,0x8(%rsp)
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1050 <vasnprintf@plt>
test   %rax,%rax
je     11e4 <vfprintf+0xa4>
mov    %rax,%rbx
mov    0x8(%rsp),%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <fwrite@plt>
cmp    %r15,%rax
jae    11b9 <vfprintf+0x79>
mov    $0xffffffff,%r15d
cmp    %r12,%rbx
je     11f2 <vfprintf+0xb2>
mov    0x2e27(%rip),%r14
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,(%r14)
jmp    11f2 <vfprintf+0xb2>
cmp    %r12,%rbx
je     11c6 <vfprintf+0x86>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    0x2e13(%rip),%rax
cmp    (%rax),%r15
jbe    11f2 <vfprintf+0xb2>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    0x2dee(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14,%rdi
call   1030 <fseterr@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x7e0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@INT_MAX = global i64 0
@EOVERFLOW = global i32 0
define i32 @vfprintf(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [2000 x i8]
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i64 2000, i64* %11
%13 = getelementptr inbounds [2000 x i8], [2000 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i8* @vasnprintf(i8* %13, i64* %11, i8* %14, i32 %15)
store i8* %16, i8** %9
%17 = load i64, i64* %11
store i64 %17, i64* %10
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %3
%21 = load i32*, i32** %5
%22 = call i32 @fseterr(i32* %21)
store i32 -1, i32* %4
br label %58
23: ; preds = %3
%24 = load i8*, i8** %9
%25 = load i64, i64* %10
%26 = load i32*, i32** %5
%27 = call i64 @fwrite(i8* %24, i32 1, i64 %25, i32* %26)
%28 = load i64, i64* %10
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %23
%31 = load i8*, i8** %9
%32 = getelementptr inbounds [2000 x i8], [2000 x i8]* %8, i64 0, i64 0
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @errno
store i32 %35, i32* %12
%36 = load i8*, i8** %9
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %12
store i32 %38, i32* @errno
br label %39
39: ; preds = %34, %30
store i32 -1, i32* %4
br label %58
40: ; preds = %23
%41 = load i8*, i8** %9
%42 = getelementptr inbounds [2000 x i8], [2000 x i8]* %8, i64 0, i64 0
%43 = icmp ne i8* %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i8*, i8** %9
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %44, %40
%48 = load i64, i64* %10
%49 = load i64, i64* @INT_MAX
%50 = icmp ugt i64 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* @EOVERFLOW
store i32 %52, i32* @errno
%53 = load i32*, i32** %5
%54 = call i32 @fseterr(i32* %53)
store i32 -1, i32* %4
br label %58
55: ; preds = %47
%56 = load i64, i64* %10
%57 = trunc i64 %56 to i32
store i32 %57, i32* %4
br label %58
58: ; preds = %55, %51, %39, %20
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i32 0
@INT_MAX = global i64 0
@EOVERFLOW = global i32 0
define i32 @vfprintf(i32* %0, i8* %1, i32 %2) {
%4 = alloca [2000 x i8]
%5 = alloca i64
%6 = getelementptr inbounds [2000 x i8], [2000 x i8]* %4, i64 0, i64 0
%7 = bitcast i64* %5 to i8*
store i64 2000, i64* %5
%8 = call i8* @vasnprintf(i8* %6, i64* %5, i8* %1, i32 %2)
%9 = load i64, i64* %5
%10 = icmp eq i8* %8, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @fseterr(i32* %0)
br label %32
13: ; preds = %3
%14 = call i64 @fwrite(i8* %8, i32 1, i64 %9, i32* %0)
%15 = icmp ult i64 %14, %9
%16 = icmp eq i8* %8, %6
br i1 %15, label %17, label %21
17: ; preds = %13
br i1 %16, label %32, label %18
18: ; preds = %17
%19 = load i32, i32* @errno
%20 = call i32 @free(i8* %8)
store i32 %19, i32* @errno
br label %32
21: ; preds = %13
br i1 %16, label %24, label %22
22: ; preds = %21
%23 = call i32 @free(i8* %8)
br label %24
24: ; preds = %22, %21
%25 = load i64, i64* @INT_MAX
%26 = icmp ugt i64 %9, %25
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @EOVERFLOW
store i32 %28, i32* @errno
%29 = call i32 @fseterr(i32* %0)
br label %32
30: ; preds = %24
%31 = trunc i64 %9 to i32
br label %32
32: ; preds = %17, %18, %30, %27, %11
%33 = phi i32 [ -1, %27 ], [ %31, %30 ], [ -1, %11 ], [ -1, %18 ], [ -1, %17 ]
ret i32 %33
}