#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 /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
void redirect(char **fname, int inred, int outred, int append) {
  int newdescriptor;
  if (inred == 1) {
    newdescriptor = open(fname[0], O_RDONLY, 0600);
    if (dup2(newdescriptor, STDIN_FILENO) == -1) {
      printf("ERROR while redirecting the input\n");
      exit(110);
    }
    close(newdescriptor);
  }
  if (outred == 1) {
    newdescriptor = open(fname[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
    if (dup2(newdescriptor, STDOUT_FILENO) == -1) {
      printf("ERROR while redirecting the output\n");
      exit(111);
    }
    close(newdescriptor);
  }
  if (append == 1) {
    newdescriptor = open(fname[2], O_WRONLY | O_APPEND);
    if (dup2(newdescriptor, STDOUT_FILENO) == -1) {
      printf("ERROR while redirecting the output(append)\n");
      exit(112);
    }
    close(newdescriptor);
  }
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    11c6 <redirect+0x76>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11be <redirect+0x6e>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    $0x6e,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x1,-0x10(%rbp)
jne    123a <redirect+0xea>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
mov    0x2dff(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1232 <redirect+0xe2>
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
mov    $0x6f,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x1,-0x14(%rbp)
jne    12a0 <redirect+0x150>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d4c(%rip),%rax
or     (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1298 <redirect+0x148>
lea    0xdbe(%rip),%rdi
call   1040 <printf@plt>
mov    $0x70,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rdi,%r15
cmp    $0x1,%esi
jne    119e <redirect+0x4e>
mov    (%r15),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1242 <redirect+0xf2>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0x1,%ebp
jne    11ec <redirect+0x9c>
mov    0x2e06(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r15),%rdi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1258 <redirect+0x108>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0x1,%r14d
jne    1237 <redirect+0xe7>
mov    0x10(%r15),%rdi
mov    0x2db3(%rip),%rax
mov    0x2da4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     126e <redirect+0x11e>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1030 <puts@plt>
mov    $0x6e,%edi
call   1070 <exit@plt>
lea    0xdcc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x6f,%edi
call   1070 <exit@plt>
lea    0xd8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x70,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@.str = [35 x i8] c"ERROR while redirecting the input\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [36 x i8] c"ERROR while redirecting the output\0A\00"
@O_APPEND = global i32 0
@.str.2 = [44 x i8] c"ERROR while redirecting the output(append)\0A\00"
define void @redirect(i8** %0, i32 %1, i32 %2, i32 %3) {
%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
store i32 %3, i32* %8
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %28
12: ; preds = %4
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %16, i32 384)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = load i32, i32* @STDIN_FILENO
%20 = call i32 @dup2(i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 110)
unreachable
25: ; preds = %12
%26 = load i32, i32* %9
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %4
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %51
31: ; preds = %28
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 (i8*, i32, ...) @open(i8* %34, i32 %39, i32 384)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %31
%46 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 111)
unreachable
48: ; preds = %31
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %28
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 1
br i1 %53, label %54, label %72
54: ; preds = %51
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_WRONLY
%59 = load i32, i32* @O_APPEND
%60 = or i32 %58, %59
%61 = call i32 (i8*, i32, ...) @open(i8* %57, i32 %60)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = load i32, i32* @STDOUT_FILENO
%64 = call i32 @dup2(i32 %62, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %54
%67 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @exit(i32 112)
unreachable
69: ; preds = %54
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %69, %51
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
@O_APPEND = global i32 0
@str = [43 x i8] c"ERROR while redirecting the output(append)\00"
@str.3 = [35 x i8] c"ERROR while redirecting the output\00"
@str.4 = [34 x i8] c"ERROR while redirecting the input\00"
define void @redirect(i8** nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %1, 1
br i1 %5, label %6, label %18
6: ; preds = %4
%7 = load i8*, i8** %0
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 (i8*, i32, ...) @open(i8* %7, i32 %8, i32 384)
%10 = load i32, i32* @STDIN_FILENO
%11 = tail call i32 @dup2(i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 110)
unreachable
16: ; preds = %6
%17 = tail call i32 @close(i32 %9)
br label %18
18: ; preds = %16, %4
%19 = icmp eq i32 %2, 1
br i1 %19, label %20, label %37
20: ; preds = %18
%21 = getelementptr inbounds i8*, i8** %0, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 384)
%29 = load i32, i32* @STDOUT_FILENO
%30 = tail call i32 @dup2(i32 %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 111)
unreachable
35: ; preds = %20
%36 = tail call i32 @close(i32 %28)
br label %37
37: ; preds = %35, %18
%38 = icmp eq i32 %3, 1
br i1 %38, label %39, label %54
39: ; preds = %37
%40 = getelementptr inbounds i8*, i8** %0, i64 2
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_WRONLY
%43 = load i32, i32* @O_APPEND
%44 = or i32 %43, %42
%45 = tail call i32 (i8*, i32, ...) @open(i8* %41, i32 %44)
%46 = load i32, i32* @STDOUT_FILENO
%47 = tail call i32 @dup2(i32 %45, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%51 = tail call i32 @exit(i32 112)
unreachable
52: ; preds = %39
%53 = tail call i32 @close(i32 %45)
br label %54
54: ; preds = %52, %37
ret void
}