#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*/ dup(int);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ pipe(int *);
int main(int argc, char **argv) {
  int fds1[2];
  pipe(fds1);
  if (!fork()) {
    close(fds1[0]);
    close(1);
    dup(fds1[1]);
    argv[0] = "grep";
    execvp(argv[0], argv);
  }
  close(fds1[1]);
  int fds2[2];
  pipe(fds2);
  if (!fork()) {
    close(fds2[0]);
    close(0);
    dup(fds1[0]);
    close(1);
    dup(fds2[1]);
    execlp("sort", "sort", NULL);
  }
  close(fds1[0]);
  close(fds2[1]);
  int fds3[2];
  pipe(fds3);
  if (!fork()) {
    close(fds3[0]);
    close(0);
    dup(fds2[0]);
    close(1);
    dup(fds3[1]);
    execlp("uniq", "uniq", NULL);
  }
  close(fds2[0]);
  close(fds3[1]);
  close(0);
  dup(fds3[0]);
  execlp("wc", "wc", "-l", NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    11c7 <main+0x67>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <dup@plt>
mov    -0x10(%rbp),%rax
lea    0xe4c(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    1226 <main+0xc6>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
lea    0xded(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    128d <main+0x12d>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <dup@plt>
lea    0xd8b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1030 <dup@plt>
lea    0xd5c(%rip),%rsi
lea    0xd58(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11ab <main+0x4b>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1030 <dup@plt>
lea    0xe60(%rip),%rdi
mov    %rdi,(%rbx)
mov    %rbx,%rsi
call   1060 <execvp@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    1208 <main+0xa8>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
lea    0xe09(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1070 <execlp@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0x18(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    126e <main+0x10e>
mov    0x18(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x1c(%rsp),%edi
call   1030 <dup@plt>
lea    0xda8(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1070 <execlp@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x1c(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x18(%rsp),%edi
call   1030 <dup@plt>
lea    0xd78(%rip),%rdi
lea    0xd74(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execlp@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

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