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


x86 assembly (clang, -O0):
<nlopt_vsprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x80,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118a <nlopt_vsprintf+0x4a>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    118f <nlopt_vsprintf+0x4f>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jl     11c4 <nlopt_vsprintf+0x84>
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
setae  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11d4 <nlopt_vsprintf+0x94>
jmp    122c <nlopt_vsprintf+0xec>
cmpl   $0x0,-0x24(%rbp)
jl     11ef <nlopt_vsprintf+0xaf>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
jmp    11fc <nlopt_vsprintf+0xbc>
imul   $0x3,-0x20(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1227 <nlopt_vsprintf+0xe7>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    118f <nlopt_vsprintf+0x4f>
mov    -0x8(%rbp),%rax
add    $0x30,%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,%rbx
mov    %rsi,%rdi
call   1050 <strlen@plt>
sub    $0xffffff80,%eax
movslq %eax,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     11d8 <nlopt_vsprintf+0x98>
mov    %rax,%rbx
jmp    11ad <nlopt_vsprintf+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rax),%ecx
lea    0x0(,%rbp,2),%rdx
add    %rbp,%rdx
shr    %rdx
test   %eax,%eax
movslq %ecx,%rbp
cmovs  %rdx,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d8 <nlopt_vsprintf+0x98>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
test   %eax,%eax
js     1180 <nlopt_vsprintf+0x40>
movslq %eax,%rcx
cmp    %rcx,%rbp
jbe    1180 <nlopt_vsprintf+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
define i8* @nlopt_vsprintf(i8* %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 i64 @realloc(i8* %0, i64 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 (...) @abort()
unreachable
11: ; preds = %3, %20
%12 = phi i64 [ %26, %20 ], [ %6, %3 ]
%13 = phi i64 [ %27, %20 ], [ %7, %3 ]
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @vsnprintf(i8* %14, i64 %12, i8* %1, i32 %2)
%16 = icmp slt i32 %15, 0
%17 = sext i32 %15 to i64
%18 = icmp ule i64 %12, %17
%19 = or i1 %16, %18
br i1 %19, label %20, label %31
20: ; preds = %11
%21 = icmp sgt i32 %15, -1
%22 = add nsw i32 %15, 1
%23 = sext i32 %22 to i64
%24 = mul i64 %12, 3
%25 = lshr i64 %24, 1
%26 = select i1 %21, i64 %23, i64 %25
%27 = tail call i64 @realloc(i8* %14, i64 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %11
29: ; preds = %20
%30 = tail call i32 (...) @abort()
unreachable
31: ; preds = %11
%32 = inttoptr i64 %13 to i8*
ret i8* %32
}