#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 /*<<< orphan*/ free(char *);
char *realloc(char *, int);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
char *gg_vsaprintf(const char *format, va_list ap) {
  int size = 0;
  const char *start;
  char *buf = NULL;
  start = format;
  {
    int res;
    char *tmp;
    size = 128;
    do {
      size *= 2;
      if (!(tmp = realloc(buf, size + 1))) {
        free(buf);
        return NULL;
      }
      buf = tmp;
      res = vsnprintf(buf, size, format, ap);
    } while (res == size - 1 || res == -1);
  }
  format = start;
  vsnprintf(buf, size + 1, format, ap);
  return buf;
}


x86 assembly (clang, -O0):
<gg_vsaprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x80,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1199 <gg_vsaprintf+0x69>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1208 <gg_vsaprintf+0xd8>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%al
cmp    %edx,%ecx
mov    %al,-0x39(%rbp)
je     11d7 <gg_vsaprintf+0xa7>
cmpl   $0xffffffff,-0x2c(%rbp)
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    115d <gg_vsaprintf+0x2d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gg_vsaprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x80,%ebx
xor    %r13d,%r13d
nopl   0x0(%rax)
lea    (%rbx,%rbx,1),%r12d
add    $0x1,%r12d
mov    %r13,%rdi
mov    %r12d,%esi
call   1050 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11a2 <gg_vsaprintf+0x72>
add    %ebx,%ebx
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
lea    -0x1(%rbx),%ecx
mov    %rbp,%r13
cmp    %ecx,%eax
je     1150 <gg_vsaprintf+0x20>
mov    %rbp,%r13
cmp    $0xffffffff,%eax
je     1150 <gg_vsaprintf+0x20>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
jmp    11aa <gg_vsaprintf+0x7a>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @gg_vsaprintf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i8* null, i8** %8
%11 = load i8*, i8** %4
store i8* %11, i8** %7
store i32 128, i32* %6
br label %12
12: ; preds = %38, %2
%13 = load i32, i32* %6
%14 = mul nsw i32 %13, 2
store i32 %14, i32* %6
%15 = load i8*, i8** %8
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
%18 = call i8* @realloc(i8* %15, i32 %17)
store i8* %18, i8** %10
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %12
%21 = load i8*, i8** %8
%22 = call i32 @free(i8* %21)
store i8* null, i8** %3
br label %49
23: ; preds = %12
%24 = load i8*, i8** %10
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = load i32, i32* %6
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = call i32 @vsnprintf(i8* %25, i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %9
br label %30
30: ; preds = %23
%31 = load i32, i32* %9
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 1
%34 = icmp eq i32 %31, %33
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br label %38
38: ; preds = %35, %30
%39 = phi i1 [ true, %30 ], [ %37, %35 ]
br i1 %39, label %12, label %40
40: ; preds = %38
%41 = load i8*, i8** %7
store i8* %41, i8** %4
%42 = load i8*, i8** %8
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
%45 = load i8*, i8** %4
%46 = load i32, i32* %5
%47 = call i32 @vsnprintf(i8* %42, i32 %44, i8* %45, i32 %46)
%48 = load i8*, i8** %8
store i8* %48, i8** %3
br label %49
49: ; preds = %40, %20
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
define i8* @gg_vsaprintf(i8* nocapture %0, i32 %1) {
br label %3
3: ; preds = %12, %2
%4 = phi i32 [ 128, %2 ], [ %6, %12 ]
%5 = phi i8* [ null, %2 ], [ %8, %12 ]
%6 = shl nsw i32 %4, 1
%7 = or i32 %6, 1
%8 = tail call i8* @realloc(i8* %5, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @free(i8* %5)
br label %20
12: ; preds = %3
%13 = tail call i32 @vsnprintf(i8* %8, i32 %6, i8* %0, i32 %1)
%14 = add nsw i32 %6, -1
%15 = icmp eq i32 %13, %14
%16 = icmp eq i32 %13, -1
%17 = or i1 %15, %16
br i1 %17, label %3, label %18
18: ; preds = %12
%19 = tail call i32 @vsnprintf(i8* %8, i32 %7, i8* %0, i32 %1)
br label %20
20: ; preds = %10, %18
ret i8* %8
}