#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 close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  pid_t childpid;
  int fd[2];
  if (argc != 2)
    usage(argv[0]);
  if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
    perror("Failed to setup pipeline");
    exit(1);
  }
  if (childpid == 0) {
    if (dup2(fd[1], STDOUT_FILENO) == -1)
      perror("Failed to redirect stdout of ls");
    else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
      perror("Failed to close extra pipe"
             " descriptors on ls");
    else {
      execl("/bin/ls", "ls", "-l", NULL);
      perror("Failed to exec ls");
    }
  } else {
    if (dup2(fd[0], STDIN_FILENO) == -1)
      perror("Failed to redirect stdin of sort");
    else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
      perror("Failed to close extra pipe"
             " file descriptors on sort");
    else {
      execl("/bin/grep", "grep", argv[1], "--color", NULL);
      perror("Failed to exec sort");
    }
  }
  exit(0);
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x2c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x51>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e7 <main+0x67>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1285 <main+0x105>
mov    -0x18(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x9c>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
jmp    1280 <main+0x100>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     123e <main+0xbe>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xcf>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
jmp    127b <main+0xfb>
lea    0xe10(%rip),%rdi
lea    0xe23(%rip),%rsi
lea    0xe0a(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1090 <execl@plt>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
jmp    1280 <main+0x100>
jmp    1322 <main+0x1a2>
mov    -0x1c(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0x130>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
jmp    131d <main+0x19d>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     12d2 <main+0x152>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12e3 <main+0x163>
lea    0xdcb(%rip),%rdi
call   1070 <perror@plt>
jmp    1318 <main+0x198>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xde6(%rip),%rdi
lea    0xde4(%rip),%rsi
lea    0xde2(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x0,%al
call   1090 <execl@plt>
lea    0xdd7(%rip),%rdi
call   1070 <perror@plt>
jmp    131d <main+0x19d>
jmp    1322 <main+0x1a2>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1194 <main+0x14>
mov    (%rbx),%rdi
call   1060 <usage@plt>
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11b1 <main+0x31>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11c7 <main+0x47>
cmp    $0xffffffff,%eax
jne    11ea <main+0x6a>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x4(%rsp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    120c <main+0x8c>
lea    0xe34(%rip),%r14
jmp    12a5 <main+0x125>
mov    (%rsp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1259 <main+0xd9>
lea    0xe7c(%rip),%r14
jmp    12a5 <main+0x125>
mov    (%rsp),%edi
call   1040 <close@plt>
lea    0xe1e(%rip),%r14
cmp    $0xffffffff,%eax
je     12a5 <main+0x125>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     12a5 <main+0x125>
lea    0xe2d(%rip),%rdi
lea    0xe40(%rip),%rsi
lea    0xe27(%rip),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <execl@plt>
lea    0xe1a(%rip),%r14
jmp    12a5 <main+0x125>
mov    (%rsp),%edi
call   1040 <close@plt>
lea    0xe3c(%rip),%r14
cmp    $0xffffffff,%eax
je     12a5 <main+0x125>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     12a5 <main+0x125>
mov    0x8(%rbx),%rdx
lea    0xe52(%rip),%rdi
lea    0xe50(%rip),%rsi
lea    0xe4e(%rip),%rcx
xor    %r8d,%r8d
xor    %eax,%eax
call   1090 <execl@plt>
lea    0xe45(%rip),%r14
mov    %r14,%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Failed to setup pipeline\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [32 x i8] c"Failed to redirect stdout of ls\00"
@.str.2 = [45 x i8] c"Failed to close extra pipe descriptors on ls\00"
@.str.3 = [8 x i8] c"/bin/ls\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [3 x i8] c"-l\00"
@.str.6 = [18 x i8] c"Failed to exec ls\00"
@STDIN_FILENO = global i32 0
@.str.7 = [33 x i8] c"Failed to redirect stdin of sort\00"
@.str.8 = [52 x i8] c"Failed to close extra pipe file descriptors on sort\00"
@.str.9 = [10 x i8] c"/bin/grep\00"
@.str.10 = [5 x i8] c"grep\00"
@.str.11 = [8 x i8] c"--color\00"
@.str.12 = [20 x i8] c"Failed to exec sort\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @usage(i8* %13)
br label %15
15: ; preds = %10, %2
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 (...) @fork()
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %19, %15
%23 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %53
28: ; preds = %25
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %52
36: ; preds = %28
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %46, label %41
41: ; preds = %36
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41, %36
%47 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %51
48: ; preds = %41
%49 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 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)
%50 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %48, %46
br label %52
52: ; preds = %51, %34
br label %81
53: ; preds = %25
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%55 = load i32, i32* %54
%56 = load i32, i32* @STDIN_FILENO
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %53
%60 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %80
61: ; preds = %53
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %71, label %66
66: ; preds = %61
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %66, %61
%72 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.8, i64 0, i64 0))
br label %79
73: ; preds = %66
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = call i32 (i8*, i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* %76, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0), i8* null)
%78 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
br label %79
79: ; preds = %73, %71
br label %80
80: ; preds = %79, %59
br label %81
81: ; preds = %80, %52
%82 = call i32 @exit(i32 0)
unreachable
}

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