#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
scalar_t__ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int pipe1[2], pipe2[2];
  if (pipe(pipe1) < 0 || pipe(pipe2) < 0) {
    perror("Creating pipes failed!");
  }
  if (fork() == 0) {
    close(1);
    dup(pipe1[1]);
    close(pipe1[0]);
    close(pipe2[0]);
    close(pipe2[1]);
    execlp("ls", "ls", NULL);
    exit(0);
  } else {
    if (fork() == 0) {
      close(0);
      dup(pipe1[0]);
      close(pipe1[1]);
      close(1);
      dup(pipe2[1]);
      close(pipe2[0]);
      execlp("sort", "sort", "-r", NULL);
      exit(0);
    } else {
      if (fork() == 0) {
        close(pipe1[0]);
        close(pipe1[1]);
        close(0);
        dup(pipe2[0]);
        close(pipe2[1]);
        execlp("more", "more", NULL);
        exit(0);
      }
    }
  }
  close(pipe1[0]);
  close(pipe1[1]);
  close(pipe2[0]);
  close(pipe2[1]);
  pid_t pid;
  while (pid = waitpid(-1, NULL, 0)) {
    if (errno == ECHILD) {
      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>
cmp    $0x0,%rax
jl     11bc <main+0x3c>
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11c8 <main+0x48>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    121f <main+0x9f>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <dup@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1284 <main+0x104>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
lea    0xdb2(%rip),%rsi
lea    0xdb0(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12d8 <main+0x158>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xd5f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    12dd <main+0x15d>
jmp    12e2 <main+0x162>
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>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1342 <main+0x1c2>
mov    0x2cbb(%rip),%rax
mov    (%rax),%rax
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%rax
jne    133d <main+0x1bd>
jmp    1342 <main+0x1c2>
jmp    1302 <main+0x182>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     1244 <main+0xc4>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     1244 <main+0xc4>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1255 <main+0xd5>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     128c <main+0x10c>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
test   %rbx,%rbx
je     12e2 <main+0x162>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x2dd5(%rip),%r14
mov    0x2dc6(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
je     123a <main+0xba>
mov    (%r14),%rax
cmp    (%rbx),%rax
jne    1220 <main+0xa0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb5(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <main+0x2d>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1030 <dup@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xd8d(%rip),%rdi
jmp    1302 <main+0x182>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1030 <dup@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xd52(%rip),%rdi
lea    0xd50(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xd20(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1090 <execlp@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Creating pipes failed!\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.3 = [3 x i8] c"-r\00"
@.str.4 = [5 x i8] c"more\00"
@errno = global i64 0
@ECHILD = global i64 0
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 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 i64 @pipe(i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12, %2
%17 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = call i32 @close(i32 1)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @dup(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, 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 0
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = 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* null)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %18
%38 = call i64 (...) @fork()
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = call i32 @close(i32 0)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @dup(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i32 @close(i32 1)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @dup(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 = 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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* null)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %37
%58 = call i64 (...) @fork()
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %76
60: ; preds = %57
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = call i32 @close(i32 0)
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @dup(i32 %69)
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%72 = load i32, i32* %71
%73 = call i32 @close(i32 %72)
%74 = 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* null)
%75 = call i32 @exit(i32 0)
unreachable
76: ; preds = %57
br label %77
77: ; preds = %76
br label %78
78: ; preds = %77
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
%85 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%86 = load i32, i32* %85
%87 = call i32 @close(i32 %86)
%88 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %99, %78
%92 = call i32 @waitpid(i32 -1, i32* null, i32 0)
store i32 %92, i32* %8
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %100
94: ; preds = %91
%95 = load i64, i64* @errno
%96 = load i64, i64* @ECHILD
%97 = icmp eq i64 %95, %96
br i1 %97, label %98, label %99
98: ; preds = %94
br label %100
99: ; preds = %94
br label %91
100: ; preds = %98, %91
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Creating pipes failed!\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.3 = [3 x i8] c"-r\00"
@.str.4 = [5 x i8] c"more\00"
@errno = global i64 0
@ECHILD = global i64 0
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 i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10, %2
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = call i32 @close(i32 1)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @dup(i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %4, 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* null)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %16
%35 = call i64 (...) @fork()
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %53
37: ; preds = %34
%38 = call i32 @close(i32 0)
%39 = load i32, i32* %7
%40 = call i32 @dup(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = call i32 @close(i32 1)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @dup(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = 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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* null)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %34
%54 = call i64 (...) @fork()
%55 = icmp eq i64 %54, 0
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
br i1 %55, label %61, label %71
61: ; preds = %53
%62 = call i32 @close(i32 0)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @dup(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = 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* null)
%70 = call i32 @exit(i32 0)
unreachable
71: ; preds = %53
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%76 = load i32, i32* %75
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %81, %71
%79 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%80 = icmp eq i32 %79, 0
br i1 %80, label %85, label %81
81: ; preds = %78
%82 = load i64, i64* @errno
%83 = load i64, i64* @ECHILD
%84 = icmp eq i64 %82, %83
br i1 %84, label %85, label %78
85: ; preds = %81, %78
ret i32 0
}