#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
size_t stub1(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ vasprintf(char **, char const *, int /*<<< orphan*/);
int vfprintf(FILE *stream, const char *format, va_list ap) {
  char *buffer = NULL;
  size_t written = -1;
  if (vasprintf(&buffer, format, ap) > 0) {
    written = (*fwrite)(buffer, 1, strlen(buffer), stream);
    free(buffer);
  }
  return written;
}


x86 assembly (clang, -O0):
<vfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1040 <vasprintf@plt>
cmp    $0x0,%rax
jle    11af <vfprintf+0x6f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vfprintf>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   1040 <vasprintf@plt>
test   %rax,%rax
jle    1188 <vfprintf+0x48>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    118d <vfprintf+0x4d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @vfprintf(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %7
store i64 -1, i64* %8
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i64 @vasprintf(i8** %7, i8* %9, i32 %10)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
%17 = load i32*, i32** %4
%18 = call i64 @fwrite(i8* %14, i32 1, i32 %16, i32* %17)
store i64 %18, i64* %8
%19 = load i8*, i8** %7
%20 = call i32 @free(i8* %19)
br label %21
21: ; preds = %13, %3
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
ret i32 %23
}

LLVM IR (-O2):
define i32 @vfprintf(i32* nocapture %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = call i64 @vasprintf(i8** %4, i8* %1, i32 %2)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = call i64 @fwrite(i8* %9, i32 1, i32 %10, i32* %0)
%12 = load i8*, i8** %4
%13 = call i32 @free(i8* %12)
%14 = trunc i64 %11 to i32
br label %15
15: ; preds = %8, %3
%16 = phi i32 [ %14, %8 ], [ -1, %3 ]
ret i32 %16
}