#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;
typedef int /*<<< orphan*/ ap2;
typedef int /*<<< orphan*/ ap;

/* Variables and functions */
int /*<<< orphan*/ err_log(int /*<<< orphan*/, int, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
char *realloc(char *, int);
int vsnprintf(char *, int, char *, int /*<<< orphan*/);
char *util_vasprintf(char *fmt, va_list ap) {
  char *outbuf;
  char *newbuf;
  va_list ap2;
  int size = 200;
  int new_size;
  outbuf = (char *)malloc(size);
  if (!outbuf)
    err_log(0, 0x80000000, "Could not allocate buffer in vasprintf\n");
  memcpy((&ap2), (&ap), sizeof(ap));
  while (1) {
    new_size = vsnprintf(outbuf, size, fmt, ap);
    if (new_size > -1 && new_size < size)
      break;
    if (new_size > -1)
      size = new_size + 1;
    else
      size *= 2;
    if ((newbuf = realloc(outbuf, size)) == NULL) {
      free(outbuf);
      err_log(0, 0x80000000, "malloc error in vasprintf\n");
      exit(1);
    }
    outbuf = newbuf;
    memcpy((&ap), (&ap2), sizeof(ap2));
  }
  return outbuf;
}


x86 assembly (clang, -O0):
<util_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xc8,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <util_vasprintf+0x40>
xor    %edi,%edi
mov    $0x80000000,%esi
lea    0xe55(%rip),%rdx
call   1050 <err_log@plt>
lea    -0x24(%rbp),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jle    11f3 <util_vasprintf+0x83>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11f3 <util_vasprintf+0x83>
jmp    1273 <util_vasprintf+0x103>
cmpl   $0xffffffff,-0x2c(%rbp)
jle    120b <util_vasprintf+0x9b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1214 <util_vasprintf+0xa4>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1254 <util_vasprintf+0xe4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
mov    $0x80000000,%esi
lea    0xde3(%rip),%rdx
call   1050 <err_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    11c2 <util_vasprintf+0x52>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %esi,(%rsp)
mov    $0xc8,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11a6 <util_vasprintf+0x36>
lea    0xe66(%rip),%rdx
xor    %edi,%edi
mov    $0x80000000,%esi
call   1050 <err_log@plt>
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    (%rsp),%ecx
mov    $0xc8,%ebp
mov    %r13,%rdi
mov    $0xc8,%esi
mov    %r14,%rdx
call   1040 <vsnprintf@plt>
cmp    $0xc8,%eax
jb     123e <util_vasprintf+0xce>
test   %eax,%eax
setns  %cl
mov    %rsp,%r15
lea    0x4(%rsp),%r12
mov    %r13,%rbx
nopw   0x0(%rax,%rax,1)
add    $0x1,%eax
add    %ebp,%ebp
test   $0x1,%cl
cmovne %eax,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1250 <util_vasprintf+0xe0>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    (%rsp),%ecx
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <vsnprintf@plt>
mov    %r13,%rbx
test   %eax,%eax
setns  %cl
js     11f0 <util_vasprintf+0x80>
mov    %r13,%rbx
cmp    %ebp,%eax
jge    11f0 <util_vasprintf+0x80>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdc9(%rip),%rdx
xor    %edi,%edi
mov    $0x80000000,%esi
call   1050 <err_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"Could not allocate buffer in vasprintf\0A\00"
@.str.1 = [27 x i8] c"malloc error in vasprintf\0A\00"
define i8* @util_vasprintf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 200, i32* %8
%10 = load i32, i32* %8
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @err_log(i32 0, i32 -2147483648, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = call i32 @memcpy(i32* %7, i32* %4, i32 4)
br label %19
19: ; preds = %17, %51
%20 = load i8*, i8** %5
%21 = load i32, i32* %8
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = call i32 @vsnprintf(i8* %20, i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %54
32: ; preds = %27, %19
%33 = load i32, i32* %9
%34 = icmp sgt i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %41
38: ; preds = %32
%39 = load i32, i32* %8
%40 = mul nsw i32 %39, 2
store i32 %40, i32* %8
br label %41
41: ; preds = %38, %35
%42 = load i8*, i8** %5
%43 = load i32, i32* %8
%44 = call i8* @realloc(i8* %42, i32 %43)
store i8* %44, i8** %6
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
%49 = call i32 @err_log(i32 0, i32 -2147483648, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %41
%52 = load i8*, i8** %6
store i8* %52, i8** %5
%53 = call i32 @memcpy(i32* %4, i32* %7, i32 4)
br label %19
54: ; preds = %31
%55 = load i8*, i8** %5
ret i8* %55
}

LLVM IR (-O2):
@.str = [40 x i8] c"Could not allocate buffer in vasprintf\0A\00"
@.str.1 = [27 x i8] c"malloc error in vasprintf\0A\00"
define i8* @util_vasprintf(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %1, i32* %3
%5 = bitcast i32* %4 to i8*
%6 = tail call i64 @malloc(i32 200)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @err_log(i32 0, i32 -2147483648, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @memcpy(i32* %4, i32* %3, i32 4)
%13 = load i32, i32* %3
%14 = call i32 @vsnprintf(i8* %7, i32 200, i8* %0, i32 %13)
%15 = icmp ult i32 %14, 200
br i1 %15, label %39, label %16
16: ; preds = %11
%17 = icmp sgt i32 %14, -1
br label %18
18: ; preds = %16, %32
%19 = phi i1 [ %36, %32 ], [ %17, %16 ]
%20 = phi i32 [ %35, %32 ], [ %14, %16 ]
%21 = phi i8* [ %26, %32 ], [ %7, %16 ]
%22 = phi i32 [ %25, %32 ], [ 200, %16 ]
%23 = add nsw i32 %20, 1
%24 = shl nsw i32 %22, 1
%25 = select i1 %19, i32 %23, i32 %24
%26 = call i8* @realloc(i8* %21, i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = call i32 @free(i8* %21)
%30 = call i32 @err_log(i32 0, i32 -2147483648, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = call i32 @memcpy(i32* %3, i32* %4, i32 4)
%34 = load i32, i32* %3
%35 = call i32 @vsnprintf(i8* %26, i32 %25, i8* %0, i32 %34)
%36 = icmp sgt i32 %35, -1
%37 = icmp slt i32 %35, %25
%38 = and i1 %36, %37
br i1 %38, label %39, label %18
39: ; preds = %32, %11
%40 = phi i8* [ %7, %11 ], [ %26, %32 ]
ret i8* %40
}