#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_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strip(char *);
void redirect(char *input, char *output) {
  strip(input);
  int input_file = open(input, O_RDONLY);
  if (input_file < 0) {
    perror("Error opening input file in child after fork! Exiting.");
    exit(1);
  } else {
    dup2(input_file, STDIN_FILENO);
    close(input_file);
  }
  strip(output);
  int output_file = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (output_file < 0) {
    perror("Error opening output file in child after fork! Exiting.");
    exit(1);
  } else {
    dup2(output_file, STDOUT_FILENO);
    close(output_file);
  }
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strip@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b0 <redirect+0x50>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strip@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1220 <redirect+0xc0>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1050 <strip@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11f3 <redirect+0x93>
mov    %eax,%ebp
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
call   1050 <strip@plt>
mov    0x2e03(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11fc <redirect+0x9c>
mov    %eax,%ebx
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
lea    0xe06(%rip),%rdi
jmp    1203 <redirect+0xa3>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [55 x i8] c"Error opening input file in child after fork! Exiting.\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [56 x i8] c"Error opening output file in child after fork! Exiting.\00"
@STDOUT_FILENO = global i32 0
define void @redirect(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i32 @strip(i8* %7)
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %5
%19 = load i32, i32* @STDIN_FILENO
%20 = call i32 @dup2(i32 %18, i32 %19)
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = call i32 @strip(i8* %24)
%26 = load i8*, i8** %4
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 420)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %23
%36 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %23
%39 = load i32, i32* %6
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @dup2(i32 %39, i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %38
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [55 x i8] c"Error opening input file in child after fork! Exiting.\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [56 x i8] c"Error opening output file in child after fork! Exiting.\00"
@STDOUT_FILENO = global i32 0
define void @redirect(i8* %0, i8* %1) {
%3 = tail call i32 @strip(i8* %0)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @dup2(i32 %5, i32 %11)
%13 = tail call i32 @close(i32 %5)
%14 = tail call i32 @strip(i8* %1)
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %19, i32 420)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = tail call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %10
%26 = load i32, i32* @STDOUT_FILENO
%27 = tail call i32 @dup2(i32 %20, i32 %26)
%28 = tail call i32 @close(i32 %20)
ret void
}