#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;
typedef scalar_t__ ssize_t;

/* Variables and functions */
char *malloc(scalar_t__);
int vsnprintf(char *, scalar_t__, char const *, int /*<<< orphan*/);
int vasprintf(char **strp, const char *fmt, va_list ap) {
  ssize_t len = vsnprintf(NULL, 0, fmt, ap) + 1;
  char *res = malloc(len);
  if (res == NULL)
    return -1;
  *strp = res;
  return vsnprintf(res, len, fmt, ap);
}


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)
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,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1173 <vasprintf+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <vasprintf+0x75>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <vsnprintf@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     1170 <vasprintf+0x50>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%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 @vasprintf(i8** %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @vsnprintf(i8* null, i64 0, i8* %10, i32 %11)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %28
20: ; preds = %3
%21 = load i8*, i8** %9
%22 = load i8**, i8*** %5
store i8* %21, i8** %22
%23 = load i8*, i8** %9
%24 = load i64, i64* %8
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @vsnprintf(i8* %23, i64 %24, i8* %25, i32 %26)
store i32 %27, i32* %4
br label %28
28: ; preds = %20, %19
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @vasprintf(i8** nocapture writeonly %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)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
store i8* %7, i8** %0
%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
}