#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 _vsnprintf(int /*<<< orphan*/, int /*<<< orphan*/, char *,
               int /*<<< orphan*/);
scalar_t__ malloc(int);
int vsprintf(char *, char *, int /*<<< orphan*/);
int vasprintf(char **sptr, char *fmt, va_list argv) {
  int wanted = _vsnprintf(*sptr = NULL, 0, fmt, argv);
  if ((wanted > 0) && ((*sptr = (char *)malloc(1 + wanted)) != NULL))
    return vsprintf(*sptr, fmt, argv);
  return wanted;
}


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   1050 <_vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11a2 <vasprintf+0x72>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
je     11a2 <vasprintf+0x72>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <vsprintf@plt>
mov    %eax,-0x4(%rbp)
jmp    11a8 <vasprintf+0x78>
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,%r15
mov    %rdi,%rbp
movq   $0x0,(%rdi)
xor    %edi,%edi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1050 <_vsnprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1185 <vasprintf+0x55>
lea    0x1(%rbx),%edi
call   1030 <malloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     1185 <vasprintf+0x55>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <vsprintf@plt>
mov    %ebx,%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*** %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 sgt i32 %13, 0
br i1 %14, label %15, label %28
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 ne i8* %19, null
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @vsprintf(i8* %24, i8* %25, i32 %26)
store i32 %27, i32* %4
br label %30
28: ; preds = %15, %3
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %22
%31 = load i32, i32* %4
ret i32 %31
}

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 sgt i32 %4, 0
br i1 %5, label %6, label %13
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 ], [ %4, %6 ], [ %4, %3 ]
ret i32 %14
}