#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void redirection_output(char *word1, char *argument, char *word2) {
  pid_t cpid;
  int rtrnstatus;
  int outpt;
  int save_out;
  outpt = open(word2, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (-1 == outpt) {
    perror(word2);
    return;
  }
  save_out = dup(fileno(stdout));
  if (-1 == dup2(outpt, fileno(stdout))) {
    perror("Cannot redirect stdout");
    return;
  }
  cpid = fork();
  if (cpid < 0) {
    perror("Fork failed");
    return;
  } else if (cpid == 0) {
    if (strcmp(argument, "\0") == 0)
      execlp(word1, word1, NULL);
    else
      execlp(word1, word1, argument, NULL);
  } else {
    waitpid(cpid, &rtrnstatus, 0);
    dup2(save_out, fileno(stdout));
    close(save_out);
    close(outpt);
    if (WIFEXITED(rtrnstatus))
      printf("Child's exit code %d\n", WEXITSTATUS(rtrnstatus));
    else
      printf("Child did not terminate with exit\n");
  }
}


x86 assembly (clang, -O0):
<redirection_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2dc5(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    1239 <redirection_output+0x69>
mov    -0x18(%rbp),%rdi
call   10c0 <perror@plt>
jmp    1393 <redirection_output+0x1c3>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1050 <dup@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    128f <redirection_output+0xbf>
lea    0xd7b(%rip),%rdi
call   10c0 <perror@plt>
jmp    1393 <redirection_output+0x1c3>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    12b6 <redirection_output+0xe6>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
jmp    1393 <redirection_output+0x1c3>
cmpq   $0x0,-0x20(%rbp)
jne    130f <redirection_output+0x13f>
mov    -0x10(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f3 <redirection_output+0x123>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   10e0 <execlp@plt>
jmp    130a <redirection_output+0x13a>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   10e0 <execlp@plt>
jmp    138e <redirection_output+0x1be>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     137b <redirection_output+0x1ab>
mov    -0x24(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xcb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1389 <redirection_output+0x1b9>
lea    0xcb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138e <redirection_output+0x1be>
jmp    1393 <redirection_output+0x1c3>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirection_output>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2db4(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2db1(%rip),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1333 <redirection_output+0x153>
mov    %eax,%ebp
mov    0x2d84(%rip),%r12
mov    (%r12),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%ebx
mov    (%r12),%edi
call   1090 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1338 <redirection_output+0x158>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
js     1341 <redirection_output+0x161>
je     12d3 <redirection_output+0xf3>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    (%r12),%edi
call   1090 <fileno@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   10e0 <WIFEXITED@plt>
test   %rax,%rax
je     1305 <redirection_output+0x125>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xd5b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1311 <redirection_output+0x131>
lea    0xd81(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
mov    %r15,%rdi
mov    %r15,%rsi
test   %rax,%rax
je     131e <redirection_output+0x13e>
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    10f0 <execlp@plt>
lea    0xd2d(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edx,%edx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    10f0 <execlp@plt>
mov    %rbx,%rdi
jmp    1348 <redirection_output+0x168>
lea    0xcc1(%rip),%rdi
jmp    1348 <redirection_output+0x168>
lea    0xccf(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stdout = global i32 0
@.str = [23 x i8] c"Cannot redirect stdout\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [22 x i8] c"Child's exit code %d\0A\00"
@.str.4 = [35 x i8] c"Child did not terminate with exit\0A\00"
define void @redirection_output(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %11, i32 %16, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = call i32 @perror(i8* %24)
br label %82
26: ; preds = %3
%27 = load i32, i32* @stdout
%28 = call i32 @fileno(i32 %27)
%29 = call i32 @dup(i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %9
%31 = load i32, i32* @stdout
%32 = call i32 @fileno(i32 %31)
%33 = call i32 @dup2(i32 %30, i32 %32)
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %82
37: ; preds = %26
%38 = call i64 (...) @fork()
store i64 %38, i64* %7
%39 = load i64, i64* %7
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %82
43: ; preds = %37
%44 = load i64, i64* %7
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %60
46: ; preds = %43
%47 = load i8*, i8** %5
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %4
%52 = load i8*, i8** %4
%53 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %51, i8* %52, i8* null)
br label %59
54: ; preds = %46
%55 = load i8*, i8** %4
%56 = load i8*, i8** %4
%57 = load i8*, i8** %5
%58 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %55, i8* %56, i8* %57, i8* null)
br label %59
59: ; preds = %54, %50
br label %81
60: ; preds = %43
%61 = load i64, i64* %7
%62 = call i32 @waitpid(i64 %61, i32* %8, i32 0)
%63 = load i32, i32* %10
%64 = load i32, i32* @stdout
%65 = call i32 @fileno(i32 %64)
%66 = call i32 @dup2(i32 %63, i32 %65)
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %9
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %8
%72 = call i64 @WIFEXITED(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %60
%75 = load i32, i32* %8
%76 = call i32 @WEXITSTATUS(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %76)
br label %80
78: ; preds = %60
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
br label %81
81: ; preds = %80, %59
br label %82
82: ; preds = %23, %35, %41, %81
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stdout = global i32 0
@.str = [23 x i8] c"Cannot redirect stdout\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [2 x i8] zeroinitializer
@.str.3 = [22 x i8] c"Child's exit code %d\0A\00"
@str = [34 x i8] c"Child did not terminate with exit\00"
define void @redirection_output(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %2, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i32 @perror(i8* %2)
br label %58
18: ; preds = %3
%19 = load i32, i32* @stdout
%20 = tail call i32 @fileno(i32 %19)
%21 = tail call i32 @dup(i32 %20)
%22 = load i32, i32* @stdout
%23 = tail call i32 @fileno(i32 %22)
%24 = tail call i32 @dup2(i32 %14, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %58
28: ; preds = %18
%29 = tail call i64 (...) @fork()
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %58
33: ; preds = %28
%34 = icmp eq i64 %29, 0
br i1 %34, label %35, label %42
35: ; preds = %33
%36 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* %0, i8* %0, i8* null)
br label %58
40: ; preds = %35
%41 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* %0, i8* %0, i8* %1, i8* null)
br label %58
42: ; preds = %33
%43 = call i32 @waitpid(i64 %29, i32* %4, i32 0)
%44 = load i32, i32* @stdout
%45 = call i32 @fileno(i32 %44)
%46 = call i32 @dup2(i32 %21, i32 %45)
%47 = call i32 @close(i32 %21)
%48 = call i32 @close(i32 %14)
%49 = load i32, i32* %4
%50 = call i64 @WIFEXITED(i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %42
%53 = load i32, i32* %4
%54 = call i32 @WEXITSTATUS(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %58
56: ; preds = %42
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %58
58: ; preds = %52, %56, %38, %40, %31, %26, %16
ret void
}