#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execute(char *);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strsep(char **, char *);
void redir(int type, char *s) {
  if (type == 1) {
    char *cmd = strsep(&s, "<");
    int status;
    char *input = malloc(256);
    int fdIn = open(s, O_RDONLY);
    read(fdIn, input, 256);
    strcpy(cmd, input);
    execute(cmd);
    close(fdIn);
  } else if (type == 2) {
    int status;
    char *cmd = strsep(&s, ">");
    int fdOut = open(s, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    int fd1 = dup(STDOUT_FILENO);
    dup2(fdOut, STDOUT_FILENO);
    execute(cmd);
    dup2(fd1, STDOUT_FILENO);
    close(fdOut);
  }
}


x86 assembly (clang, -O0):
<redir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    1216 <redir+0x86>
lea    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   10a0 <strsep@plt>
mov    %rax,-0x18(%rbp)
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1060 <execute@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
jmp    12ab <redir+0x11b>
cmpl   $0x2,-0x4(%rbp)
jne    12a6 <redir+0x116>
lea    -0x10(%rbp),%rdi
lea    0xdd7(%rip),%rsi
call   10a0 <strsep@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
or     (%rax),%esi
mov    0x2d9f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x38(%rbp),%rdi
call   1060 <execute@plt>
mov    -0x40(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
jmp    12ab <redir+0x11b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,(%rsp)
cmp    $0x2,%edi
je     1202 <redir+0x72>
cmp    $0x1,%edi
jne    1275 <redir+0xe5>
lea    0xe50(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <strsep@plt>
mov    %rax,%r14
mov    $0x100,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    (%rsp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10b0 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1060 <execute@plt>
jmp    126e <redir+0xde>
lea    0xdf9(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <strsep@plt>
mov    %rax,%r14
mov    0x2ddd(%rip),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rsp),%rdi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10b0 <open@plt>
mov    %eax,%ebx
mov    0x2d98(%rip),%r15
mov    (%r15),%edi
call   1050 <dup@plt>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %r14,%rdi
call   1060 <execute@plt>
mov    (%r15),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"<\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c">\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
define void @redir(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %31
15: ; preds = %2
%16 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %5
%17 = call i8* @malloc(i32 256)
store i8* %17, i8** %7
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = load i8*, i8** %7
%23 = call i32 @read(i32 %21, i8* %22, i32 256)
%24 = load i8*, i8** %5
%25 = load i8*, i8** %7
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @execute(i8* %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
br label %56
31: ; preds = %2
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %55
34: ; preds = %31
%35 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %35, i8** %10
%36 = load i8*, i8** %4
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %37, %38
%40 = load i32, i32* @O_TRUNC
%41 = or i32 %39, %40
%42 = call i32 (i8*, i32, ...) @open(i8* %36, i32 %41, i32 438)
store i32 %42, i32* %11
%43 = load i32, i32* @STDOUT_FILENO
%44 = call i32 @dup(i32 %43)
store i32 %44, i32* %12
%45 = load i32, i32* %11
%46 = load i32, i32* @STDOUT_FILENO
%47 = call i32 @dup2(i32 %45, i32 %46)
%48 = load i8*, i8** %10
%49 = call i32 @execute(i8* %48)
%50 = load i32, i32* %12
%51 = load i32, i32* @STDOUT_FILENO
%52 = call i32 @dup2(i32 %50, i32 %51)
%53 = load i32, i32* %11
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %34, %31
br label %56
56: ; preds = %55, %15
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"<\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c">\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
define void @redir(i32 %0, i8* %1) {
%3 = alloca i8*
store i8* %1, i8** %3
switch i32 %0, label %32 [
i32 1, label %4
i32 2, label %13
]
4: ; preds = %2
%5 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = call dereferenceable_or_null(256) i8* @malloc(i32 256)
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 (i8*, i32, ...) @open(i8* %7, i32 %8)
%10 = call i32 @read(i32 %9, i8* %6, i32 256)
%11 = call i32 @strcpy(i8* %5, i8* %6)
%12 = call i32 @execute(i8* %5)
br label %29
13: ; preds = %2
%14 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %20, i32 438)
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @dup(i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %21, i32 %24)
%26 = call i32 @execute(i8* %14)
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @dup2(i32 %23, i32 %27)
br label %29
29: ; preds = %4, %13
%30 = phi i32 [ %21, %13 ], [ %9, %4 ]
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %2
ret void
}