#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_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int dup(int);
int dup2(int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *strtok(char *, char *);
int check_redirect_output(char *command) {
  int saved_stdout = 0;
  char command_dup[1024];
  strncpy(command_dup, command, 1023);
  char *output_symbol_location = strstr(command_dup, " > ");
  if (output_symbol_location) {
    char *file = strtok(output_symbol_location + 3, " \n");
    if (file) {
      int fd = open(file, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
      if (fd > 0) {
        saved_stdout = dup(1);
        printf("Redirecting stdout to file: %s\n", file);
        if (dup2(fd, 1) != -1) {
          close(fd);
        } else {
          perror("Unable to dup2. Error");
        }
      } else {
        perror("Unable to open file. Error");
      }
    } else {
      printf("No file specified.\n");
    }
  }
  return saved_stdout;
}


x86 assembly (clang, -O0):
<check_redirect_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     12db <check_redirect_output+0x14b>
mov    -0x418(%rbp),%rdi
add    $0x3,%rdi
lea    0xe0f(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     12c8 <check_redirect_output+0x138>
mov    -0x420(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    0x2d97(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    12b7 <check_redirect_output+0x127>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x420(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x424(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     12a6 <check_redirect_output+0x116>
mov    -0x424(%rbp),%edi
call   1070 <close@plt>
jmp    12b2 <check_redirect_output+0x122>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
jmp    12c3 <check_redirect_output+0x133>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
jmp    12d6 <check_redirect_output+0x146>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12db <check_redirect_output+0x14b>
mov    -0xc(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_redirect_output>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <strstr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1273 <check_redirect_output+0xd3>
add    $0x3,%rax
lea    0xe20(%rip),%rsi
mov    %rax,%rdi
call   10b0 <strtok@plt>
test   %rax,%rax
je     1267 <check_redirect_output+0xc7>
mov    %rax,%rbx
mov    0x2dbd(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    0x2db2(%rip),%rax
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
jle    1282 <check_redirect_output+0xe2>
mov    %eax,%ebp
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,%r14d
lea    0xdc4(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     1290 <check_redirect_output+0xf0>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1273 <check_redirect_output+0xd3>
lea    0xdea(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
jmp    1273 <check_redirect_output+0xd3>
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
jmp    1273 <check_redirect_output+0xd3>

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

LLVM IR (-O2):
@.str = [4 x i8] c" > \00"
@.str.1 = [3 x i8] c" \0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [32 x i8] c"Redirecting stdout to file: %s\0A\00"
@.str.3 = [22 x i8] c"Unable to dup2. Error\00"
@.str.4 = [27 x i8] c"Unable to open file. Error\00"
@str = [19 x i8] c"No file specified.\00"
define i32 @check_redirect_output(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 %35, 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 = icmp eq i8* %9, null
br i1 %10, label %33, label %11
11: ; preds = %7
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %13, %12
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %18, %17
%20 = call i32 @open(i8* %9, i32 %16, i32 %19)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %11
%23 = call i32 @dup(i32 1)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%25 = call i32 @dup2(i32 %20, i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %29, label %27
27: ; preds = %22
%28 = call i32 @close(i32 %20)
br label %35
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %35
31: ; preds = %11
%32 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %35
33: ; preds = %7
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %33, %27, %29, %31, %1
%36 = phi i32 [ 0, %1 ], [ 0, %33 ], [ %23, %27 ], [ %23, %29 ], [ 0, %31 ]
ret i32 %36
}