#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 __pformat(int /*<<< orphan*/, char *, size_t, char const *,
              int /*<<< orphan*/);
int __attribute__((__cdecl__))
__vsnprintf(char *buf, size_t length, const char *fmt, va_list argv) {
  register int retval;
  if (length == (size_t)(0))
    return __pformat(0, buf, 0, fmt, argv);
  retval = __pformat(0, buf, --length, fmt, argv);
  buf[retval < length ? retval : length] = '\0';
  return retval;
}


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)
cmpq   $0x0,-0x18(%rbp)
jne    1151 <__vsnprintf+0x41>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <__pformat@plt>
mov    %eax,-0x4(%rbp)
jmp    11b0 <__vsnprintf+0xa0>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
xor    %edi,%edi
call   1030 <__pformat@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1196 <__vsnprintf+0x86>
movslq -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    119e <__vsnprintf+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__vsnprintf>:
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r8d
mov    %rdx,%rcx
mov    %rdi,%r14
test   %rsi,%rsi
je     114c <__vsnprintf+0x3c>
mov    %rsi,%rbx
add    $0xffffffffffffffff,%rbx
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <__pformat@plt>
cltq
cmp    %rax,%rbx
cmova  %rax,%rbx
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
mov    %r14,%rsi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <__pformat@plt>

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

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