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


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)
mov    -0x20(%rbp),%eax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     117b <vasprintf+0x5b>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1187 <vasprintf+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <vasprintf+0x86>
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    -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,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
xor    %edi,%edi
xor    %esi,%esi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
mov    0x2e9c(%rip),%rax
mov    $0xffffffff,%ebx
cmp    (%rax),%ebp
jge    117c <vasprintf+0x5c>
lea    0x1(%rbp),%r13d
mov    %r13d,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     117c <vasprintf+0x5c>
mov    %rax,%rdi
mov    %r13d,%esi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@INT_MAX = 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 = load i32, i32* @INT_MAX
%14 = sub nsw i32 %13, 1
%15 = icmp sgt i32 %12, %14
br i1 %15, label %22, label %16
16: ; preds = %3
%17 = load i32, i32* %8
%18 = add nsw i32 %17, 1
%19 = call i8* @malloc(i32 %18)
%20 = load i8**, i8*** %5
store i8* %19, i8** %20
%21 = icmp ne i8* %19, null
br i1 %21, label %23, label %22
22: ; preds = %16, %3
store i32 -1, i32* %4
br label %32
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @vsnprintf(i8* %25, i32 %27, i8* %28, i32 %29)
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %32
32: ; preds = %23, %22
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@INT_MAX = global i32 0
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 = load i32, i32* @INT_MAX
%6 = icmp slt i32 %4, %5
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = add nsw i32 %4, 1
%9 = tail call i8* @malloc(i32 %8)
store i8* %9, i8** %0
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @vsnprintf(i8* %9, i32 %8, i8* %1, i32 %2)
br label %13
13: ; preds = %3, %7, %11
%14 = phi i32 [ %4, %11 ], [ -1, %7 ], [ -1, %3 ]
ret i32 %14
}