#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 __mingw_vsnprintf(char *, int, char const *, int /*<<< orphan*/);
char *malloc(int);
int __mingw_vasprintf(char **__restrict__ ret, const char *__restrict__ format,
                      va_list ap) {
  int len;
  len = __mingw_vsnprintf(NULL, 0, format, ap);
  if (len < 0)
    return -1;
  *ret = malloc(len + 1);
  if (!*ret)
    return -1;
  __mingw_vsnprintf(*ret, len + 1, format, ap);
  (*ret)[len] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<__mingw_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1030 <__mingw_vsnprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    115e <__mingw_vasprintf+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <__mingw_vasprintf+0x9b>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118d <__mingw_vasprintf+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <__mingw_vasprintf+0x9b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <__mingw_vsnprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mingw_vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
xor    %edi,%edi
xor    %esi,%esi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <__mingw_vsnprintf@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
js     1183 <__mingw_vasprintf+0x63>
mov    %eax,%ebp
lea    0x1(%rbp),%r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
mov    %rbx,%rcx
mov    %rax,(%rbx)
test   %rax,%rax
je     1183 <__mingw_vasprintf+0x63>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <__mingw_vsnprintf@plt>
mov    %ebp,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %ebp,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @__mingw_vasprintf(i8** noalias nocapture %0, i8* noalias %1, i32 %2) {
%4 = tail call i32 @__mingw_vsnprintf(i8* null, i32 0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = add nuw nsw i32 %4, 1
%8 = tail call i8* @malloc(i32 %7)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @__mingw_vsnprintf(i8* %8, i32 %7, i8* %1, i32 %2)
%12 = zext i32 %4 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %6, %3, %10
%15 = phi i32 [ %4, %10 ], [ -1, %3 ], [ -1, %6 ]
ret i32 %15
}