#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;
int _vscprintf(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vasprintf(char **strp, const char *fmt, va_list ap) {
  int r = -1, size = _vscprintf(fmt, ap);
  if ((size >= 0) && (size < INT_MAX)) {
    *strp = (char *)malloc(size + 1);
    if (*strp) {
      r = vsnprintf(*strp, size + 1, fmt, ap);
      if ((r < 0) || (r > size)) {
        {
          free(*strp);
          *strp = 0;
        };
        r = -1;
      }
    }
  } else {
    *strp = 0;
  }
  return (r);
}


x86 assembly (clang, -O0):
<vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <_vscprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1202 <vasprintf+0xc2>
mov    -0x1c(%rbp),%eax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%eax
jge    1202 <vasprintf+0xc2>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11fd <vasprintf+0xbd>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11da <vasprintf+0x9a>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    11f8 <vasprintf+0xb8>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x18(%rbp)
jmp    11fd <vasprintf+0xbd>
jmp    120d <vasprintf+0xcd>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
call   1060 <_vscprintf@plt>
test   %eax,%eax
js     11a1 <vasprintf+0x61>
mov    %eax,%ebx
mov    0x2e80(%rip),%rax
cmp    (%rax),%ebx
jge    11a1 <vasprintf+0x61>
lea    0x1(%rbx),%r12d
mov    %r12d,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11a8 <vasprintf+0x68>
mov    %rax,%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
test   %eax,%eax
js     1199 <vasprintf+0x59>
cmp    %ebx,%eax
jle    11ad <vasprintf+0x6d>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
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 i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @_vscprintf(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %48
14: ; preds = %3
%15 = load i32, i32* %8
%16 = load i32, i32* @INT_MAX
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %48
18: ; preds = %14
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = load i8**, i8*** %4
store i8* %22, i8** %23
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %47
27: ; preds = %18
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = call i32 @vsnprintf(i8* %29, i32 %31, i8* %32, i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %27
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = icmp sgt i32 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37, %27
%42 = load i8**, i8*** %4
%43 = load i8*, i8** %42
%44 = call i32 @free(i8* %43)
%45 = load i8**, i8*** %4
store i8* null, i8** %45
store i32 -1, i32* %7
br label %46
46: ; preds = %41, %37
br label %47
47: ; preds = %46, %18
br label %50
48: ; preds = %14, %3
%49 = load i8**, i8*** %4
store i8* null, i8** %49
br label %50
50: ; preds = %48, %47
%51 = load i32, i32* %7
ret i32 %51
}

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