#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 */
char *realloc(char *, size_t);
int strlen(char const *);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
char *nlopt_vsprintf(char *p, const char *format, va_list ap) {
  size_t len = strlen(format) + 128;
  int ret;
  p = (char *)realloc(p, len);
  if (!p)
    return 0;
  while ((ret = vsnprintf(p, len, format, ap)) < 0 || (size_t)ret >= len) {
    len = ret >= 0 ? (size_t)(ret + 1) : (len * 3) >> 1;
    p = realloc(p, len);
    if (!p)
      return 0;
  }
  return p;
}


x86 assembly (clang, -O0):
<nlopt_vsprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x80,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1180 <nlopt_vsprintf+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1230 <nlopt_vsprintf+0x100>
jmp    1185 <nlopt_vsprintf+0x55>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x2d(%rbp)
jl     11ba <nlopt_vsprintf+0x8a>
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
setae  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11ca <nlopt_vsprintf+0x9a>
jmp    1228 <nlopt_vsprintf+0xf8>
cmpl   $0x0,-0x2c(%rbp)
jl     11e5 <nlopt_vsprintf+0xb5>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
jmp    11f2 <nlopt_vsprintf+0xc2>
imul   $0x3,-0x28(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1223 <nlopt_vsprintf+0xf3>
movq   $0x0,-0x8(%rbp)
jmp    1230 <nlopt_vsprintf+0x100>
jmp    1185 <nlopt_vsprintf+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlopt_vsprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1040 <strlen@plt>
sub    $0xffffff80,%eax
movslq %eax,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     11b5 <nlopt_vsprintf+0x85>
mov    %rax,%rbp
jmp    1196 <nlopt_vsprintf+0x66>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rax),%ecx
lea    (%rbx,%rbx,2),%rdx
shr    %rdx
test   %eax,%eax
movslq %ecx,%rbx
cmovs  %rdx,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11b5 <nlopt_vsprintf+0x85>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
test   %eax,%eax
js     1170 <nlopt_vsprintf+0x40>
movslq %eax,%rcx
cmp    %rcx,%rbx
jbe    1170 <nlopt_vsprintf+0x40>
jmp    11b7 <nlopt_vsprintf+0x87>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @nlopt_vsprintf(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 128
%6 = sext i32 %5 to i64
%7 = tail call i8* @realloc(i8* %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %26, label %9
9: ; preds = %3, %17
%10 = phi i8* [ %24, %17 ], [ %7, %3 ]
%11 = phi i64 [ %23, %17 ], [ %6, %3 ]
%12 = tail call i32 @vsnprintf(i8* %10, i64 %11, i8* %1, i32 %2)
%13 = icmp slt i32 %12, 0
%14 = sext i32 %12 to i64
%15 = icmp ule i64 %11, %14
%16 = or i1 %13, %15
br i1 %16, label %17, label %26
17: ; preds = %9
%18 = icmp sgt i32 %12, -1
%19 = add nsw i32 %12, 1
%20 = sext i32 %19 to i64
%21 = mul i64 %11, 3
%22 = lshr i64 %21, 1
%23 = select i1 %18, i64 %20, i64 %22
%24 = tail call i8* @realloc(i8* %10, i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %9
26: ; preds = %9, %17, %3
%27 = phi i8* [ null, %3 ], [ %10, %9 ], [ null, %17 ]
ret i8* %27
}