#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int main(void) {
  int fd[2];
  int fd2[2];
  pid_t childpid;
  pid_t childpid2;
  pipe(fd2);
  if ((childpid2 = fork() == 0)) {
    pipe(fd);
    if ((childpid = fork()) == 0) {
      dup2(fd[1], STDOUT_FILENO);
      close(fd[0]);
      close(fd[1]);
      execl("/bin/ls", "ls", "-l", NULL);
      perror("The exec of ls failed");
    } else {
      dup2(fd[0], STDIN_FILENO);
      close(fd[0]);
      close(fd[1]);
      dup2(fd2[1], STDOUT_FILENO);
      close(fd2[0]);
      close(fd2[1]);
      execl("/bin/sort", "sort", "-k", "+9", NULL);
      perror("The exec of sort failed");
    }
  } else {
    dup2(fd2[0], STDIN_FILENO);
    close(fd2[0]);
    close(fd2[1]);
    execl("/usr/bin/head", "head", "-5", NULL);
    perror("The exec of head failed");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    11ae <main+0x3e>
jmp    1297 <main+0x127>
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    121c <main+0xac>
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
lea    0xe0e(%rip),%rdi
lea    0xe0c(%rip),%rsi
lea    0xe08(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1080 <execl@plt>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
jmp    1292 <main+0x122>
mov    -0xc(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
lea    0xdbc(%rip),%rdi
lea    0xdba(%rip),%rsi
lea    0xdb8(%rip),%rdx
lea    0xdb4(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x0,%al
call   1080 <execl@plt>
lea    0xda4(%rip),%rdi
call   1060 <perror@plt>
jmp    12e4 <main+0x174>
mov    -0x14(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
lea    0xd8a(%rip),%rdi
lea    0xd8c(%rip),%rsi
lea    0xd8a(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1080 <execl@plt>
lea    0xd7b(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11f1 <main+0x81>
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    123c <main+0xcc>
mov    0x14(%rsp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
lea    0xe32(%rip),%rdi
lea    0xe30(%rip),%rsi
lea    0xe2c(%rip),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execl@plt>
lea    0xe1f(%rip),%rdi
jmp    12b1 <main+0x141>
mov    0x8(%rsp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe2d(%rip),%rdi
lea    0xe2f(%rip),%rsi
lea    0xe2d(%rip),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execl@plt>
lea    0xe20(%rip),%rdi
jmp    12b1 <main+0x141>
mov    0x10(%rsp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xd96(%rip),%rdi
lea    0xd94(%rip),%rsi
lea    0xd92(%rip),%rdx
lea    0xd8e(%rip),%rcx
xor    %r8d,%r8d
xor    %eax,%eax
call   1080 <execl@plt>
lea    0xd80(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@.str.3 = [22 x i8] c"The exec of ls failed\00"
@STDIN_FILENO = global i32 0
@.str.4 = [10 x i8] c"/bin/sort\00"
@.str.5 = [5 x i8] c"sort\00"
@.str.6 = [3 x i8] c"-k\00"
@.str.7 = [3 x i8] c"+9\00"
@.str.8 = [24 x i8] c"The exec of sort failed\00"
@.str.9 = [14 x i8] c"/usr/bin/head\00"
@.str.10 = [5 x i8] c"head\00"
@.str.11 = [3 x i8] c"-5\00"
@.str.12 = [24 x i8] c"The exec of head failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
store i32 %10, i32* %5
br i1 %9, label %11, label %54
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i64 (...) @fork()
%15 = trunc i64 %14 to i32
store i32 %15, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %11
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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.2, i64 0, i64 0), i8* null)
%29 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %53
30: ; preds = %11
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load i32, i32* @STDOUT_FILENO
%44 = call i32 @dup2(i32 %42, i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, 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)
%52 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %53
53: ; preds = %30, %17
br label %67
54: ; preds = %0
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%56 = load i32, i32* %55
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i8* null)
%66 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0))
br label %67
67: ; preds = %54, %53
%68 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@.str.3 = [22 x i8] c"The exec of ls failed\00"
@STDIN_FILENO = global i32 0
@.str.4 = [10 x i8] c"/bin/sort\00"
@.str.5 = [5 x i8] c"sort\00"
@.str.6 = [3 x i8] c"-k\00"
@.str.7 = [3 x i8] c"+9\00"
@.str.8 = [24 x i8] c"The exec of sort failed\00"
@.str.9 = [14 x i8] c"/usr/bin/head\00"
@.str.10 = [5 x i8] c"head\00"
@.str.11 = [3 x i8] c"-5\00"
@.str.12 = [24 x i8] c"The exec of head failed\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]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %43
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i64 (...) @fork()
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %9
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = load i32, i32* %10
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %16
%23 = call i32 @close(i32 %22)
%24 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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.2, i64 0, i64 0), i8* null)
br label %53
25: ; preds = %9
%26 = load i32, i32* %10
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = load i32, i32* %10
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %34
%41 = call i32 @close(i32 %40)
%42 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, 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 %53
43: ; preds = %0
%44 = load i32, i32* %5
%45 = load i32, i32* @STDIN_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i8* null)
br label %53
53: ; preds = %15, %25, %43
%54 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), %25 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), %43 ]
%55 = call i32 @perror(i8* %54)
%56 = call i32 @exit(i32 0)
unreachable
}