#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 EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, ...);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int main(void) {
  int pipefd[2], pipefd2[2];
  if ((pipe(pipefd)) == -1 || (pipe(pipefd2)) == -1) {
    perror("pipe");
    return EXIT_FAILURE;
  }
  if (fork() == 0) {
    dup2(pipefd[1], 1);
    close(pipefd[0]);
    execlp("ls", "ls", "-rt", "/bin/", NULL);
    return EXIT_FAILURE;
  } else {
    if (fork() == 0) {
      dup2(pipefd[0], 0);
      close(pipefd[1]);
      dup2(pipefd2[1], 1);
      execlp("grep", "grep", "sh", NULL);
      return EXIT_FAILURE;
    } else {
      dup2(pipefd2[0], 0);
      close(pipefd2[1]);
      execlp("wc", "wc", "-l", NULL);
      return EXIT_FAILURE;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     1193 <main+0x33>
lea    -0x14(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x50>
lea    0xe66(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a2 <main+0x142>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    120b <main+0xab>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
lea    0xe28(%rip),%rsi
lea    0xe24(%rip),%rdx
lea    0xe21(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a2 <main+0x142>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    1268 <main+0x108>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xdd0(%rip),%rsi
lea    0xdce(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a2 <main+0x142>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
lea    0xd99(%rip),%rsi
lea    0xd95(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xeb>
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xeb>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11c6 <main+0x66>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1201 <main+0xa1>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
lea    0xe5d(%rip),%rdi
lea    0xe59(%rip),%rdx
jmp    1231 <main+0xd1>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xe21(%rip),%rdi
lea    0xe1d(%rip),%rdx
lea    0xe1a(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
xor    %eax,%eax
call   1070 <execlp@plt>
jmp    123d <main+0xdd>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xde8(%rip),%rdi
lea    0xde6(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execlp@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
ret
lea    0xdae(%rip),%rdi
call   1060 <perror@plt>
jmp    123d <main+0xdd>

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

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [4 x i8] c"-rt\00"
@.str.3 = [6 x i8] c"/bin/\00"
@.str.4 = [5 x i8] c"grep\00"
@.str.5 = [3 x i8] c"sh\00"
@.str.6 = [3 x i8] c"wc\00"
@.str.7 = [3 x i8] c"-l\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = bitcast [2 x i32]* %2 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8, %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %44
14: ; preds = %8
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @dup2(i32 %19, i32 1)
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
%23 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %44
24: ; preds = %14
%25 = call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* %5
%29 = call i32 @dup2(i32 %28, i32 0)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @dup2(i32 %34, i32 1)
%36 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %44
37: ; preds = %24
%38 = load i32, i32* %9
%39 = call i32 @dup2(i32 %38, i32 0)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* null)
br label %44
44: ; preds = %37, %27, %17, %12
%45 = load i32, i32* @EXIT_FAILURE
ret i32 %45
}