#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 stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v) {
  int res;
  res = vsnprintf(s, n, fmt, v);
  if (n)
    s[n - 1] = 0;
  return (res >= (int)n || res < 0) ? -1 : res;
}


x86 assembly (clang, -O0):
<stb_vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
cmpq   $0x0,-0x10(%rbp)
je     1159 <stb_vsnprintf+0x49>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    %ecx,%eax
jge    1172 <stb_vsnprintf+0x62>
cmpl   $0x0,-0x20(%rbp)
jge    117f <stb_vsnprintf+0x6f>
mov    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1185 <stb_vsnprintf+0x75>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stb_vsnprintf>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <vsnprintf@plt>
test   %rbx,%rbx
je     112a <stb_vsnprintf+0x1a>
movb   $0x0,-0x1(%rbx,%r14,1)
test   %eax,%eax
mov    $0xffffffff,%edx
mov    %eax,%ecx
cmovs  %edx,%ecx
cmp    %ebx,%eax
cmovge %edx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @stb_vsnprintf(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 i64, i64* %6
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %4
%18 = load i8*, i8** %5
%19 = load i64, i64* %6
%20 = sub i64 %19, 1
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %17, %4
%23 = load i32, i32* %9
%24 = load i64, i64* %6
%25 = trunc i64 %24 to i32
%26 = icmp sge i32 %23, %25
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27, %22
br label %33
31: ; preds = %27
%32 = load i32, i32* %9
br label %33
33: ; preds = %31, %30
%34 = phi i32 [ -1, %30 ], [ %32, %31 ]
ret i32 %34
}

LLVM IR (-O2):
define i32 @stb_vsnprintf(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 eq i64 %1, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = add i64 %1, -1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %9
br label %10
10: ; preds = %7, %4
%11 = trunc i64 %1 to i32
%12 = icmp sge i32 %5, %11
%13 = icmp slt i32 %5, 0
%14 = or i1 %12, %13
%15 = select i1 %14, i32 -1, i32 %5
ret i32 %15
}