#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*/ EXIT_FAILURE;
size_t INT_MAX;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
  int i;
  if (len > INT_MAX) {
    errx(EXIT_FAILURE, "len > INT_MAX");
  }
  i = vsnprintf(buf, len, fmt, ap);
  if (i < 0) {
    err(EXIT_FAILURE, "vsnprintf failed");
  }
  return (i);
}


x86 assembly (clang, -O0):
<xvsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
jbe    1170 <xvsnprintf+0x40>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
call   1040 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a6 <xvsnprintf+0x76>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <err@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xvsnprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e9e(%rip),%rax
cmp    %rsi,(%rax)
jae    1164 <xvsnprintf+0x34>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
call   1040 <errx@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1190 <xvsnprintf+0x60>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
call   1050 <err@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@INT_MAX = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"len > INT_MAX\00"
@.str.1 = [17 x i8] c"vsnprintf failed\00"
define i32 @xvsnprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load i64, i64* %6
%11 = load i64, i64* @INT_MAX
%12 = icmp ugt i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @errx(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %4
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = call i32 @vsnprintf(i8* %17, i64 %18, i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @err(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %16
%28 = load i32, i32* %9
ret i32 %28
}

LLVM IR (-O2):
@INT_MAX = global i64 0
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"len > INT_MAX\00"
@.str.1 = [17 x i8] c"vsnprintf failed\00"
define i32 @xvsnprintf(i8* nocapture %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = load i64, i64* @INT_MAX
%6 = icmp ult i64 %5, %1
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @errx(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %4
%11 = tail call i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @err(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %10
ret i32 %11
}