#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ prompt_and_parse(char **, char **);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
main() {
  int p[2];
  char *upstream[20], *downstream[20];
  while (prompt_and_parse(upstream, downstream) > 0) {
    if (downstream[0] == NULL) {
      if (fork() == 0) {
        execvp(upstream[0], upstream);
        printf("%s: not found\n", upstream[0]);
        exit(1);
      } else {
        wait(0);
      }
    } else {
      pipe(p);
      if (fork() == 0) {
        dup2(p[1], 1);
        close(p[0]);
        execvp(upstream[0], upstream);
        printf("%s: not found\n", upstream[0]);
        exit(1);
      }
      if (fork() == 0) {
        dup2(p[0], 0);
        close(p[1]);
        execvp(downstream[0], downstream);
        printf("%s: not found\n", downstream[0]);
        exit(1);
      } else {
        close(p[0]);
        close(p[1]);
        wait(0);
        wait(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xb0(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1070 <prompt_and_parse@plt>
cmp    $0x0,%rax
jle    12f4 <main+0x164>
cmpq   $0x0,-0x150(%rbp)
jne    121a <main+0x8a>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    120e <main+0x7e>
mov    -0xb0(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0xb0(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    12ef <main+0x15f>
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    1279 <main+0xe9>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xb0(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0xb0(%rbp),%rsi
lea    0xd96(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12cc <main+0x13c>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x150(%rbp),%rdi
lea    -0x150(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x150(%rbp),%rsi
lea    0xd43(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    12ef <main+0x15f>
jmp    11a2 <main+0x12>
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x150,%rsp
lea    0x10(%rsp),%rdi
lea    0xb0(%rsp),%rsi
call   1070 <prompt_and_parse@plt>
test   %rax,%rax
jle    123d <main+0xad>
lea    0x10(%rsp),%r14
lea    0xb0(%rsp),%rbx
lea    0x8(%rsp),%r15
jmp    11f7 <main+0x67>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1263 <main+0xd3>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <prompt_and_parse@plt>
test   %rax,%rax
jle    123d <main+0xad>
cmpq   $0x0,0xb0(%rsp)
je     11d0 <main+0x40>
mov    %r15,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     124c <main+0xbc>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    0x8(%rsp),%edi
test   %rax,%rax
je     1279 <main+0xe9>
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    11e0 <main+0x50>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1080 <execvp@plt>
mov    0x10(%rsp),%rsi
jmp    12a6 <main+0x116>
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0xb0(%rsp),%rdi
lea    0xb0(%rsp),%rsi
call   1080 <execvp@plt>
mov    0xb0(%rsp),%rsi
lea    0xd53(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"%s: not found\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [20 x i8*]
%4 = alloca [20 x i8*]
store i32 0, i32* %1
br label %5
5: ; preds = %77, %0
%6 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%7 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%8 = call i64 @prompt_and_parse(i8** %6, i8** %7)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %78
10: ; preds = %5
%11 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %29
14: ; preds = %10
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%21 = call i32 @execvp(i8* %19, i8** %20)
%22 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %14
%27 = call i32 @wait(i32 0)
br label %28
28: ; preds = %26
br label %77
29: ; preds = %10
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%31 = call i32 @pipe(i32* %30)
%32 = call i64 (...) @fork()
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %49
34: ; preds = %29
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @dup2(i32 %36, i32 1)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%42 = load i8*, i8** %41
%43 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%44 = call i32 @execvp(i8* %42, i8** %43)
%45 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%46 = load i8*, i8** %45
%47 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %29
%50 = call i64 (...) @fork()
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %67
52: ; preds = %49
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @dup2(i32 %54, i32 0)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%60 = load i8*, i8** %59
%61 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%62 = call i32 @execvp(i8* %60, i8** %61)
%63 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%64 = load i8*, i8** %63
%65 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %49
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = call i32 @wait(i32 0)
%75 = call i32 @wait(i32 0)
br label %76
76: ; preds = %67
br label %77
77: ; preds = %76, %28
br label %5
78: ; preds = %5
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str = [15 x i8] c"%s: not found\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [20 x i8*]
%3 = alloca [20 x i8*]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast [20 x i8*]* %2 to i8*
%6 = bitcast [20 x i8*]* %3 to i8*
%7 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%9 = call i64 @prompt_and_parse(i8** %7, i8** %8)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %62
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %14
14: ; preds = %11, %58
%15 = load i8*, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = call i64 (...) @fork()
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %58
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = call i32 @execvp(i8* %21, i8** %7)
%23 = load i8*, i8** %7
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %14
%27 = call i32 @pipe(i32* %12)
%28 = call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = load i32, i32* %13
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = load i32, i32* %12
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %7
%36 = call i32 @execvp(i8* %35, i8** %7)
%37 = load i8*, i8** %7
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %26
%41 = call i64 (...) @fork()
%42 = icmp eq i64 %41, 0
%43 = load i32, i32* %12
br i1 %42, label %44, label %53
44: ; preds = %40
%45 = call i32 @dup2(i32 %43, i32 0)
%46 = load i32, i32* %13
%47 = call i32 @close(i32 %46)
%48 = load i8*, i8** %8
%49 = call i32 @execvp(i8* %48, i8** %8)
%50 = load i8*, i8** %8
%51 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %40
%54 = call i32 @close(i32 %43)
%55 = load i32, i32* %13
%56 = call i32 @close(i32 %55)
%57 = call i32 @wait(i32 0)
br label %58
58: ; preds = %17, %53
%59 = call i32 @wait(i32 0)
%60 = call i64 @prompt_and_parse(i8** %7, i8** %8)
%61 = icmp sgt i64 %60, 0
br i1 %61, label %14, label %62
62: ; preds = %58, %0
ret i32 0
}