#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 vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
int evsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
  int ret;
  ret = vsnprintf(str, size, fmt, ap);
  if (ret < 0) {
    warn("vsnprintf:");
    return -1;
  } else if ((size_t)ret >= size) {
    warn("vsnprintf: Output truncated");
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<evsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1170 <evsnprintf+0x50>
lea    0xea1(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <evsnprintf+0x81>
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1196 <evsnprintf+0x76>
lea    0xe86(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <evsnprintf+0x81>
jmp    119b <evsnprintf+0x7b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evsnprintf>:
push   %rbx
mov    %rsi,%rbx
call   1040 <vsnprintf@plt>
test   %eax,%eax
js     113d <evsnprintf+0x1d>
mov    %eax,%ecx
cmp    %rbx,%rcx
jb     114e <evsnprintf+0x2e>
lea    0xed0(%rip),%rdi
jmp    1144 <evsnprintf+0x24>
lea    0xebc(%rip),%rdi
call   1030 <warn@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"vsnprintf:\00"
@.str.1 = [28 x i8] c"vsnprintf: Output truncated\00"
define i32 @evsnprintf(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
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32 @vsnprintf(i8* %11, i64 %12, i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %30
20: ; preds = %4
%21 = load i32, i32* %10
%22 = sext i32 %21 to i64
%23 = load i64, i64* %7
%24 = icmp uge i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %30
27: ; preds = %20
br label %28
28: ; preds = %27
%29 = load i32, i32* %10
store i32 %29, i32* %5
br label %30
30: ; preds = %28, %25, %18
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
@.str = [11 x i8] c"vsnprintf:\00"
@.str.1 = [28 x i8] c"vsnprintf: Output truncated\00"
define i32 @evsnprintf(i8* nocapture %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = zext i32 %5 to i64
%9 = icmp ult i64 %8, %1
br i1 %9, label %13, label %10
10: ; preds = %7, %4
%11 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %7 ]
%12 = tail call i32 @warn(i8* %11)
br label %13
13: ; preds = %10, %7
%14 = phi i32 [ %5, %7 ], [ -1, %10 ]
ret i32 %14
}