#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*/ error(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int fork();
int pipe(int *);
int main(int argc, char **argv) {
  char *dir;
  if (argc == 1)
    dir = NULL;
  else
    dir = argv[1];
  int fd[2], pid;
  if (pipe(fd) == -1)
    error("sortedlist: error creating pipe");
  if ((pid = fork()) < 0)
    error("sortedlist: error while forking");
  if (pid > 0) {
    close(1);
    dup(fd[1]);
    close(fd[0]);
    close(fd[1]);
    execlp("ls", "ls", dir, NULL);
  } else if (pid == 0) {
    close(0);
    dup(fd[0]);
    close(fd[0]);
    close(fd[1]);
    execlp("sort", "sort", "-r", NULL);
  }
  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)
cmpl   $0x1,-0x8(%rbp)
jne    118d <main+0x2d>
movq   $0x0,-0x18(%rbp)
jmp    1199 <main+0x39>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11b7 <main+0x57>
lea    0xe4e(%rip),%rdi
call   1060 <error@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d6 <main+0x76>
lea    0xe4f(%rip),%rdi
call   1060 <error@plt>
cmpl   $0x0,-0x24(%rbp)
jle    121e <main+0xbe>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1030 <dup@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdx
lea    0xe33(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
jmp    1266 <main+0x106>
cmpl   $0x0,-0x24(%rbp)
jne    1261 <main+0x101>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xdf5(%rip),%rsi
lea    0xdf3(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1070 <execlp@plt>
jmp    1266 <main+0x106>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jne    116e <main+0xe>
xor    %ebx,%ebx
jmp    1172 <main+0x12>
mov    0x8(%rsi),%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    118d <main+0x2d>
lea    0xe78(%rip),%rdi
call   1060 <error@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     11ce <main+0x6e>
je     11dc <main+0x7c>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe7a(%rip),%rdi
mov    %rdi,%rsi
mov    %rbx,%rdx
jmp    120f <main+0xaf>
lea    0xe4b(%rip),%rdi
call   1060 <error@plt>
jmp    1216 <main+0xb6>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1030 <dup@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xe3e(%rip),%rdi
lea    0xe3c(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1070 <execlp@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [32 x i8] c"sortedlist: error creating pipe\00"
@.str.1 = [32 x i8] c"sortedlist: error while forking\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [3 x i8] c"-r\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [2 x i32]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %6
br label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
br label %16
16: ; preds = %12, %11
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = call i32 (...) @fork()
store i32 %23, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = call i32 @close(i32 1)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %6
%42 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32* null)
br label %59
43: ; preds = %27
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = call i32 @close(i32 0)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @dup(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = call i32 @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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* null)
br label %58
58: ; preds = %46, %43
br label %59
59: ; preds = %58, %30
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"sortedlist: error creating pipe\00"
@.str.1 = [32 x i8] c"sortedlist: error while forking\00"
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [3 x i8] c"-r\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = icmp eq i32 %0, 1
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
br label %8
8: ; preds = %2, %5
%9 = phi i8* [ %7, %5 ], [ null, %2 ]
%10 = bitcast [2 x i32]* %3 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
%17 = call i32 (...) @fork()
%18 = icmp slt i32 %17, 0
br i1 %18, label %31, label %19
19: ; preds = %16
%20 = icmp eq i32 %17, 0
br i1 %20, label %33, label %21
21: ; preds = %19
%22 = call i32 @close(i32 1)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @dup(i32 %24)
%26 = load i32, i32* %11
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %23
%29 = call i32 @close(i32 %28)
%30 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32* null)
br label %43
31: ; preds = %16
%32 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %43
33: ; preds = %19
%34 = call i32 @close(i32 0)
%35 = load i32, i32* %11
%36 = call i32 @dup(i32 %35)
%37 = load i32, i32* %11
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* null)
br label %43
43: ; preds = %31, %33, %21
ret i32 0
}