#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*/ free(char *);
scalar_t__ malloc(int);
int vsnprintf(char *, int /*<<< orphan*/, char const *, int /*<<< orphan*/);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int vasprintf(char **buf, const char *fmt, va_list ap) {
  static char _T_emptybuffer = '\0';
  int chars;
  char *b;
  if (!buf) {
    return -1;
  }
  chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap) + 1;
  if (chars < 0) {
    chars *= -1;
  }
  b = (char *)malloc(sizeof(char) * chars);
  if (!b) {
    return -1;
  }
  if ((chars = vsprintf(b, fmt, ap)) < 0) {
    free(b);
  } else {
    *buf = b;
  }
  return chars;
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116a <vasprintf+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <vasprintf+0xbb>
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0x2ec9(%rip),%rdi
xor    %esi,%esi
call   1040 <vsnprintf@plt>
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1196 <vasprintf+0x56>
imul   $0xffffffff,-0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c0 <vasprintf+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <vasprintf+0xbb>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <vsprintf@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11ea <vasprintf+0xaa>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11f5 <vasprintf+0xb5>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11a8 <vasprintf+0x68>
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x2edf(%rip),%rdi
xor    %esi,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
lea    0x1(%rax),%edi
cmp    $0xffffffff,%eax
not    %eax
cmovl  %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a8 <vasprintf+0x68>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1060 <vsprintf@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a0 <vasprintf+0x60>
mov    %rbp,(%r14)
jmp    11a8 <vasprintf+0x68>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@vasprintf._T_emptybuffer = internal global i8 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
%9 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8**, i8*** %5
%11 = icmp ne i8** %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 -1, i32* %4
br label %47
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @vsnprintf(i8* @vasprintf._T_emptybuffer, i32 0, i8* %14, i32 %15)
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* %8
%22 = mul nsw i32 %21, -1
store i32 %22, i32* %8
br label %23
23: ; preds = %20, %13
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = mul i64 1, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %23
store i32 -1, i32* %4
br label %47
33: ; preds = %23
%34 = load i8*, i8** %9
%35 = load i8*, i8** %6
%36 = load i32, i32* %7
%37 = call i32 @vsprintf(i8* %34, i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i8*, i8** %9
%41 = call i32 @free(i8* %40)
br label %45
42: ; preds = %33
%43 = load i8*, i8** %9
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %47
47: ; preds = %45, %32, %12
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@vasprintf._T_emptybuffer = internal global i8 0
define i32 @vasprintf(i8** writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i8** %0, null
br i1 %4, label %20, label %5
5: ; preds = %3
%6 = tail call i32 @vsnprintf(i8* @vasprintf._T_emptybuffer, i32 0, i8* %1, i32 %2)
%7 = add nsw i32 %6, 1
%8 = icmp slt i32 %6, -1
%9 = xor i32 %6, -1
%10 = select i1 %8, i32 %9, i32 %7
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %20, label %14
14: ; preds = %5
%15 = tail call i32 @vsprintf(i8* %12, i8* %1, i32 %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @free(i8* %12)
br label %20
19: ; preds = %14
store i8* %12, i8** %0
br label %20
20: ; preds = %17, %19, %5, %3
%21 = phi i32 [ -1, %3 ], [ -1, %5 ], [ %15, %19 ], [ %15, %17 ]
ret i32 %21
}