#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 */
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
scalar_t__ execlp(char *, char *, char const *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char const *);
int main(int argc, char const *argv[]) {
  if (argc != 2) {
    printf("Usage: %s <file_name>\n", argv[0]);
    exit(1);
  }
  const char *filename = argv[1];
  int fd[2];
  if (pipe(fd) < 0) {
    perror("pipe");
    exit(1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  } else if (pid > 0) {
    close(fd[0]);
    if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
      perror("dup2");
      exit(1);
    }
    if (execlp("cat", "cat", filename, NULL) < 0) {
      perror("execlp");
      exit(1);
    }
  } else {
    close(fd[1]);
    if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) {
      perror("dup2");
      exit(1);
    }
    if (execlp("sort", "sort", NULL) < 0) {
      perror("execlp");
      exit(1);
    }
  }
  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   $0x2,-0x8(%rbp)
je     11bd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11f2 <main+0x72>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    121e <main+0x9e>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jle    12a7 <main+0x127>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
je     1269 <main+0xe9>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdx
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
cmp    $0x0,%rax
jge    12a2 <main+0x122>
lea    0xd97(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1321 <main+0x1a1>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%rax
je     12e7 <main+0x167>
lea    0xd49(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd43(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
mov    $0x0,%al
call   1090 <execlp@plt>
cmp    $0x0,%rax
jge    131c <main+0x19c>
lea    0xd1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1321 <main+0x1a1>
jmp    1326 <main+0x1a6>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    123e <main+0xbe>
mov    0x8(%rsi),%r14
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1257 <main+0xd7>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1260 <main+0xe0>
je     11fd <main+0x7d>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2e29(%rip),%rbx
mov    (%rbx),%rsi
call   1030 <dup2@plt>
cmp    (%rbx),%rax
jne    1269 <main+0xe9>
lea    0xe47(%rip),%rdi
mov    %rdi,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1090 <execlp@plt>
test   %rax,%rax
js     1235 <main+0xb5>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
mov    0x2dc8(%rip),%rbx
mov    (%rbx),%rsi
call   1030 <dup2@plt>
cmp    (%rbx),%rax
jne    1269 <main+0xe9>
lea    0xe0d(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1090 <execlp@plt>
test   %rax,%rax
jns    11f3 <main+0x73>
lea    0xdee(%rip),%rdi
jmp    1270 <main+0xf0>
mov    (%rsi),%rsi
lea    0xdb8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdb9(%rip),%rdi
jmp    1270 <main+0xf0>
lea    0xdb5(%rip),%rdi
jmp    1270 <main+0xf0>
lea    0xdb1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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