#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;

/* Variables and functions */
int EOVERFLOW;
size_t INT_MAX;
int errno;
int /*<<< orphan*/ free(char *);
size_t full_write(int, char *, size_t);
char *vasnprintf(char *, size_t *, char const *, int /*<<< orphan*/);
int vdprintf(int fd, 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)
    return -1;
  if (full_write(fd, output, len) < 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;
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<vdprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x7d0,-0x808(%rbp)
lea    -0x7f0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%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    1199 <vdprintf+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <vdprintf+0x13b>
mov    -0x8(%rbp),%edi
mov    -0x7f8(%rbp),%rsi
mov    -0x800(%rbp),%rdx
call   1040 <full_write@plt>
cmp    -0x800(%rbp),%rax
jae    1209 <vdprintf+0xd9>
mov    -0x7f8(%rbp),%rax
lea    -0x7f0(%rbp),%rcx
cmp    %rcx,%rax
je     11fd <vdprintf+0xcd>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80c(%rbp)
mov    -0x7f8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x80c(%rbp),%ecx
mov    0x2dd5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <vdprintf+0x13b>
mov    -0x7f8(%rbp),%rax
lea    -0x7f0(%rbp),%rcx
cmp    %rcx,%rax
je     122c <vdprintf+0xfc>
mov    -0x7f8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x800(%rbp),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jbe    1261 <vdprintf+0x131>
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <vdprintf+0x13b>
mov    -0x800(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vdprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x7e0,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %edi,%r12d
movq   $0x7d0,0x8(%rsp)
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <vasnprintf@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11d4 <vdprintf+0xa4>
mov    %rax,%rbx
mov    0x8(%rsp),%rbp
mov    %r12d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1040 <full_write@plt>
cmp    %rbp,%rax
jae    11a4 <vdprintf+0x74>
cmp    %r14,%rbx
je     11d4 <vdprintf+0xa4>
mov    0x2e3c(%rip),%r14
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,(%r14)
jmp    11d4 <vdprintf+0xa4>
cmp    %r14,%rbx
je     11b1 <vdprintf+0x81>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2e28(%rip),%rax
cmp    (%rax),%rbp
jbe    11d1 <vdprintf+0xa1>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
mov    %eax,(%rcx)
jmp    11d4 <vdprintf+0xa4>
mov    %ebp,%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 @vdprintf(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 %21, label %20
20: ; preds = %3
store i32 -1, i32* %4
br label %54
21: ; preds = %3
%22 = load i32, i32* %5
%23 = load i8*, i8** %9
%24 = load i64, i64* %10
%25 = call i64 @full_write(i32 %22, i8* %23, i64 %24)
%26 = load i64, i64* %10
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %21
%29 = load i8*, i8** %9
%30 = getelementptr inbounds [2000 x i8], [2000 x i8]* %8, i64 0, i64 0
%31 = icmp ne i8* %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @errno
store i32 %33, i32* %12
%34 = load i8*, i8** %9
%35 = call i32 @free(i8* %34)
%36 = load i32, i32* %12
store i32 %36, i32* @errno
br label %37
37: ; preds = %32, %28
store i32 -1, i32* %4
br label %54
38: ; preds = %21
%39 = load i8*, i8** %9
%40 = getelementptr inbounds [2000 x i8], [2000 x i8]* %8, i64 0, i64 0
%41 = icmp ne i8* %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i8*, i8** %9
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %42, %38
%46 = load i64, i64* %10
%47 = load i64, i64* @INT_MAX
%48 = icmp ugt i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i32, i32* @EOVERFLOW
store i32 %50, i32* @errno
store i32 -1, i32* %4
br label %54
51: ; preds = %45
%52 = load i64, i64* %10
%53 = trunc i64 %52 to i32
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %49, %37, %20
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@errno = global i32 0
@INT_MAX = global i64 0
@EOVERFLOW = global i32 0
define i32 @vdprintf(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 %29, label %11
11: ; preds = %3
%12 = call i64 @full_write(i32 %0, i8* %8, i64 %9)
%13 = icmp ult i64 %12, %9
%14 = icmp eq i8* %8, %6
br i1 %13, label %15, label %19
15: ; preds = %11
br i1 %14, label %29, label %16
16: ; preds = %15
%17 = load i32, i32* @errno
%18 = call i32 @free(i8* %8)
store i32 %17, i32* @errno
br label %29
19: ; preds = %11
br i1 %14, label %22, label %20
20: ; preds = %19
%21 = call i32 @free(i8* %8)
br label %22
22: ; preds = %20, %19
%23 = load i64, i64* @INT_MAX
%24 = icmp ugt i64 %9, %23
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* @EOVERFLOW
store i32 %26, i32* @errno
br label %29
27: ; preds = %22
%28 = trunc i64 %9 to i32
br label %29
29: ; preds = %15, %16, %3, %27, %25
%30 = phi i32 [ -1, %25 ], [ %28, %27 ], [ -1, %3 ], [ -1, %16 ], [ -1, %15 ]
ret i32 %30
}