#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 */
scalar_t__ malloc(int);
int vsnprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *,
              int /*<<< orphan*/);
int vsprintf(char *, char *, int /*<<< orphan*/);
int vasprintf(char **strPtr, char *format, va_list argv) {
  int needed = vsnprintf((*strPtr = NULL), 0, format, argv);
  if ((needed < 0) || ((*strPtr = (char *)malloc(1 + needed)) == NULL)) {
    return -1;
  }
  return vsprintf(*strPtr, format, argv);
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %esi,%esi
mov    %esi,%edi
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1187 <vasprintf+0x57>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1193 <vasprintf+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <vasprintf+0x79>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <vsprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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