#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 _vscprintf(char const *, int /*<<< orphan*/);
int _vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int vasprintf(char **strp, const char *fmt, va_list ap) {
  int len = _vscprintf(fmt, ap);
  if (len == -1) {
    return -1;
  }
  size_t size = (size_t)len + 1;
  char *str = malloc(size);
  if (!str) {
    return -1;
  }
  int r = _vsnprintf(str, len + 1, fmt, ap);
  if (r == -1) {
    free(str);
    return -1;
  }
  *strp = str;
  return r;
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <_vscprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1178 <vasprintf+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <vasprintf+0xb1>
movslq -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a8 <vasprintf+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <vasprintf+0xb1>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1060 <_vsnprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11e0 <vasprintf+0xa0>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <vasprintf+0xb1>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r12d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
call   1050 <_vscprintf@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11a7 <vasprintf+0x67>
mov    %eax,%ebp
movslq %eax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11a7 <vasprintf+0x67>
mov    %rax,%r13
add    $0x1,%ebp
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1060 <_vsnprintf@plt>
cmp    $0xffffffff,%eax
je     119f <vasprintf+0x5f>
mov    %r13,(%r14)
mov    %eax,%r15d
jmp    11a7 <vasprintf+0x67>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @_vscprintf(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %43
18: ; preds = %3
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = add i64 %20, 1
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %18
store i32 -1, i32* %4
br label %43
27: ; preds = %18
%28 = load i8*, i8** %10
%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* %11
%34 = load i32, i32* %11
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i8*, i8** %10
%38 = call i32 @free(i8* %37)
store i32 -1, i32* %4
br label %43
39: ; preds = %27
%40 = load i8*, i8** %10
%41 = load i8**, i8*** %5
store i8* %40, i8** %41
%42 = load i32, i32* %11
store i32 %42, i32* %4
br label %43
43: ; preds = %39, %36, %26, %17
%44 = load i32, i32* %4
ret i32 %44
}

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