#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*/ FILE;

/* Variables and functions */
int _fwrite(char *, int, int /*<<< orphan*/ *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vfprintf(FILE *file, const char *format, va_list ap) {
  int rv;
  char buffer[32768];
  rv = vsnprintf(buffer, 32768, format, ap);
  if (rv < 0)
    return rv;
  if (rv > 32768 - 1)
    rv = 32768 - 1;
  return _fwrite(buffer, rv, file);
}


x86 assembly (clang, -O0):
<vfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8020,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x8020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x8000,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1166 <vfprintf+0x46>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <vfprintf+0x70>
cmpl   $0x7fff,-0x20(%rbp)
jle    117a <vfprintf+0x5a>
movl   $0x7fff,-0x20(%rbp)
lea    -0x8020(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <_fwrite@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vfprintf>:
push   %rbx
sub    $0x8000,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x8000,%esi
call   1030 <vsnprintf@plt>
test   %eax,%eax
js     1159 <vfprintf+0x39>
cmp    $0x7fff,%eax
mov    $0x7fff,%esi
cmovb  %eax,%esi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1040 <_fwrite@plt>
add    $0x8000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @vfprintf(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32768 x i8]
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [32768 x i8], [32768 x i8]* %9, i64 0, i64 0
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @vsnprintf(i8* %10, i32 32768, i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %27
18: ; preds = %3
%19 = load i32, i32* %8
%20 = icmp sgt i32 %19, 32767
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 32767, i32* %8
br label %22
22: ; preds = %21, %18
%23 = getelementptr inbounds [32768 x i8], [32768 x i8]* %9, i64 0, i64 0
%24 = load i32, i32* %8
%25 = load i32*, i32** %5
%26 = call i32 @_fwrite(i8* %23, i32 %24, i32* %25)
store i32 %26, i32* %4
br label %27
27: ; preds = %22, %16
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @vfprintf(i32* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [32768 x i8]
%5 = getelementptr inbounds [32768 x i8], [32768 x i8]* %4, i64 0, i64 0
%6 = call i32 @vsnprintf(i8* %5, i32 32768, i8* %1, i32 %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = icmp ult i32 %6, 32767
%10 = select i1 %9, i32 %6, i32 32767
%11 = call i32 @_fwrite(i8* %5, i32 %10, i32* %0)
br label %12
12: ; preds = %3, %8
%13 = phi i32 [ %11, %8 ], [ %6, %3 ]
ret i32 %13
}