#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *strtok(char *, char *);
void check_redirect_input(char *command) {
  char command_dup[1024];
  strncpy(command_dup, command, 1023);
  char *input_symbol_location = strstr(command_dup, " < ");
  if (input_symbol_location) {
    char *file = strtok(input_symbol_location + 3, " \n");
    printf("Filename: %s\n", file);
    if (file) {
      int fd = open(file, O_RDONLY);
      if (fd > 0) {
        printf("Redirecting stdin to file: %s\n", file);
        if (dup2(fd, 0) != -1) {
          close(fd);
        } else {
          perror("Unable to dup2. Error");
        }
      } else {
        perror("Unable to open file. Error");
      }
    } else {
      printf("No file specified.\n");
    }
  }
}


x86 assembly (clang, -O0):
<check_redirect_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     12a5 <check_redirect_input+0x125>
mov    -0x418(%rbp),%rdi
add    $0x3,%rdi
lea    0xe26(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x420(%rbp)
je     1292 <check_redirect_input+0x112>
mov    -0x420(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    1281 <check_redirect_input+0x101>
mov    -0x420(%rbp),%rsi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x424(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1270 <check_redirect_input+0xf0>
mov    -0x424(%rbp),%edi
call   1060 <close@plt>
jmp    127c <check_redirect_input+0xfc>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
jmp    128d <check_redirect_input+0x10d>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
jmp    12a0 <check_redirect_input+0x120>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a5 <check_redirect_input+0x125>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_redirect_input>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0xe4d(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1237 <check_redirect_input+0xa7>
add    $0x3,%rax
lea    0xe39(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strtok@plt>
mov    %rax,%rbx
lea    0xe2a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbx,%rbx
je     122b <check_redirect_input+0x9b>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    1241 <check_redirect_input+0xb1>
mov    %eax,%ebp
lea    0xe0b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     124f <check_redirect_input+0xbf>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1237 <check_redirect_input+0xa7>
lea    0xe33(%rip),%rdi
call   1040 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
jmp    1237 <check_redirect_input+0xa7>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
jmp    1237 <check_redirect_input+0xa7>

LLVM IR (-O0):
@.str = [4 x i8] c" < \00"
@.str.1 = [3 x i8] c" \0A\00"
@.str.2 = [14 x i8] c"Filename: %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [31 x i8] c"Redirecting stdin to file: %s\0A\00"
@.str.4 = [22 x i8] c"Unable to dup2. Error\00"
@.str.5 = [27 x i8] c"Unable to open file. Error\00"
@.str.6 = [20 x i8] c"No file specified.\0A\00"
define void @check_redirect_input(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @strncpy(i8* %7, i8* %8, i32 1023)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %46
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i64 3
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %43
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %22
%29 = load i8*, i8** %5
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %6
%32 = call i32 @dup2(i32 %31, i32 0)
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
br label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
br label %42
40: ; preds = %22
%41 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %39
br label %45
43: ; preds = %14
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %42
br label %46
46: ; preds = %45, %1
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c" < \00"
@.str.1 = [3 x i8] c" \0A\00"
@.str.2 = [14 x i8] c"Filename: %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [31 x i8] c"Redirecting stdin to file: %s\0A\00"
@.str.4 = [22 x i8] c"Unable to dup2. Error\00"
@.str.5 = [27 x i8] c"Unable to open file. Error\00"
@str = [19 x i8] c"No file specified.\00"
define void @check_redirect_input(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @strncpy(i8* %3, i8* %0, i32 1023)
%5 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %28, label %7
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %5, i64 3
%9 = call i8* @strtok(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = icmp eq i8* %9, null
br i1 %11, label %26, label %12
12: ; preds = %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %9, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%18 = call i32 @dup2(i32 %14, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @close(i32 %14)
br label %28
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %28
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %28
26: ; preds = %7
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %20, %22, %24, %1
ret void
}