#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*/ string_overflow_handler(char *, char *);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int vsn_printf(char *dst, size_t size, const char *format, va_list args) {
  int ret = vsnprintf(dst, size, format, args);
  if (ret >= size || ret < 0) {
    string_overflow_handler("Buffer truncated '%s'\n", dst);
  }
  return ret;
}


x86 assembly (clang, -O0):
<vsn_printf>:
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    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1166 <vsn_printf+0x46>
cmpl   $0x0,-0x20(%rbp)
jge    1176 <vsn_printf+0x56>
mov    -0x8(%rbp),%rsi
lea    0xe8f(%rip),%rdi
call   1040 <string_overflow_handler@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsn_printf>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
test   %eax,%eax
js     113d <vsn_printf+0x1d>
movslq %ebp,%rax
cmp    %rbx,%rax
jb     114c <vsn_printf+0x2c>
lea    0xebc(%rip),%rdi
mov    %r14,%rsi
call   1040 <string_overflow_handler@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [23 x i8] c"Buffer truncated '%s'\0A\00"
define i32 @vsn_printf(i8* %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3)
%6 = sext i32 %5 to i64
%7 = icmp uge i64 %6, %1
%8 = icmp slt i32 %5, 0
%9 = or i1 %8, %7
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @string_overflow_handler(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %4, %10
ret i32 %5
}