#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 compute_format_length(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int do_vasprintf(char **dest, const char *fmt, va_list ap1, va_list ap2) {
  int length;
  char *buf;
  int expected_length;
  expected_length = compute_format_length(fmt, ap1);
  if (expected_length < 0) {
    return expected_length;
  }
  buf = malloc(sizeof(char) * (expected_length + 1));
  if (buf == NULL) {
    return -11;
  }
  length = vsprintf(buf, fmt, ap2);
  if (length < 0) {
    free(buf);
    return -12;
  }
  *dest = buf;
  return length;
}


x86 assembly (clang, -O0):
<do_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <compute_format_length@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    117a <do_vasprintf+0x3a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <do_vasprintf+0xab>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a8 <do_vasprintf+0x68>
movl   $0xfffffff5,-0x4(%rbp)
jmp    11eb <do_vasprintf+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <vsprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11da <do_vasprintf+0x9a>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movl   $0xfffffff4,-0x4(%rbp)
jmp    11eb <do_vasprintf+0xab>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <compute_format_length@plt>
test   %eax,%eax
js     119b <do_vasprintf+0x5b>
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1187 <do_vasprintf+0x47>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <vsprintf@plt>
test   %eax,%eax
js     118e <do_vasprintf+0x4e>
mov    %rbp,(%r14)
jmp    119b <do_vasprintf+0x5b>
mov    $0xfffffff5,%eax
jmp    119b <do_vasprintf+0x5b>
mov    %rbp,%rdi
call   1040 <free@plt>
mov    $0xfffffff4,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @do_vasprintf(i8** %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %7
%14 = load i32, i32* %8
%15 = call i32 @compute_format_length(i8* %13, i32 %14)
store i32 %15, i32* %12
%16 = load i32, i32* %12
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i32, i32* %12
store i32 %19, i32* %5
br label %44
20: ; preds = %4
%21 = load i32, i32* %12
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %20
store i32 -11, i32* %5
br label %44
30: ; preds = %20
%31 = load i8*, i8** %11
%32 = load i8*, i8** %7
%33 = load i32, i32* %9
%34 = call i32 @vsprintf(i8* %31, i8* %32, i32 %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i8*, i8** %11
%39 = call i32 @free(i8* %38)
store i32 -12, i32* %5
br label %44
40: ; preds = %30
%41 = load i8*, i8** %11
%42 = load i8**, i8*** %6
store i8* %41, i8** %42
%43 = load i32, i32* %10
store i32 %43, i32* %5
br label %44
44: ; preds = %40, %37, %29, %18
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @do_vasprintf(i8** nocapture writeonly %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @compute_format_length(i8* %1, i32 %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = add nuw nsw i32 %5, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = tail call i32 @vsprintf(i8* %9, i8* %1, i32 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @free(i8* %9)
br label %17
16: ; preds = %11
store i8* %9, i8** %0
br label %17
17: ; preds = %7, %4, %16, %14
%18 = phi i32 [ -12, %14 ], [ %12, %16 ], [ %5, %4 ], [ -11, %7 ]
ret i32 %18
}