#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(char *, int, char const *, int /*<<< orphan*/);
int vasprintf(char **ptr, const char *format, va_list ap) {
  int ret;
  ret = vsnprintf(0, 0, format, ap);
  if (ret <= 0)
    return ret;
  (*ptr) = (char *)malloc(ret + 1);
  if (!*ptr)
    return -1;
  ret = vsnprintf(*ptr, ret + 1, format, ap);
  return ret;
}


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    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     115d <vasprintf+0x3d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ae <vasprintf+0x8e>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118c <vasprintf+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <vasprintf+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
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>
mov    %eax,%ebp
test   %eax,%eax
jle    1176 <vasprintf+0x56>
add    $0x1,%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1171 <vasprintf+0x51>
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <vsnprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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 i32
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, i32 0, i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %8
store i32 %15, i32* %4
br label %35
16: ; preds = %3
%17 = load i32, i32* %8
%18 = add nsw i32 %17, 1
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = load i8**, i8*** %5
store i8* %20, i8** %21
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %16
store i32 -1, i32* %4
br label %35
26: ; preds = %16
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 1
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = call i32 @vsnprintf(i8* %28, i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %26, %25, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @vasprintf(i8** nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @vsnprintf(i8* null, i32 0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 1
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 @vsnprintf(i8* %9, i32 %7, i8* %1, i32 %2)
br label %13
13: ; preds = %6, %3, %11
%14 = phi i32 [ %12, %11 ], [ %4, %3 ], [ -1, %6 ]
ret i32 %14
}