#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 */
char *malloc(size_t);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int a_sprintfv(char **buf, const char *format, va_list lst) {
  size_t size = vsnprintf(NULL, 0, format, lst) + 1;
  *buf = malloc(size);
  if (!*buf)
    return -1;
  (*buf)[0] = '\0';
  size = vsnprintf(*buf, size, format, lst);
  return size;
}


x86 assembly (clang, -O0):
<a_sprintfv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1030 <vsnprintf@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    117c <a_sprintfv+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <a_sprintfv+0x8a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a_sprintfv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1173 <a_sprintfv+0x53>
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <vsnprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @a_sprintfv(i8** %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = load i32, i32* %7
%11 = call i32 @vsnprintf(i8* null, i64 0, i8* %9, i32 %10)
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = call i8* @malloc(i64 %14)
%16 = load i8**, i8*** %5
store i8* %15, i8** %16
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 -1, i32* %4
br label %34
21: ; preds = %3
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = getelementptr inbounds i8, i8* %23, i64 0
store i8 0, i8* %24
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = load i64, i64* %8
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @vsnprintf(i8* %26, i64 %27, i8* %28, i32 %29)
%31 = sext i32 %30 to i64
store i64 %31, i64* %8
%32 = load i64, i64* %8
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %34
34: ; preds = %21, %20
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
define i32 @a_sprintfv(i8** nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @vsnprintf(i8* null, i64 0, i8* %1, i32 %2)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @malloc(i64 %6)
store i8* %7, i8** %0
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
store i8 0, i8* %7
%10 = tail call i32 @vsnprintf(i8* %7, i64 %6, i8* %1, i32 %2)
br label %11
11: ; preds = %3, %9
%12 = phi i32 [ %10, %9 ], [ -1, %3 ]
ret i32 %12
}