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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ memcpy(char *, char *, int);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) {
  int ret;
  char buffer[4096] = {
      0,
  };
  if ((int)n < 1)
    return (EOF);
  ret = vsprintf(buffer, fmt, ap);
  if (ret >= n) {
    memcpy(str, buffer, n);
    str[n - 1] = '\0';
  } else {
    memcpy(str, buffer, ret);
    str[ret] = '\0';
  }
  if (ret >= n)
    ret = -1;
  return (ret);
}


x86 assembly (clang, -O0):
<vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
cmp    $0x1,%eax
jge    117b <vsnprintf+0x4b>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <vsnprintf+0xd4>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <vsprintf@plt>
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11ca <vsnprintf+0x9a>
mov    -0x10(%rbp),%rdi
lea    -0x1030(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11e9 <vsnprintf+0xb9>
mov    -0x10(%rbp),%rdi
lea    -0x1030(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11fe <vsnprintf+0xce>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1030 <memset@plt>
test   %r15d,%r15d
jle    1185 <vsnprintf+0x55>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <vsprintf@plt>
movslq %eax,%rbx
mov    %rsp,%rsi
mov    %r14,%rdi
cmp    %r15,%rbx
jae    1190 <vsnprintf+0x60>
mov    %ebx,%edx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
jmp    11a3 <vsnprintf+0x73>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebx
jmp    11a3 <vsnprintf+0x73>
mov    %r15d,%edx
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%r15,%r14,1)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4096 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = bitcast [4096 x i8]* %11 to i8*
%13 = load i64, i64* %7
%14 = trunc i64 %13 to i32
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* @EOF
store i32 %17, i32* %5
br label %54
18: ; preds = %4
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%20 = load i8*, i8** %8
%21 = load i32, i32* %9
%22 = call i32 @vsprintf(i8* %19, i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = load i64, i64* %7
%26 = icmp uge i64 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %18
%28 = load i8*, i8** %6
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%30 = load i64, i64* %7
%31 = trunc i64 %30 to i32
%32 = call i32 @memcpy(i8* %28, i8* %29, i32 %31)
%33 = load i8*, i8** %6
%34 = load i64, i64* %7
%35 = sub i64 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %46
37: ; preds = %18
%38 = load i8*, i8** %6
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%40 = load i32, i32* %10
%41 = call i32 @memcpy(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %37, %27
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = load i64, i64* %7
%50 = icmp uge i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %46
store i32 -1, i32* %10
br label %52
52: ; preds = %51, %46
%53 = load i32, i32* %10
store i32 %53, i32* %5
br label %54
54: ; preds = %52, %16
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@EOF = global i32 0
define i32 @vsnprintf(i8* %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = trunc i64 %1 to i32
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = load i32, i32* @EOF
br label %22
11: ; preds = %4
%12 = call i32 @vsprintf(i8* %6, i8* %2, i32 %3)
%13 = sext i32 %12 to i64
%14 = icmp ult i64 %13, %1
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = call i32 @memcpy(i8* %0, i8* %6, i32 %7)
%17 = add i64 %1, -1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 0, i8* %18
br label %22
19: ; preds = %11
%20 = call i32 @memcpy(i8* %0, i8* %6, i32 %12)
%21 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %21
br label %22
22: ; preds = %15, %19, %9
%23 = phi i32 [ %10, %9 ], [ %12, %19 ], [ -1, %15 ]
ret i32 %23
}