#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 == -1) {
        {
          free(*strp);
          *strp = 0;
        };
      }
    }
  } 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     11ef <vasprintf+0xaf>
mov    -0x1c(%rbp),%eax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%eax
jge    11ef <vasprintf+0xaf>
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     11ea <vasprintf+0xaa>
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   $0xffffffff,-0x18(%rbp)
jne    11e5 <vasprintf+0xa5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
jmp    11ea <vasprintf+0xaa>
jmp    11fa <vasprintf+0xba>
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   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
call   1060 <_vscprintf@plt>
test   %eax,%eax
js     119a <vasprintf+0x5a>
mov    %eax,%ebp
mov    0x2e81(%rip),%rax
cmp    (%rax),%ebp
jge    119a <vasprintf+0x5a>
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11a1 <vasprintf+0x61>
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
cmp    $0xffffffff,%eax
jne    11a6 <vasprintf+0x66>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
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 %44
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 %44
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 %43
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 eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %27
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
%40 = call i32 @free(i8* %39)
%41 = load i8**, i8*** %4
store i8* null, i8** %41
br label %42
42: ; preds = %37, %27
br label %43
43: ; preds = %42, %18
br label %46
44: ; preds = %14, %3
%45 = load i8**, i8*** %4
store i8* null, i8** %45
br label %46
46: ; preds = %44, %43
%47 = load i32, i32* %7
ret i32 %47
}

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 %20
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 %21, label %14
14: ; preds = %9
%15 = tail call i32 @vsnprintf(i8* %12, i32 %10, i8* %1, i32 %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %0
%19 = tail call i32 @free(i8* %18)
br label %20
20: ; preds = %3, %17
store i8* null, i8** %0
br label %21
21: ; preds = %20, %9, %14
%22 = phi i32 [ %15, %14 ], [ -1, %9 ], [ -1, %20 ]
ret i32 %22
}