#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void redirection_input(char *word1, char *word2) {
  pid_t cpid;
  int rtrnstatus;
  cpid = fork();
  if (cpid < 0) {
    perror("fork failed");
    return;
  } else if (cpid == 0) {
    execlp(word1, word1, word2, NULL);
  } else {
    waitpid(cpid, &rtrnstatus, 0);
    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_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11a7 <redirection_input+0x37>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
jmp    1222 <redirection_input+0xb2>
cmpq   $0x0,-0x18(%rbp)
jne    11cc <redirection_input+0x5c>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <execlp@plt>
jmp    121d <redirection_input+0xad>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     120a <redirection_input+0x9a>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1218 <redirection_input+0xa8>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121d <redirection_input+0xad>
jmp    1222 <redirection_input+0xb2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirection_input>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11fb <redirection_input+0x7b>
je     11d0 <redirection_input+0x50>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11e7 <redirection_input+0x67>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f3 <redirection_input+0x73>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1090 <execlp@plt>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfe(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <perror@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [22 x i8] c"Child's exit code %d\0A\00"
@.str.2 = [35 x i8] c"Child did not terminate with exit\0A\00"
define void @redirection_input(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = call i64 (...) @fork()
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %34
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @execlp(i8* %16, i8* %17, i8* %18, i32* null)
br label %33
20: ; preds = %12
%21 = load i64, i64* %5
%22 = call i32 @waitpid(i64 %21, i32* %6, i32 0)
%23 = load i32, i32* %6
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* %6
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %32
30: ; preds = %20
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %15
br label %34
34: ; preds = %10, %33
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [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_input(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %2
%10 = icmp eq i64 %5, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @execlp(i8* %0, i8* %0, i8* %1, i32* null)
br label %24
13: ; preds = %9
%14 = call i32 @waitpid(i64 %5, i32* %3, i32 0)
%15 = load i32, i32* %3
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i32, i32* %3
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %24
22: ; preds = %13
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %18, %22, %11, %7
ret void
}