#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 < (int)length ? retval : (int)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)
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jge    1195 <__vsnprintf+0x85>
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    119c <__vsnprintf+0x8c>
mov    -0x18(%rbp),%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
movslq %ecx,%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     114b <__vsnprintf+0x3b>
mov    %rsi,%rbx
add    $0xffffffffffffffff,%rbx
xor    %edi,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <__pformat@plt>
cmp    %ebx,%eax
cmovl  %eax,%ebx
movslq %ebx,%rcx
movb   $0x0,(%r14,%rcx,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 %40
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 = load i64, i64* %7
%28 = trunc i64 %27 to i32
%29 = icmp slt i32 %26, %28
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = load i32, i32* %10
br label %35
32: ; preds = %18
%33 = load i64, i64* %7
%34 = trunc i64 %33 to i32
br label %35
35: ; preds = %32, %30
%36 = phi i32 [ %31, %30 ], [ %34, %32 ]
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %25, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %10
store i32 %39, i32* %5
br label %40
40: ; preds = %35, %13
%41 = load i32, i32* %5
ret i32 %41
}

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 %16
8: ; preds = %4
%9 = add i64 %1, -1
%10 = tail call i32 @__pformat(i32 0, i8* %0, i64 %9, i8* %2, i32 %3)
%11 = trunc i64 %9 to i32
%12 = icmp slt i32 %10, %11
%13 = select i1 %12, i32 %10, i32 %11
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %8, %6
%17 = phi i32 [ %7, %6 ], [ %10, %8 ]
ret i32 %17
}