#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 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*/ fatal(char *);
int open(char *, int, int);
int strlen(char *);
char *strtok(char *, char *);
void parse_redirect(char *cmd) {
  char *arg;
  int cmdlen = strlen(cmd);
  int fd, i;
  for (i = cmdlen - 1; i >= 0; i--) {
    switch (cmd[i]) {
    case '<':
      arg = strtok(&cmd[i + 1], " \t");
      if ((fd = open(arg, O_RDONLY | O_CREAT, 0644)) < 0)
        fatal("file open error");
      dup2(fd, STDIN_FILENO);
      close(fd);
      cmd[i] = '\0';
      break;
    case '>':
      arg = strtok(&cmd[i + 1], " \t");
      if ((fd = open(arg, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
        fatal("file open error");
      dup2(fd, STDOUT_FILENO);
      close(fd);
      cmd[i] = '\0';
      break;
    default:
      break;
    }
  }
}


x86 assembly (clang, -O0):
<parse_redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     12dc <parse_redirect+0x17c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x20(%rbp)
sub    $0x3c,%eax
je     11b9 <parse_redirect+0x59>
jmp    11a8 <parse_redirect+0x48>
mov    -0x20(%rbp),%eax
sub    $0x3e,%eax
je     123a <parse_redirect+0xda>
jmp    12c4 <parse_redirect+0x164>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rdi
lea    0xe31(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1210 <parse_redirect+0xb0>
lea    0xdf8(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x18(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12c9 <parse_redirect+0x169>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rdi
lea    0xdb0(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x2d8b(%rip),%rax
or     (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    129a <parse_redirect+0x13a>
lea    0xd6e(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x18(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12c9 <parse_redirect+0x169>
jmp    12c9 <parse_redirect+0x169>
jmp    12ce <parse_redirect+0x16e>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1181 <parse_redirect+0x21>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_redirect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1264 <parse_redirect+0x104>
mov    %eax,%edi
mov    0x2e74(%rip),%r13
jmp    11b7 <parse_redirect+0x57>
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
movb   $0x0,(%rbx,%r12,1)
lea    0x1(%r15),%rax
mov    %r15,%rdi
cmp    $0x1,%rax
jbe    1264 <parse_redirect+0x104>
lea    -0x1(%rdi),%r15
mov    %r15d,%r12d
movsbl (%rbx,%r12,1),%eax
cmp    $0x3e,%eax
je     1210 <parse_redirect+0xb0>
cmp    $0x3c,%eax
jne    11a6 <parse_redirect+0x46>
add    %rbx,%rdi
lea    0xe29(%rip),%rsi
call   1070 <strtok@plt>
mov    0x0(%r13),%esi
mov    0x2de9(%rip),%rcx
or     (%rcx),%esi
mov    %rax,%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2dc1(%rip),%r14
test   %eax,%eax
jns    1190 <parse_redirect+0x30>
jmp    1253 <parse_redirect+0xf3>
cs nopw 0x0(%rax,%rax,1)
nop
add    %rbx,%rdi
lea    0xde6(%rip),%rsi
call   1070 <strtok@plt>
mov    0x0(%r13),%esi
mov    0x2d86(%rip),%rcx
or     (%rcx),%esi
mov    0x2dbd(%rip),%rcx
or     (%rcx),%esi
mov    %rax,%rdi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2d95(%rip),%r14
test   %eax,%eax
jns    1190 <parse_redirect+0x30>
lea    0xda9(%rip),%rdi
call   1080 <fatal@plt>
jmp    1190 <parse_redirect+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c" \09\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [16 x i8] c"file open error\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
define void @parse_redirect(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %6
br label %11
11: ; preds = %75, %1
%12 = load i32, i32* %6
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %78
14: ; preds = %11
%15 = load i8*, i8** %2
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
switch i32 %20, label %73 [
i32 60, label %21
i32 62, label %46
]
21: ; preds = %14
%22 = load i8*, i8** %2
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = call i8* @strtok(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %27, i8** %3
%28 = load i8*, i8** %3
%29 = load i32, i32* @O_RDONLY
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31, i32 420)
store i32 %32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %21
%37 = load i32, i32* %5
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
%42 = load i8*, i8** %2
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
br label %74
46: ; preds = %14
%47 = load i8*, i8** %2
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = call i8* @strtok(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %52, i8** %3
%53 = load i8*, i8** %3
%54 = load i32, i32* @O_WRONLY
%55 = load i32, i32* @O_CREAT
%56 = or i32 %54, %55
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %56, %57
%59 = call i32 @open(i8* %53, i32 %58, i32 420)
store i32 %59, i32* %5
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %46
%62 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %63
63: ; preds = %61, %46
%64 = load i32, i32* %5
%65 = load i32, i32* @STDOUT_FILENO
%66 = call i32 @dup2(i32 %64, i32 %65)
%67 = load i32, i32* %5
%68 = call i32 @close(i32 %67)
%69 = load i8*, i8** %2
%70 = load i32, i32* %6
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
br label %74
73: ; preds = %14
br label %74
74: ; preds = %73, %63, %36
br label %75
75: ; preds = %74
%76 = load i32, i32* %6
%77 = add nsw i32 %76, -1
store i32 %77, i32* %6
br label %11
78: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c" \09\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [16 x i8] c"file open error\00"
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@STDOUT_FILENO = global i32 0
define void @parse_redirect(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %43
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %41
%7 = phi i64 [ %5, %4 ], [ %8, %41 ]
%8 = add nsw i64 %7, -1
%9 = and i64 %8, 4294967295
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %41 [
i32 60, label %13
i32 62, label %21
]
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %0, i64 %7
%15 = tail call i8* @strtok(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %15, i32 %18, i32 420)
%20 = icmp slt i32 %19, 0
br i1 %20, label %31, label %35
21: ; preds = %6
%22 = getelementptr inbounds i8, i8* %0, i64 %7
%23 = tail call i8* @strtok(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = tail call i32 @open(i8* %23, i32 %28, i32 420)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21, %13
%32 = phi i32* [ @STDIN_FILENO, %13 ], [ @STDOUT_FILENO, %21 ]
%33 = phi i32 [ %19, %13 ], [ %29, %21 ]
%34 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %31, %21, %13
%36 = phi i32* [ @STDIN_FILENO, %13 ], [ @STDOUT_FILENO, %21 ], [ %32, %31 ]
%37 = phi i32 [ %19, %13 ], [ %29, %21 ], [ %33, %31 ]
%38 = load i32, i32* %36
%39 = tail call i32 @dup2(i32 %37, i32 %38)
%40 = tail call i32 @close(i32 %37)
store i8 0, i8* %10
br label %41
41: ; preds = %35, %6
%42 = icmp ugt i64 %7, 1
br i1 %42, label %6, label %43
43: ; preds = %41, %1
ret void
}