#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 fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int main(int argc, char *argv[]) {
  int p1[2];
  int p2[2];
  pipe(p1);
  pipe(p2);
  switch (fork()) {
  case -1:
    perror("Fork error");
    break;
  case 0:
    close(1);
    dup(p1[1]);
    close(p1[0]);
    close(p1[1]);
    close(p2[0]);
    close(p2[1]);
    execlp("ls", "ls", "-alh", NULL);
    break;
  default:
    switch (fork()) {
    case -1:
      perror("Fork error");
      break;
    case 0:
      close(0);
      close(1);
      dup(p1[0]);
      dup(p2[1]);
      close(p1[0]);
      close(p1[1]);
      close(p2[0]);
      close(p2[1]);
      execlp("sort", "sort", NULL);
      break;
    default:
      close(0);
      dup(p2[0]);
      close(p1[0]);
      close(p1[1]);
      close(p2[0]);
      close(p2[1]);
      execlp("grep", "grep", ".c", NULL);
      break;
    }
  }
  return 0;
}


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>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11b2 <main+0x52>
jmp    11a2 <main+0x42>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11c3 <main+0x63>
jmp    1216 <main+0xb6>
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
jmp    12fc <main+0x19c>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xe0f(%rip),%rsi
lea    0xe0b(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    12fc <main+0x19c>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1240 <main+0xe0>
jmp    1230 <main+0xd0>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     1251 <main+0xf1>
jmp    12ac <main+0x14c>
lea    0xdb9(%rip),%rdi
call   1060 <perror@plt>
jmp    12f7 <main+0x197>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xd7a(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    12f7 <main+0x197>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xd36(%rip),%rsi
lea    0xd34(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    12fc <main+0x19c>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11e8 <main+0x88>
cmp    $0xffffffff,%eax
je     129c <main+0x13c>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     123b <main+0xdb>
cmp    $0xffffffff,%eax
je     129c <main+0x13c>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe39(%rip),%rdi
lea    0xe37(%rip),%rdx
jmp    122d <main+0xcd>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1030 <dup@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xde5(%rip),%rdi
lea    0xde1(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <execlp@plt>
jmp    1295 <main+0x135>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1030 <dup@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xd8a(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1070 <execlp@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xd5d(%rip),%rdi
call   1060 <perror@plt>
jmp    1295 <main+0x135>

LLVM IR (-O0):
@.str = [11 x i8] c"Fork error\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"-alh\00"
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [5 x i8] c"grep\00"
@.str.5 = [3 x i8] c".c\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 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
switch i32 %12, label %33 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %78
15: ; preds = %2
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 (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 ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %78
33: ; preds = %2
%34 = call i32 (...) @fork()
switch i32 %34, label %59 [
i32 -1, label %35
i32 0, label %37
]
35: ; preds = %33
%36 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %77
37: ; preds = %33
%38 = call i32 @close(i32 0)
%39 = call i32 @close(i32 1)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @dup(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @dup(i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* null)
br label %77
59: ; preds = %33
%60 = call i32 @close(i32 0)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%62 = load i32, i32* %61
%63 = call i32 @dup(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
%76 = call i32 (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 %77
77: ; preds = %59, %37, %35
br label %78
78: ; preds = %77, %15, %13
ret i32 0
}

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