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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    11dd <main+0x7d>
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
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rax
lea    0xe3c(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%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    1242 <main+0xe2>
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>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
lea    0xda2(%rip),%rsi
lea    0xd9e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11b2 <main+0x52>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1030 <dup@plt>
lea    0x8(%rbx),%rsi
mov    0x8(%rbx),%rax
mov    %rax,0x10(%rbx)
lea    0xe57(%rip),%rdi
mov    %rdi,0x8(%rbx)
call   1060 <execvp@plt>
mov    0x4(%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    1210 <main+0xb0>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    0x10(%rbx),%rdx
lea    0xdff(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1070 <execlp@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
lea    0xdd2(%rip),%rdi
lea    0xdce(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1070 <execlp@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"head\00"
@.str.1 = [5 x i8] c"grep\00"
@.str.2 = [3 x i8] c"wc\00"
@.str.3 = [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]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i32 (...) @fork()
%11 = icmp ne i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = call i32 @close(i32 1)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @dup(i32 %18)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
store i8* %22, i8** %24
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = call i32 @execvp(i8* %29, i8** %31)
br label %33
33: ; preds = %12, %2
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%38 = call i32 @pipe(i32* %37)
%39 = call i32 (...) @fork()
%40 = icmp ne i32 %39, 0
br i1 %40, label %57, label %41
41: ; preds = %33
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = call i32 @close(i32 0)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @dup(i32 %47)
%49 = call i32 @close(i32 1)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @dup(i32 %51)
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = call i32 @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* %55, i32* null)
br label %57
57: ; preds = %41, %33
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%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 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [5 x i8] c"head\00"
@.str.1 = [5 x i8] c"grep\00"
@.str.2 = [3 x i8] c"wc\00"
@.str.3 = [3 x i8] c"-l\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i32, i32* %6
%12 = call i32 @close(i32 %11)
%13 = call i32 @close(i32 1)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @dup(i32 %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = getelementptr inbounds i8*, i8** %1, i64 2
store i8* %18, i8** %19
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %17
%20 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %17)
br label %21
21: ; preds = %10, %2
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = bitcast [2 x i32]* %4 to i8*
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%27 = call i32 @pipe(i32* %26)
%28 = call i32 (...) @fork()
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %21
%31 = load i32, i32* %26
%32 = call i32 @close(i32 %31)
%33 = call i32 @close(i32 0)
%34 = load i32, i32* %6
%35 = call i32 @dup(i32 %34)
%36 = call i32 @close(i32 1)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @dup(i32 %38)
%40 = getelementptr inbounds i8*, i8** %1, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @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* %41, i32* null)
br label %43
43: ; preds = %30, %21
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = call i32 @close(i32 0)
%50 = load i32, i32* %26
%51 = call i32 @dup(i32 %50)
%52 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
ret i32 0
}