#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 /*<<< orphan*/ errno;
scalar_t__ malloc(size_t);
char *strdup(char *);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int vasprintf(char **strp, const char *fmt, va_list ap) {
  char buffer[256];
  int count = vsnprintf(buffer, 256, fmt, ap);
  if (count < 256) {
    *strp = strdup(buffer);
    return count;
  } else {
    *strp = (char *)malloc((size_t)count + 1);
    if (*strp == NULL) {
      errno = ENOMEM;
      return -1;
    };
    return vsnprintf(*strp, (size_t)count + 1, fmt, ap);
  };
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x100,%esi
call   1030 <vsnprintf@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x100,-0x124(%rbp)
jge    1198 <vasprintf+0x68>
lea    -0x120(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <vasprintf+0xcf>
movslq -0x124(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11de <vasprintf+0xae>
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <vasprintf+0xcf>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x124(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
cmp    $0xff,%eax
jg     116f <vasprintf+0x3f>
mov    %rsp,%rdi
call   1050 <strdup@plt>
mov    %rax,(%r14)
jmp    11ba <vasprintf+0x8a>
mov    %ebp,%ebp
add    $0x1,%rbp
mov    %rbp,%rdi
call   1040 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11a3 <vasprintf+0x73>
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <vsnprintf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    0x2e25(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
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 [256 x i8]
%9 = alloca i32
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @vsnprintf(i8* %10, i64 256, i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 256
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%18 = call i8* @strdup(i8* %17)
%19 = load i8**, i8*** %5
store i8* %18, i8** %19
%20 = load i32, i32* %9
store i32 %20, i32* %4
br label %42
21: ; preds = %3
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = add i64 %23, 1
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
%27 = load i8**, i8*** %5
store i8* %26, i8** %27
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = load i32, i32* @ENOMEM
store i32 %32, i32* @errno
store i32 -1, i32* %4
br label %42
33: ; preds = %21
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = add i64 %37, 1
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = call i32 @vsnprintf(i8* %35, i64 %38, i8* %39, i32 %40)
store i32 %41, i32* %4
br label %42
42: ; preds = %33, %31, %16
%43 = load i32, i32* %4
ret i32 %43
}

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