#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 kprintf(char const *, int, char **, char *, int /*<<< orphan*/);
int vsnprintf(char *bf, size_t size, const char *fmt, va_list ap) {
  int retval;
  char *p;
  p = bf + size;
  retval = kprintf(fmt, 0x0008, &p, bf, ap);
  if (bf && size > 0) {
    if (size <= (size_t)retval)
      bf[size - 1] = '\0';
    else
      bf[retval] = '\0';
  }
  return retval;
}


x86 assembly (clang, -O0):
<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),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    $0x8,%esi
lea    -0x28(%rbp),%rdx
call   1030 <kprintf@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119d <vsnprintf+0x8d>
cmpq   $0x0,-0x10(%rbp)
jbe    119d <vsnprintf+0x8d>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmp    %rcx,%rax
ja     118c <vsnprintf+0x7c>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1198 <vsnprintf+0x88>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119d <vsnprintf+0x8d>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r8d
mov    %rdx,%rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    (%rdi,%rsi,1),%rcx
mov    %rcx,(%rsp)
mov    %rsp,%rdx
mov    %rax,%rdi
mov    $0x8,%esi
mov    %rbx,%rcx
call   1030 <kprintf@plt>
test   %rbx,%rbx
je     1157 <vsnprintf+0x47>
test   %r14,%r14
je     1157 <vsnprintf+0x47>
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
%10 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8* %13, i8** %10
%14 = load i8*, i8** %7
%15 = load i8*, i8** %5
%16 = load i32, i32* %8
%17 = call i32 @kprintf(i8* %14, i32 8, i8** %10, i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %39
20: ; preds = %4
%21 = load i64, i64* %6
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = load i64, i64* %6
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = icmp ule i64 %24, %26
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load i64, i64* %6
%31 = sub i64 %30, 1
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %38
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %33, %28
br label %39
39: ; preds = %38, %20, %4
%40 = load i32, i32* %9
ret i32 %40
}

LLVM IR (-O2):
define i32 @vsnprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = getelementptr inbounds i8, i8* %0, i64 %1
store i8* %7, i8** %5
%8 = call i32 @kprintf(i8* %2, i32 8, i8** %5, i8* %0, i32 %3)
%9 = icmp ne i8* %0, null
%10 = icmp ne i64 %1, 0
%11 = and i1 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %4
%13 = sext i32 %8 to i64
%14 = icmp ult i64 %13, %1
%15 = add i64 %1, -1
%16 = select i1 %14, i64 %13, i64 %15
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %12, %4
ret i32 %8
}