#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*/ __va_copy(int /*<<< orphan*/, int /*<<< orphan*/);
char *calloc(int, int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int vasprintf(char **ret, const char *fmt, va_list args) {
  va_list ap;
  char c = 0;
  int size = 0;
  if (ret == NULL) {
    return -1;
  }
  __va_copy(ap, args);
  size = vsnprintf(&c, 1, fmt, args);
  *ret = calloc(size + 1, 1);
  if (!(*ret)) {
    return -1;
  }
  size = vsprintf(*ret, fmt, ap);
  return size;
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <vasprintf+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <vasprintf+0xa8>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <__va_copy@plt>
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x21(%rbp),%rdi
mov    $0x1,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11cc <vasprintf+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <vasprintf+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <vsprintf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xf(%rsp)
test   %rdi,%rdi
je     119d <vasprintf+0x5d>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %edx,%esi
call   1050 <__va_copy@plt>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1030 <vsnprintf@plt>
lea    0x1(%rax),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     119d <vasprintf+0x5d>
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <vsprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @vasprintf(i8** writeonly %0, i8* nocapture %1, i32 %2) {
%4 = alloca i8
store i8 0, i8* %4
%5 = icmp eq i8** %0, null
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32 @__va_copy(i32 undef, i32 %2)
%8 = call i32 @vsnprintf(i8* %4, i32 1, i8* %1, i32 %2)
%9 = add nsw i32 %8, 1
%10 = tail call i8* @calloc(i32 %9, i32 1)
store i8* %10, i8** %0
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = tail call i32 @vsprintf(i8* %10, i8* %1, i32 undef)
br label %14
14: ; preds = %6, %3, %12
%15 = phi i32 [ %13, %12 ], [ -1, %3 ], [ -1, %6 ]
ret i32 %15
}