#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 is_asciiz(char *, int, int);
int /*<<< orphan*/ vsnprintf(char *, int, char const *, int /*<<< orphan*/);
char *xmalloc(int);
char *xrealloc(char *, int);
char *vstrbuild(const char *fmt, va_list ap) {
  char *out;
  void *p;
  int size, zeroindex;
  int last;
  last = 0;
  if (fmt != NULL) {
    if (!*fmt) {
      out = xmalloc(1);
      out[0] = '\0';
      return out;
    }
    size = vsnprintf(NULL, 0, fmt, ap) + 1;
    out = xmalloc(size);
    vsnprintf(out, size, fmt, ap);
    for (;;) {
      if ((zeroindex = is_asciiz(out, last, size)) != 0)
        break;
      last = size;
      size += 16;
      out = xrealloc(out, size);
      vsnprintf(out, size, fmt, ap);
    }
  } else
    out = NULL;
  return out;
}


x86 assembly (clang, -O0):
<vstrbuild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1223 <vstrbuild+0xe3>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1190 <vstrbuild+0x50>
mov    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1233 <vstrbuild+0xf3>
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1030 <vsnprintf@plt>
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1060 <is_asciiz@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     11e7 <vstrbuild+0xa7>
jmp    121e <vstrbuild+0xde>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <xrealloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <vsnprintf@plt>
jmp    11c7 <vstrbuild+0x87>
jmp    122b <vstrbuild+0xeb>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vstrbuild>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11e7 <vstrbuild+0xa7>
mov    %rdi,%r15
cmpb   $0x0,(%rdi)
je     11eb <vstrbuild+0xab>
mov    %esi,%r14d
xor    %edi,%edi
xor    %esi,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%r12d
mov    %r12d,%edi
call   1050 <xmalloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <is_asciiz@plt>
test   %eax,%eax
jne    11fb <vstrbuild+0xbb>
add    $0x11,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x10(%rbx),%r12d
mov    %rbp,%rdi
mov    %ebx,%esi
call   1040 <xrealloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1060 <is_asciiz@plt>
add    $0x10,%ebx
test   %eax,%eax
je     11b0 <vstrbuild+0x70>
jmp    11fb <vstrbuild+0xbb>
xor    %ebp,%ebp
jmp    11fb <vstrbuild+0xbb>
mov    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @vstrbuild(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %10
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %54
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = call i8* @xmalloc(i32 1)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 0, i8* %20
%21 = load i8*, i8** %6
store i8* %21, i8** %3
br label %57
22: ; preds = %13
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i32 @vsnprintf(i8* null, i32 0, i8* %23, i32 %24)
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = call i8* @xmalloc(i32 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = load i32, i32* %8
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = call i32 @vsnprintf(i8* %29, i32 %30, i8* %31, i32 %32)
br label %34
34: ; preds = %41, %22
%35 = load i8*, i8** %6
%36 = load i32, i32* %10
%37 = load i32, i32* %8
%38 = call i32 @is_asciiz(i8* %35, i32 %36, i32 %37)
store i32 %38, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
br label %53
41: ; preds = %34
%42 = load i32, i32* %8
store i32 %42, i32* %10
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 16
store i32 %44, i32* %8
%45 = load i8*, i8** %6
%46 = load i32, i32* %8
%47 = call i8* @xrealloc(i8* %45, i32 %46)
store i8* %47, i8** %6
%48 = load i8*, i8** %6
%49 = load i32, i32* %8
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = call i32 @vsnprintf(i8* %48, i32 %49, i8* %50, i32 %51)
br label %34
53: ; preds = %40
br label %55
54: ; preds = %2
store i8* null, i8** %6
br label %55
55: ; preds = %54, %53
%56 = load i8*, i8** %6
store i8* %56, i8** %3
br label %57
57: ; preds = %55, %17
%58 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
define i8* @vstrbuild(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %24, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i8* @xmalloc(i32 1)
store i8 0, i8* %8
br label %24
9: ; preds = %4
%10 = tail call i32 @vsnprintf(i8* null, i32 0, i8* %0, i32 %1)
%11 = add nsw i32 %10, 1
%12 = tail call i8* @xmalloc(i32 %11)
%13 = tail call i32 @vsnprintf(i8* %12, i32 %11, i8* %0, i32 %1)
%14 = tail call i32 @is_asciiz(i8* %12, i32 0, i32 %11)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %9, %16
%17 = phi i32 [ %19, %16 ], [ %11, %9 ]
%18 = phi i8* [ %20, %16 ], [ %12, %9 ]
%19 = add nsw i32 %17, 16
%20 = tail call i8* @xrealloc(i8* %18, i32 %19)
%21 = tail call i32 @vsnprintf(i8* %20, i32 %19, i8* %0, i32 %1)
%22 = tail call i32 @is_asciiz(i8* %20, i32 %17, i32 %19)
%23 = icmp eq i32 %22, 0
br i1 %23, label %16, label %24
24: ; preds = %16, %9, %2, %7
%25 = phi i8* [ %8, %7 ], [ null, %2 ], [ %12, %9 ], [ %20, %16 ]
ret i8* %25
}