#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 *malloc(size_t);
int /*<<< orphan*/ mymemcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
int rpl_vasprintf(char **ret, const char *format, va_list ap) {
  size_t size;
  int len;
  va_list aq;
  (void)mymemcpy(&aq, &ap, sizeof(va_list));
  len = vsnprintf(NULL, 0, format, aq);
  ;
  if (len < 0 || (*ret = malloc(size = len + 1)) == NULL)
    return -1;
  return vsnprintf(*ret, size, format, ap);
}


x86 assembly (clang, -O0):
<rpl_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <mymemcpy@plt>
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1030 <vsnprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1198 <rpl_vasprintf+0x68>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
mov    %rdi,-0x28(%rbp)
call   1040 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11a4 <rpl_vasprintf+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <rpl_vasprintf+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpl_vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %edx,(%rsp)
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1050 <mymemcpy@plt>
mov    0x4(%rsp),%ecx
xor    %edi,%edi
xor    %esi,%esi
mov    %r15,%rdx
call   1030 <vsnprintf@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1193 <rpl_vasprintf+0x63>
mov    %eax,%ebx
add    $0x1,%ebx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1193 <rpl_vasprintf+0x63>
mov    (%rsp),%ecx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <vsnprintf@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @rpl_vasprintf(i8** nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
store i32 %2, i32* %4
%6 = bitcast i32* %5 to i8*
%7 = call i32 @mymemcpy(i32* %5, i32* %4, i32 4)
%8 = load i32, i32* %5
%9 = call i32 @vsnprintf(i8* null, i64 0, i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %3
%12 = add nuw nsw i32 %9, 1
%13 = zext i32 %12 to i64
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %0
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* %4
%18 = call i32 @vsnprintf(i8* %14, i64 %13, i8* %1, i32 %17)
br label %19
19: ; preds = %3, %11, %16
%20 = phi i32 [ %18, %16 ], [ -1, %11 ], [ -1, %3 ]
ret i32 %20
}