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

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int open(char const *, int, int);
int redirect_io(const char *filename, FILE *fh, mode_t mask) {
  if (mask == 0)
    mask = 0600;
  fflush(fh);
  int fp = open(filename, O_RDWR | O_CREAT | O_APPEND, mask);
  if (fp == -1)
    return -1;
  int save_out = dup(fileno(fh));
  if (dup2(fp, fileno(fh)) == -1)
    return -2;
  close(fp);
  return save_out;
}


x86 assembly (clang, -O0):
<redirect_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1184 <redirect_io+0x24>
movl   $0x180,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11cd <redirect_io+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <redirect_io+0xbc>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <dup@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    120e <redirect_io+0xae>
movl   $0xfffffffe,-0x4(%rbp)
jmp    121c <redirect_io+0xbc>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_io>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
test   %edx,%edx
mov    $0x180,%eax
cmove  %eax,%ebp
mov    %rsi,%rdi
call   1070 <fflush@plt>
mov    0x2e5b(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    %ebp,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11db <redirect_io+0x7b>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <dup@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11e3 <redirect_io+0x83>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11e9 <redirect_io+0x89>
mov    $0xffffffff,%r14d
jmp    11e9 <redirect_io+0x89>
mov    $0xfffffffe,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define i32 @redirect_io(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 384, i32* %7
br label %13
13: ; preds = %12, %3
%14 = load i32*, i32** %6
%15 = call i32 @fflush(i32* %14)
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @O_APPEND
%21 = or i32 %19, %20
%22 = load i32, i32* %7
%23 = call i32 @open(i8* %16, i32 %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %13
store i32 -1, i32* %4
br label %41
27: ; preds = %13
%28 = load i32*, i32** %6
%29 = call i32 @fileno(i32* %28)
%30 = call i32 @dup(i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %8
%32 = load i32*, i32** %6
%33 = call i32 @fileno(i32* %32)
%34 = call i32 @dup2(i32 %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %27
store i32 -2, i32* %4
br label %41
37: ; preds = %27
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %9
store i32 %40, i32* %4
br label %41
41: ; preds = %37, %36, %26
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
define i32 @redirect_io(i8* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = select i1 %4, i32 384, i32 %2
%6 = tail call i32 @fflush(i32* %1)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %11, i32 %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %22, label %14
14: ; preds = %3
%15 = tail call i32 @fileno(i32* %1)
%16 = tail call i32 @dup(i32 %15)
%17 = tail call i32 @fileno(i32* %1)
%18 = tail call i32 @dup2(i32 %12, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @close(i32 %12)
br label %22
22: ; preds = %20, %14, %3
%23 = phi i32 [ -1, %3 ], [ %16, %20 ], [ -2, %14 ]
ret i32 %23
}