#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 *malloc(int);
int vsnprintf(int /*<<< orphan*/ *, int /*<<< orphan*/, char const *,
              int /*<<< orphan*/);
int vsprintf(char *, char const *, int /*<<< orphan*/);
int generic_printf(void (*send)(const char *), const char *fmt, va_list ap) {
  char *buf;
  int size;
  size = vsnprintf(NULL, 0, fmt, ap);
  buf = malloc(size + 1);
  size = vsprintf(buf, fmt, ap);
  send(buf);
  free(buf);
  return size;
}


x86 assembly (clang, -O0):
<generic_printf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1040 <vsnprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <vsprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rdi
call   *%rax
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generic_printf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <vsprintf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   *%r14
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @generic_printf(void (i8*)* %0, i8* %1, i32 %2) {
%4 = alloca void (i8*)*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store void (i8*)* %0, void (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 @vsnprintf(i32* null, i32 0, i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @vsprintf(i8* %15, i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load void (i8*)*, void (i8*)** %4
%20 = load i8*, i8** %7
%21 = load i8*, i8** %7
%22 = call i32 @free(i8* %21)
%23 = load i32, i32* %8
ret i32 %23
}

LLVM IR (-O2):
define i32 @generic_printf(void (i8*)* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @vsnprintf(i32* null, i32 0, i8* %1, i32 %2)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @vsprintf(i8* %6, i8* %1, i32 %2)
tail call void %0(i8* %6)
%8 = tail call i32 @free(i8* %6)
ret i32 %7
}