#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 */

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int open(char const *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ redirect_stream(int, int /*<<< orphan*/);
void redirect_to_files(const char *input, const char *output,
                       int do_output_append) {
  if (input != NULL) {
    int fd = open(input, O_RDONLY);
    if (fd < 0)
      perror("Cannot open input file");
    else
      redirect_stream(fd, STDIN_FILENO);
  }
  if (output != NULL) {
    int fd = open(output,
                  O_WRONLY | O_CREAT | (do_output_append ? O_APPEND : O_TRUNC),
                  0666);
    if (fd < 0)
      perror("Cannot open output file");
    else
      redirect_stream(fd, STDOUT_FILENO);
  }
}


x86 assembly (clang, -O0):
<redirect_to_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1196 <redirect_to_files+0x66>
mov    -0x8(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1180 <redirect_to_files+0x50>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
jmp    1191 <redirect_to_files+0x61>
mov    -0x18(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1030 <redirect_stream@plt>
jmp    1196 <redirect_to_files+0x66>
cmpq   $0x0,-0x10(%rbp)
je     1231 <redirect_to_files+0x101>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d9 <redirect_to_files+0xa9>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11e5 <redirect_to_files+0xb5>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x2c(%rbp),%eax
or     %eax,%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121b <redirect_to_files+0xeb>
lea    0xe06(%rip),%rdi
call   1050 <perror@plt>
jmp    122c <redirect_to_files+0xfc>
mov    -0x1c(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1030 <redirect_stream@plt>
jmp    1231 <redirect_to_files+0x101>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_to_files>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
test   %rdi,%rdi
je     1161 <redirect_to_files+0x31>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
js     11b7 <redirect_to_files+0x87>
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <redirect_stream@plt>
test   %rbx,%rbx
je     11c8 <redirect_to_files+0x98>
mov    0x2e43(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
test   %ebp,%ebp
jne    1185 <redirect_to_files+0x55>
mov    0x2e6d(%rip),%rax
jmp    118c <redirect_to_files+0x5c>
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
js     11cf <redirect_to_files+0x9f>
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <redirect_stream@plt>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
test   %rbx,%rbx
jne    1166 <redirect_to_files+0x36>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [23 x i8] c"Cannot open input file\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [24 x i8] c"Cannot open output file\00"
@STDOUT_FILENO = global i32 0
define void @redirect_to_files(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %24
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %23
19: ; preds = %11
%20 = load i32, i32* %7
%21 = load i32, i32* @STDIN_FILENO
%22 = call i32 @redirect_stream(i32 %20, i32 %21)
br label %23
23: ; preds = %19, %17
br label %24
24: ; preds = %23, %3
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %51
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = load i32, i32* @O_APPEND
br label %38
36: ; preds = %27
%37 = load i32, i32* @O_TRUNC
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ %35, %34 ], [ %37, %36 ]
%40 = or i32 %31, %39
%41 = call i32 (i8*, i32, ...) @open(i8* %28, i32 %40, i32 438)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %50
46: ; preds = %38
%47 = load i32, i32* %8
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @redirect_stream(i32 %47, i32 %48)
br label %50
50: ; preds = %46, %44
br label %51
51: ; preds = %50, %24
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [23 x i8] c"Cannot open input file\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [24 x i8] c"Cannot open output file\00"
@STDOUT_FILENO = global i32 0
define void @redirect_to_files(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
11: ; preds = %5
%12 = load i32, i32* @STDIN_FILENO
%13 = tail call i32 @redirect_stream(i32 %7, i32 %12)
br label %14
14: ; preds = %9, %11, %3
%15 = icmp eq i8* %1, null
br i1 %15, label %32, label %16
16: ; preds = %14
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = icmp eq i32 %2, 0
%21 = load i32, i32* @O_APPEND
%22 = load i32, i32* @O_TRUNC
%23 = select i1 %20, i32 %22, i32 %21
%24 = or i32 %19, %23
%25 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %24, i32 438)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %32
29: ; preds = %16
%30 = load i32, i32* @STDOUT_FILENO
%31 = tail call i32 @redirect_stream(i32 %25, i32 %30)
br label %32
32: ; preds = %27, %29, %14
ret void
}