#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 *, size_t);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
char *strf(const char *fmt, va_list ap) {
  int nc;
  char *str = NULL;
  char *p;
  size_t size = 512;
  while (1) {
    p = realloc(str, size);
    if (!p) {
      free(str);
      str = NULL;
      break;
    }
    str = p;
    nc = vsnprintf(str, size, fmt, ap);
    if (nc < size)
      break;
    size *= 2;
  }
  return (str);
}


x86 assembly (clang, -O0):
<strf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x200,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1181 <strf+0x51>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    11c4 <strf+0x94>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11b3 <strf+0x83>
jmp    11c4 <strf+0x94>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    114f <strf+0x1f>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x200,%ebp
mov    $0x200,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a3 <strf+0x63>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
cltq
cmp    %rax,%rbp
ja     11ac <strf+0x6c>
add    %rbp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <realloc@plt>
mov    %rbx,%rdi
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <strf+0x30>
jmp    11a5 <strf+0x65>
xor    %edi,%edi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* null, i8** %6
store i64 512, i64* %8
br label %9
9: ; preds = %2, %30
%10 = load i8*, i8** %6
%11 = load i64, i64* %8
%12 = call i8* @realloc(i8* %10, i64 %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = load i8*, i8** %6
%17 = call i32 @free(i8* %16)
store i8* null, i8** %6
br label %33
18: ; preds = %9
%19 = load i8*, i8** %7
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i64, i64* %8
%22 = load i8*, i8** %3
%23 = load i32, i32* %4
%24 = call i32 @vsnprintf(i8* %20, i64 %21, i8* %22, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = load i64, i64* %8
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %18
br label %33
30: ; preds = %18
%31 = load i64, i64* %8
%32 = mul i64 %31, 2
store i64 %32, i64* %8
br label %9
33: ; preds = %29, %15
%34 = load i8*, i8** %6
ret i8* %34
}

LLVM IR (-O2):
define noalias i8* @strf(i8* nocapture %0, i32 %1) {
%3 = tail call dereferenceable_or_null(512) i8* @malloc(i64 512)
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %11
5: ; preds = %11
%6 = tail call i8* @realloc(i8* %12, i64 %17)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5, %2
%9 = phi i8* [ null, %2 ], [ %12, %5 ]
%10 = tail call i32 @free(i8* %9)
br label %18
11: ; preds = %2, %5
%12 = phi i8* [ %6, %5 ], [ %3, %2 ]
%13 = phi i64 [ %17, %5 ], [ 512, %2 ]
%14 = tail call i32 @vsnprintf(i8* %12, i64 %13, i8* %0, i32 %1)
%15 = sext i32 %14 to i64
%16 = icmp ugt i64 %13, %15
%17 = shl i64 %13, 1
br i1 %16, label %18, label %5
18: ; preds = %11, %8
%19 = phi i8* [ null, %8 ], [ %12, %11 ]
ret i8* %19
}