#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void handle_pipe(char *word1, char *pipearg1, char *word2, char *pipearg2) {
  int pipefd[2];
  int status;
  pid_t pid1, pid2;
  if (pipe(pipefd) == -1) {
    perror("pipe");
    _exit(0);
  }
  pid1 = fork();
  if (pid1 == -1) {
    perror("fork");
    _exit(0);
  }
  if (pid1 == 0) {
    if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
      perror("dup2");
      _exit(0);
    }
    close(pipefd[0]);
    if (pipefd[1] != STDOUT_FILENO)
      close(pipefd[1]);
    if (strcmp(pipearg1, "\0") == 0)
      execlp(word1, word1, NULL);
    else
      execlp(word1, word1, pipearg1, NULL);
  }
  close(pipefd[1]);
  pid2 = fork();
  if (pid2 == -1) {
    perror("fork");
    kill(pid1, SIGTERM);
    _exit(0);
  }
  if (pid2 == 0) {
    if (dup2(pipefd[0], STDIN_FILENO) == -1) {
      perror("dup2");
      kill(pid1, SIGTERM);
      _exit(0);
    }
    if (pipefd[0] != STDIN_FILENO)
      close(pipefd[0]);
    if (strcmp(pipearg2, "\0") == 0)
      execlp(word2, word2, NULL);
    else
      execlp(word2, word2, pipearg2, NULL);
  }
  close(pipefd[0]);
  waitpid(pid1, NULL, 0);
  waitpid(pid2, &status, 0);
}


x86 assembly (clang, -O0):
<handle_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11dd <handle_pipe+0x3d>
lea    0xe2f(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1204 <handle_pipe+0x64>
lea    0xe0d(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x30(%rbp)
jne    12ab <handle_pipe+0x10b>
mov    -0x24(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    123b <handle_pipe+0x9b>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%eax
je     125d <handle_pipe+0xbd>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    128f <handle_pipe+0xef>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   10b0 <execlp@plt>
jmp    12a6 <handle_pipe+0x106>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   10b0 <execlp@plt>
jmp    12ab <handle_pipe+0x10b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12eb <handle_pipe+0x14b>
lea    0xd37(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x34(%rbp)
jne    139b <handle_pipe+0x1fb>
mov    -0x28(%rbp),%edi
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1333 <handle_pipe+0x193>
lea    0xcf4(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x28(%rbp),%eax
mov    0x2c93(%rip),%rcx
cmp    (%rcx),%eax
je     134d <handle_pipe+0x1ad>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
lea    0xcb7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    137f <handle_pipe+0x1df>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   10b0 <execlp@plt>
jmp    1396 <handle_pipe+0x1f6>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   10b0 <execlp@plt>
jmp    139b <handle_pipe+0x1fb>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x34(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1305 <handle_pipe+0x165>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%r12d
test   %eax,%eax
je     11ed <handle_pipe+0x4d>
cmp    $0xffffffff,%r12d
jne    1253 <handle_pipe+0xb3>
lea    0xe1d(%rip),%rdi
jmp    1315 <handle_pipe+0x175>
mov    0x10(%rsp),%edi
mov    0x2df8(%rip),%r13
mov    0x0(%r13),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     130e <handle_pipe+0x16e>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
cmp    0x0(%r13),%edi
je     1222 <handle_pipe+0x82>
call   1050 <close@plt>
lea    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
mov    %rbp,%rdi
mov    %rbp,%rsi
test   %rax,%rax
je     124a <handle_pipe+0xaa>
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   10b0 <execlp@plt>
jmp    1253 <handle_pipe+0xb3>
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <execlp@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     127a <handle_pipe+0xda>
cmp    $0xffffffff,%ebp
jne    12d3 <handle_pipe+0x133>
lea    0xd90(%rip),%rdi
jmp    1328 <handle_pipe+0x188>
mov    0xc(%rsp),%edi
mov    0x2d4b(%rip),%rbx
mov    (%rbx),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1321 <handle_pipe+0x181>
mov    0xc(%rsp),%edi
cmp    (%rbx),%edi
je     12a2 <handle_pipe+0x102>
call   1050 <close@plt>
lea    0xd66(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
mov    %r14,%rdi
mov    %r14,%rsi
test   %rax,%rax
je     12ca <handle_pipe+0x12a>
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   10b0 <execlp@plt>
jmp    12d3 <handle_pipe+0x133>
xor    %edx,%edx
xor    %eax,%eax
call   10b0 <execlp@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
jmp    1315 <handle_pipe+0x175>
lea    0xcf5(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xce2(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1080 <kill@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [2 x i8] zeroinitializer
@SIGTERM = global i32 0
@STDIN_FILENO = global i32 0
define void @handle_pipe(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @_exit(i32 0)
unreachable
19: ; preds = %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @_exit(i32 0)
unreachable
26: ; preds = %19
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %64
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @_exit(i32 0)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%43 = load i32, i32* %42
%44 = load i32, i32* @STDOUT_FILENO
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %46, %38
%51 = load i8*, i8** %6
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i8*, i8** %5
%56 = load i8*, i8** %5
%57 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %55, i8* %56, i8* null)
br label %63
58: ; preds = %50
%59 = load i8*, i8** %5
%60 = load i8*, i8** %5
%61 = load i8*, i8** %6
%62 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %59, i8* %60, i8* %61, i8* null)
br label %63
63: ; preds = %58, %54
br label %64
64: ; preds = %63, %26
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
%68 = call i32 (...) @fork()
store i32 %68, i32* %12
%69 = load i32, i32* %12
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %77
71: ; preds = %64
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%73 = load i32, i32* %11
%74 = load i32, i32* @SIGTERM
%75 = call i32 @kill(i32 %73, i32 %74)
%76 = call i32 @_exit(i32 0)
unreachable
77: ; preds = %64
%78 = load i32, i32* %12
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %115
80: ; preds = %77
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%82 = load i32, i32* %81
%83 = load i32, i32* @STDIN_FILENO
%84 = call i32 @dup2(i32 %82, i32 %83)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %92
86: ; preds = %80
%87 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%88 = load i32, i32* %11
%89 = load i32, i32* @SIGTERM
%90 = call i32 @kill(i32 %88, i32 %89)
%91 = call i32 @_exit(i32 0)
unreachable
92: ; preds = %80
%93 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%94 = load i32, i32* %93
%95 = load i32, i32* @STDIN_FILENO
%96 = icmp ne i32 %94, %95
br i1 %96, label %97, label %101
97: ; preds = %92
%98 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%99 = load i32, i32* %98
%100 = call i32 @close(i32 %99)
br label %101
101: ; preds = %97, %92
%102 = load i8*, i8** %8
%103 = call i64 @strcmp(i8* %102, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%104 = icmp eq i64 %103, 0
br i1 %104, label %105, label %109
105: ; preds = %101
%106 = load i8*, i8** %7
%107 = load i8*, i8** %7
%108 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %106, i8* %107, i8* null)
br label %114
109: ; preds = %101
%110 = load i8*, i8** %7
%111 = load i8*, i8** %7
%112 = load i8*, i8** %8
%113 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %110, i8* %111, i8* %112, i8* null)
br label %114
114: ; preds = %109, %105
br label %115
115: ; preds = %114, %77
%116 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%117 = load i32, i32* %116
%118 = call i32 @close(i32 %117)
%119 = load i32, i32* %11
%120 = call i32 @waitpid(i32 %119, i32* null, i32 0)
%121 = load i32, i32* %12
%122 = call i32 @waitpid(i32 %121, i32* %10, i32 0)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [2 x i8] zeroinitializer
@SIGTERM = global i32 0
@STDIN_FILENO = global i32 0
define void @handle_pipe(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = bitcast [2 x i32]* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @_exit(i32 0)
unreachable
15: ; preds = %4
%16 = call i32 (...) @fork()
switch i32 %16, label %44 [
i32 -1, label %17
i32 0, label %20
]
17: ; preds = %15
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @_exit(i32 0)
unreachable
20: ; preds = %15
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32, i32* @STDOUT_FILENO
%24 = call i32 @dup2(i32 %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @_exit(i32 0)
unreachable
29: ; preds = %20
%30 = load i32, i32* %9
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %21
%33 = load i32, i32* @STDOUT_FILENO
%34 = icmp eq i32 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %29
%36 = call i32 @close(i32 %32)
br label %37
37: ; preds = %35, %29
%38 = call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %0, i8* %0, i8* null)
br label %44
42: ; preds = %37
%43 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %0, i8* %0, i8* %1, i8* null)
br label %44
44: ; preds = %15, %40, %42
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i32 (...) @fork()
switch i32 %48, label %77 [
i32 -1, label %49
i32 0, label %54
]
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* @SIGTERM
%52 = call i32 @kill(i32 %16, i32 %51)
%53 = call i32 @_exit(i32 0)
unreachable
54: ; preds = %44
%55 = load i32, i32* %9
%56 = load i32, i32* @STDIN_FILENO
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* @SIGTERM
%62 = call i32 @kill(i32 %16, i32 %61)
%63 = call i32 @_exit(i32 0)
unreachable
64: ; preds = %54
%65 = load i32, i32* %9
%66 = load i32, i32* @STDIN_FILENO
%67 = icmp eq i32 %65, %66
br i1 %67, label %70, label %68
68: ; preds = %64
%69 = call i32 @close(i32 %65)
br label %70
70: ; preds = %68, %64
%71 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %70
%74 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %2, i8* %2, i8* null)
br label %77
75: ; preds = %70
%76 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* %2, i8* %2, i8* %3, i8* null)
br label %77
77: ; preds = %44, %73, %75
%78 = load i32, i32* %9
%79 = call i32 @close(i32 %78)
%80 = call i32 @waitpid(i32 %16, i32* null, i32 0)
%81 = call i32 @waitpid(i32 %48, i32* %6, i32 0)
ret void
}