#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*/ free(char *);
char *strdup_vprintf(char const *, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int vsnprintf(char *str, size_t n, char const *fmt, va_list args) {
  char *printed;
  int len;
  printed = strdup_vprintf(fmt, args);
  if (!printed)
    return -1;
  len = strlen(printed);
  strncpy(str, printed, n);
  str[n - 1] = '\0';
  free(printed);
  return len >= n ? -1 : len;
}


x86 assembly (clang, -O0):
<vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <strdup_vprintf@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    117e <vsnprintf+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <vsnprintf+0x9b>
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movslq -0x34(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11cf <vsnprintf+0x8f>
mov    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
jmp    11d5 <vsnprintf+0x95>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
mov    %ecx,%esi
call   1050 <strdup_vprintf@plt>
test   %rax,%rax
je     1195 <vsnprintf+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
movb   $0x0,-0x1(%r15,%r14,1)
mov    %rbx,%rdi
call   1030 <free@plt>
movslq %ebp,%rax
cmp    %r15,%rax
mov    $0xffffffff,%ecx
cmovae %ecx,%eax
jmp    119a <vsnprintf+0x5a>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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 i8*
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %8
%13 = load i32, i32* %9
%14 = call i8* @strdup_vprintf(i8* %12, i32 %13)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %4
store i32 -1, i32* %5
br label %40
18: ; preds = %4
%19 = load i8*, i8** %10
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %11
%21 = load i8*, i8** %6
%22 = load i8*, i8** %10
%23 = load i64, i64* %7
%24 = call i32 @strncpy(i8* %21, i8* %22, i64 %23)
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %10
%30 = call i32 @free(i8* %29)
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = load i64, i64* %7
%34 = icmp uge i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %18
br label %38
36: ; preds = %18
%37 = load i32, i32* %11
br label %38
38: ; preds = %36, %35
%39 = phi i32 [ -1, %35 ], [ %37, %36 ]
store i32 %39, i32* %5
br label %40
40: ; preds = %38, %17
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
define i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = tail call i8* @strdup_vprintf(i8* %2, i32 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %5)
%9 = tail call i32 @strncpy(i8* %0, i8* %5, i64 %1)
%10 = add i64 %1, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @free(i8* %5)
%13 = sext i32 %8 to i64
%14 = icmp ult i64 %13, %1
%15 = select i1 %14, i32 %8, i32 -1
br label %16
16: ; preds = %4, %7
%17 = phi i32 [ %15, %7 ], [ -1, %4 ]
ret i32 %17
}