#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 scalar_t__ 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*/ execlp(char *, char *, char const *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char const *);
int main(int argc, char const *argv[]) {
  if (argc != 3) {
    printf("Usage: %s <dir> <arg>\n", argv[0]);
    exit(1);
  }
  const char *dir = argv[1];
  const char *arg = argv[2];
  int fd1[2];
  pipe(fd1);
  pid_t pid1 = fork();
  if (pid1 > 0) {
    close(fd1[0]);
    dup2(fd1[1], STDOUT_FILENO);
    execlp("ls", "ls", dir, "-laR", NULL);
  } else {
    int fd2[2];
    pipe(fd2);
    close(fd1[1]);
    pid_t pid2 = fork();
    if (pid2 > 0) {
      close(fd2[0]);
      dup2(fd1[0], STDIN_FILENO);
      dup2(fd2[1], STDOUT_FILENO);
      execlp("grep", "grep", arg, NULL);
    } else {
      close(fd2[1]);
      dup2(fd2[0], STDIN_FILENO);
      execlp("sort", "sort", NULL);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ad <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    1223 <main+0xb3>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rdx
lea    0xe0f(%rip),%rsi
lea    0xe0b(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
jmp    12c5 <main+0x155>
lea    -0x38(%rbp),%rdi
call   1060 <pipe@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jle    1292 <main+0x122>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%rdx
lea    0xda0(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
jmp    12c0 <main+0x150>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xd72(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
jmp    12c5 <main+0x155>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jne    1278 <main+0x108>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    11dc <main+0x6c>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xe57(%rip),%rdi
lea    0xe53(%rip),%rcx
mov    %rdi,%rsi
mov    %rbx,%rdx
xor    %r8d,%r8d
xor    %eax,%eax
call   1080 <execlp@plt>
jmp    126e <main+0xfe>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1240 <main+0xd0>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xdf0(%rip),%rdi
mov    %rdi,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execlp@plt>
jmp    126e <main+0xfe>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xdc2(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsi),%rsi
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: %s <dir> <arg>\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"-laR\00"
@STDIN_FILENO = global i32 0
@.str.3 = [5 x i8] c"grep\00"
@.str.4 = [5 x i8] c"sort\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [2 x i32]
%9 = alloca i64
%10 = alloca [2 x i32]
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
store i8* %26, i8** %7
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%28 = call i32 @pipe(i32* %27)
%29 = call i64 (...) @fork()
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %20
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%37 = load i32, i32* %36
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
%40 = load i8*, i8** %6
%41 = 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* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %75
42: ; preds = %20
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%44 = call i32 @pipe(i32* %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i64 (...) @fork()
store i64 %48, i64* %11
%49 = load i64, i64* %11
%50 = icmp sgt i64 %49, 0
br i1 %50, label %51, label %65
51: ; preds = %42
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %8, 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]* %10, i64 0, i64 1
%60 = load i32, i32* %59
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
%63 = load i8*, i8** %7
%64 = 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* %63, i8* null)
br label %74
65: ; preds = %42
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%70 = load i32, i32* %69
%71 = load i32, i32* @STDIN_FILENO
%72 = call i32 @dup2(i32 %70, i32 %71)
%73 = 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)
br label %74
74: ; preds = %65, %51
br label %75
75: ; preds = %74, %32
ret i32 0
}

LLVM IR (-O2):
@.str = [23 x i8] c"Usage: %s <dir> <arg>\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [5 x i8] c"-laR\00"
@STDIN_FILENO = global i32 0
@.str.3 = [5 x i8] c"grep\00"
@.str.4 = [5 x i8] c"sort\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = bitcast [2 x i32]* %3 to i8*
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i64 (...) @fork()
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %10
%21 = load i32, i32* %16
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = 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* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %57
28: ; preds = %10
%29 = bitcast [2 x i32]* %4 to i8*
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%31 = call i32 @pipe(i32* %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = call i64 (...) @fork()
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %28
%38 = load i32, i32* %30
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %16
%41 = load i32, i32* @STDIN_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @STDOUT_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = 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* %14, i8* null)
br label %56
48: ; preds = %28
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %30
%53 = load i32, i32* @STDIN_FILENO
%54 = call i32 @dup2(i32 %52, i32 %53)
%55 = 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)
br label %56
56: ; preds = %48, %37
br label %57
57: ; preds = %56, %20
ret i32 0
}