#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 format_string(char *, int /*<<< orphan*/ *, int /*<<< orphan*/, size_t,
                  char const *, int /*<<< orphan*/);
int vsnprintf(char *str, size_t size, const char *format, va_list arg) {
  int r = format_string(str, NULL, 0, size, format, arg);
  if (str != NULL) {
    if (r < size) {
      str[r] = '\0';
    } else {
      str[size - 1] = '\0';
    }
  }
  return r;
}


x86 assembly (clang, -O0):
<vsnprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <format_string@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1184 <vsnprintf+0x74>
movslq -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    116f <vsnprintf+0x5f>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    117f <vsnprintf+0x6f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1184 <vsnprintf+0x74>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r9d
mov    %rdx,%r8
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1030 <format_string@plt>
test   %rbx,%rbx
je     1143 <vsnprintf+0x33>
movslq %eax,%rcx
lea    -0x1(%r14),%rdx
cmp    %r14,%rcx
cmovb  %rcx,%rdx
movb   $0x0,(%rbx,%rdx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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