#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ bufToFile(int /*<<< orphan*/ *, char *, size_t);
int vfprintf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int vsnprintf(char *s, size_t n, const char *format, va_list arg) {
  if (n == 0) {
    return -1;
  }
  FILE str;
  bufToFile(&str, s, n - 1);
  int res = vfprintf(&str, format, arg);
  if (res > 0) {
    s[res] = '\0';
  }
  return res;
}


x86 assembly (clang, -O0):
<vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114e <vsnprintf+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <vsnprintf+0x72>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
lea    -0x28(%rbp),%rdi
call   1030 <bufToFile@plt>
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    -0x28(%rbp),%rdi
call   1040 <vfprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    118c <vsnprintf+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
test   %rsi,%rsi
je     116f <vsnprintf+0x4f>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%rdx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rdx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <bufToFile@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <vfprintf@plt>
test   %eax,%eax
jle    1164 <vsnprintf+0x44>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
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 i32
%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 i64, i64* %7
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 -1, i32* %5
br label %32
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = sub i64 %17, 1
%19 = call i32 @bufToFile(i32* %10, i8* %16, i64 %18)
%20 = load i8*, i8** %8
%21 = load i32, i32* %9
%22 = call i32 @vfprintf(i32* %10, i8* %20, i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = load i8*, i8** %6
%27 = load i32, i32* %11
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %25, %15
%31 = load i32, i32* %11
store i32 %31, i32* %5
br label %32
32: ; preds = %30, %14
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
define i32 @vsnprintf(i8* %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = alloca i32
%6 = icmp eq i64 %1, 0
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = bitcast i32* %5 to i8*
%9 = add i64 %1, -1
%10 = call i32 @bufToFile(i32* %5, i8* %0, i64 %9)
%11 = call i32 @vfprintf(i32* %5, i8* %2, i32 %3)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = zext i32 %11 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %13, %7
br label %17
17: ; preds = %4, %16
%18 = phi i32 [ %11, %16 ], [ -1, %4 ]
ret i32 %18
}