#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int close(int);
int dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int main(int argc, char *argv[]) {
  int p[2];
  pid_t child;
  if (pipe(p) == -1) {
    do {
      perror("pipe creation");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if ((child = fork()) == -1) {
    do {
      perror("fork");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (child) {
    if (dup2(p[0], 0) == -1) {
      do {
        perror("dup2");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (close(p[1]) == -1) {
      do {
        perror("close");
        exit(EXIT_FAILURE);
      } while (0);
    }
    execlp("sort", "sort", "-r", NULL);
  } else {
    if (dup2(p[1], 1) == -1) {
      do {
        perror("dup2");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (close(p[0]) == -1) {
      do {
        perror("close");
        exit(EXIT_FAILURE);
      } while (0);
    }
    execlp("ls", "ls", NULL);
  }
  return EXIT_SUCCESS;
}


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)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11b7 <main+0x47>
jmp    119d <main+0x2d>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11e9 <main+0x79>
jmp    11cf <main+0x5f>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1276 <main+0x106>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1225 <main+0xb5>
jmp    120b <main+0x9b>
lea    0xe01(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1255 <main+0xe5>
jmp    123b <main+0xcb>
lea    0xdd6(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdc2(%rip),%rsi
lea    0xdc0(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
jmp    12f0 <main+0x180>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0x13b>
jmp    1291 <main+0x121>
lea    0xd7b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12db <main+0x16b>
jmp    12c1 <main+0x151>
lea    0xd50(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xd44(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1080 <execlp@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    %rsp,%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x9d>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11cf <main+0x5f>
cmp    $0xffffffff,%eax
je     1228 <main+0xb8>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1216 <main+0xa6>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     121f <main+0xaf>
lea    0xe64(%rip),%rdi
lea    0xe62(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execlp@plt>
jmp    1202 <main+0x92>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1216 <main+0xa6>
mov    (%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     121f <main+0xaf>
lea    0xe30(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xdec(%rip),%rdi
jmp    122f <main+0xbf>
lea    0xdf6(%rip),%rdi
jmp    122f <main+0xbf>
lea    0xdf2(%rip),%rdi
jmp    122f <main+0xbf>
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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