#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*/ close(int);
int dup(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int vfprintf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int vdprintf(int fd, const char *__restrict fmt, va_list va) {
  int e;
  FILE *fp;
  if ((e = dup(fd)) == -1)
    return -1;
  if ((fp = fdopen(e, "a")) == NULL) {
    close(e);
    return -1;
  }
  e = vfprintf(fp, fmt, va);
  fclose(fp);
  return e;
}


x86 assembly (clang, -O0):
<vdprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1182 <vdprintf+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <vdprintf+0x85>
mov    -0x18(%rbp),%edi
lea    0xe74(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b3 <vdprintf+0x63>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <vdprintf+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <vfprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vdprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
call   1040 <dup@plt>
cmp    $0xffffffff,%eax
je     11a2 <vdprintf+0x52>
mov    %eax,%ebp
lea    0xe90(%rip),%rsi
mov    %eax,%edi
call   1060 <fdopen@plt>
test   %rax,%rax
je     119b <vdprintf+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <vfprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    11a7 <vdprintf+0x57>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
define i32 @vdprintf(i32 %0, i8* noalias %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i32 @dup(i32 %10)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %29
14: ; preds = %3
%15 = load i32, i32* %8
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %4
br label %29
21: ; preds = %14
%22 = load i32*, i32** %9
%23 = load i8*, i8** %6
%24 = load i32, i32* %7
%25 = call i32 @vfprintf(i32* %22, i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32*, i32** %9
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %29
29: ; preds = %21, %18, %13
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
define i32 @vdprintf(i32 %0, i8* noalias nocapture %1, i32 %2) {
%4 = tail call i32 @dup(i32 %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @close(i32 %4)
br label %14
11: ; preds = %6
%12 = tail call i32 @vfprintf(i32* %7, i8* %1, i32 %2)
%13 = tail call i32 @fclose(i32* %7)
br label %14
14: ; preds = %3, %11, %9
%15 = phi i32 [ -1, %9 ], [ %12, %11 ], [ -1, %3 ]
ret i32 %15
}