#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 /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
int /*<<< orphan*/ vsprintf(char *, char const *, int /*<<< orphan*/);
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {
  char *temp = (char *)malloc(16384);
  int ret;
  vsprintf(temp, format, ap);
  ret = strlen(temp);
  if (ret > size)
    ret = size - 1;
  memcpy(str, temp, ret);
  free(temp);
  str[ret] = 0;
  return ret;
}


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    $0x4000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <vsprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11aa <vsnprintf+0x5a>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsnprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    $0x4000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <vsprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
lea    -0x1(%r13),%eax
cmp    %r13,%rbp
cmova  %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 i8*
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = call i64 @malloc(i32 16384)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 @vsprintf(i8* %13, i8* %14, i32 %15)
%17 = load i8*, i8** %9
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = load i64, i64* %6
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %4
%24 = load i64, i64* %6
%25 = sub i64 %24, 1
%26 = trunc i64 %25 to i32
store i32 %26, i32* %10
br label %27
27: ; preds = %23, %4
%28 = load i8*, i8** %5
%29 = load i8*, i8** %9
%30 = load i32, i32* %10
%31 = call i32 @memcpy(i8* %28, i8* %29, i32 %30)
%32 = load i8*, i8** %9
%33 = call i32 @free(i8* %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* %10
ret i32 %38
}

LLVM IR (-O2):
define i32 @vsnprintf(i8* %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i64 @malloc(i32 16384)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @vsprintf(i8* %6, i8* %2, i32 %3)
%8 = tail call i32 @strlen(i8* %6)
%9 = sext i32 %8 to i64
%10 = icmp ugt i64 %9, %1
%11 = trunc i64 %1 to i32
%12 = add i32 %11, -1
%13 = select i1 %10, i32 %12, i32 %8
%14 = tail call i32 @memcpy(i8* %0, i8* %6, i32 %13)
%15 = tail call i32 @free(i8* %6)
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
ret i32 %13
}