#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*/ exit(int);
char *malloc(int);
char *realloc(char *, int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vasprintf(char **strp, const char *fmt, va_list ap) {
  char *buf;
  int buf_s, buf_n;
  buf_s = 100;
  buf = malloc(buf_s);
  if (buf == NULL)
    exit(1);
  while (1) {
    buf_n = vsnprintf(buf, buf_s, fmt, ap);
    if (buf_n < buf_s) {
      *strp = buf;
      return buf_n;
    }
    buf_s = buf_s * 2;
    buf = realloc(buf, buf_s);
    if (buf == NULL)
      exit(1);
  }
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x64,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117b <vasprintf+0x3b>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1180 <vasprintf+0x40>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11b6 <vasprintf+0x76>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <vasprintf+0xa4>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1180 <vasprintf+0x40>

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r14
mov    $0x64,%ebp
mov    $0x64,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1198 <vasprintf+0x58>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1030 <vsnprintf@plt>
cmp    %ebp,%eax
jl     11a2 <vasprintf+0x62>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <vasprintf+0x30>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rbx,(%r14)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vasprintf(i8** %0, i8* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 100, i32* %8
%10 = load i32, i32* %8
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
br label %17
17: ; preds = %16, %40
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @vsnprintf(i8* %18, i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i8*, i8** %7
%28 = load i8**, i8*** %4
store i8* %27, i8** %28
%29 = load i32, i32* %9
ret i32 %29
30: ; preds = %17
%31 = load i32, i32* %8
%32 = mul nsw i32 %31, 2
store i32 %32, i32* %8
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = call i8* @realloc(i8* %33, i32 %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
br label %17
}

LLVM IR (-O2):
define i32 @vasprintf(i8** nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %3, %14
%9 = phi i32 [ %15, %14 ], [ 100, %3 ]
%10 = phi i8* [ %16, %14 ], [ %4, %3 ]
%11 = tail call i32 @vsnprintf(i8* %10, i32 %9, i8* %1, i32 %2)
%12 = icmp slt i32 %11, %9
br i1 %12, label %13, label %14
13: ; preds = %8
store i8* %10, i8** %0
ret i32 %11
14: ; preds = %8
%15 = shl nsw i32 %9, 1
%16 = tail call i8* @realloc(i8* %10, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %8
18: ; preds = %14
%19 = tail call i32 @exit(i32 1)
unreachable
}