#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*/ ENOMEM;
int _vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
char *malloc(int);
int vasprintf(char **ret, const char *format, va_list ap) {
  int len;
  len = _vsnprintf(NULL, 0, format, ap);
  if (len < 0)
    return -1;
  *ret = malloc(len + 1);
  if (!*ret) {
    errno = ENOMEM;
    return -1;
  }
  _vsnprintf(*ret, len + 1, format, ap);
  (*ret)[len] = '\0';
  return len;
}


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   1040 <_vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    115e <vasprintf+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <vasprintf+0xad>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119f <vasprintf+0x7f>
mov    0x2e58(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <vasprintf+0xad>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <_vsnprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
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   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
xor    %edi,%edi
xor    %esi,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <_vsnprintf@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1192 <vasprintf+0x72>
mov    %eax,%ebp
lea    0x1(%rbp),%r13d
mov    %r13d,%edi
call   1030 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1180 <vasprintf+0x60>
mov    %rax,%rdi
mov    %r13d,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <_vsnprintf@plt>
mov    (%r14),%rax
mov    %ebp,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    %ebp,%ebx
jmp    1192 <vasprintf+0x72>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e48(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
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 slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %39
15: ; preds = %3
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
%18 = call i8* @malloc(i32 %17)
%19 = load i8**, i8*** %5
store i8* %18, i8** %19
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* @errno
store i32 -1, i32* %4
br label %39
25: ; preds = %15
%26 = load i8**, i8*** %5
%27 = load i8*, i8** %26
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @_vsnprintf(i8* %27, i32 %29, i8* %30, i32 %31)
%33 = load i8**, i8*** %5
%34 = load i8*, i8** %33
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %39
39: ; preds = %25, %23, %14
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @vasprintf(i8** nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @_vsnprintf(i8* null, i32 0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = add nuw nsw i32 %4, 1
%8 = tail call i8* @malloc(i32 %7)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = load i32, i32* @ENOMEM
store i32 %11, i32* @errno
br label %17
12: ; preds = %6
%13 = tail call i32 @_vsnprintf(i8* %8, i32 %7, i8* %1, i32 %2)
%14 = load i8*, i8** %0
%15 = zext i32 %4 to i64
%16 = getelementptr inbounds i8, i8* %14, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %3, %12, %10
%18 = phi i32 [ %4, %12 ], [ -1, %10 ], [ -1, %3 ]
ret i32 %18
}