#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 /*<<< orphan*/ EOVERFLOW;
size_t INT_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *vasnprintf(char *, size_t *, char const *, int /*<<< orphan*/);
int vsprintf(char *str, const char *format, va_list args) {
  char *output;
  size_t len;
  size_t lenbuf;
  lenbuf = (((size_t)-1) < INT_MAX ? ((size_t)-1) : INT_MAX);
  if (lenbuf > ~(uintptr_t)str)
    lenbuf = ~(uintptr_t)str;
  output = vasnprintf(str, &lenbuf, format, args);
  len = lenbuf;
  if (!output)
    return -1;
  if (output != str) {
    free(output);
    errno = EOVERFLOW;
    return -1;
  }
  if (len > INT_MAX) {
    errno = EOVERFLOW;
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<vsprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0xffffffffffffffff,%rax
mov    0x2e9f(%rip),%rcx
cmp    (%rcx),%rax
jae    115a <vsprintf+0x3a>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
jmp    1168 <vsprintf+0x48>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
xor    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jbe    1191 <vsprintf+0x71>
mov    -0x10(%rbp),%rax
xor    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x38(%rbp),%rsi
call   1040 <vasnprintf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <vsprintf+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <vsprintf+0x116>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11fd <vsprintf+0xdd>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
mov    0x2de1(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <vsprintf+0x116>
mov    -0x30(%rbp),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
jbe    122f <vsprintf+0x10f>
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    0x2daf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <vsprintf+0x116>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    0x2ea9(%rip),%r14
mov    (%r14),%rax
mov    %rdi,%rsi
not    %rsi
cmp    %rsi,%rax
cmovbe %rax,%rsi
mov    %rsi,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1040 <vasnprintf@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     118f <vsprintf+0x6f>
cmp    %rbx,%rax
je     116f <vsprintf+0x4f>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    1179 <vsprintf+0x59>
mov    0x8(%rsp),%rax
cmp    (%r14),%rax
jbe    118d <vsprintf+0x6d>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    0x2e47(%rip),%rcx
mov    %eax,(%rcx)
jmp    118f <vsprintf+0x6f>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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