#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 printf_length(char const *, int /*<<< orphan*/);
char *ralloc_size(void const *, size_t);
int /*<<< orphan*/ vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) {
  size_t size = printf_length(fmt, args) + 1;
  char *ptr = ralloc_size(ctx, size);
  if (ptr != NULL)
    vsnprintf(ptr, size, fmt, args);
  return ptr;
}


x86 assembly (clang, -O0):
<ralloc_vasprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <printf_length@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <ralloc_size@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1188 <ralloc_vasprintf+0x58>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <vsnprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ralloc_vasprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    %edx,%esi
call   1050 <printf_length@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <ralloc_size@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1174 <ralloc_vasprintf+0x44>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @ralloc_vasprintf(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @printf_length(i8* %9, i32 %10)
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %7
%14 = load i8*, i8** %4
%15 = load i64, i64* %7
%16 = call i8* @ralloc_size(i8* %14, i64 %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i8*, i8** %8
%21 = load i64, i64* %7
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = call i32 @vsnprintf(i8* %20, i64 %21, i8* %22, i32 %23)
br label %25
25: ; preds = %19, %3
%26 = load i8*, i8** %8
ret i8* %26
}

LLVM IR (-O2):
define i8* @ralloc_vasprintf(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @printf_length(i8* %1, i32 %2)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @ralloc_size(i8* %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @vsnprintf(i8* %7, i64 %6, i8* %1, i32 %2)
br label %11
11: ; preds = %9, %3
ret i8* %7
}