#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*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printUsage(char const **);
int readFile(char const *, char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char const *argv[]) {
  if (argc != 2) {
    printUsage(argv);
    exit(1);
  }
  int pipefd[2];
  if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  } else if (pid == 0) {
    close(pipefd[1]);
    dup2(pipefd[0], STDIN_FILENO);
    execlp("sort", "sort", NULL);
  } else if (pid > 0) {
    close(pipefd[0]);
    char *contents;
    int size = readFile(argv[1], &contents);
    if (size == 0) {
      free(contents);
      fprintf(stderr, "Error reading file.\n");
      exit(1);
    }
    write(pipefd[1], contents, size * sizeof(char));
    close(pipefd[1]);
    free(contents);
    wait(NULL);
  }
  exit(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     1203 <main+0x33>
mov    -0x10(%rbp),%rdi
call   1090 <printUsage@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    122b <main+0x5b>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1257 <main+0x87>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1293 <main+0xc3>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
lea    0xd88(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10d0 <execlp@plt>
jmp    1321 <main+0x151>
cmpq   $0x0,-0x20(%rbp)
jle    131c <main+0x14c>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <readFile@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12ec <main+0x11c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
jmp    1321 <main+0x151>
jmp    1326 <main+0x156>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11f3 <main+0x23>
mov    %rsp,%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1205 <main+0x35>
lea    0xe0f(%rip),%rdi
jmp    1218 <main+0x48>
mov    %rbx,%rdi
call   1090 <printUsage@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jns    1227 <main+0x57>
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jne    125b <main+0x8b>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
lea    0xdc0(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10d0 <execlp@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   10c0 <readFile@plt>
test   %eax,%eax
jne    129e <main+0xce>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x4(%rsp),%edi
mov    0x8(%rsp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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

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