#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ STDIN_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
scalar_t__ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char const *);
int strlen(char *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int write(int, char *, int);
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];
  FILE *fp = fopen(filename, "r");
  if (fp == NULL) {
    perror("fopen");
    exit(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]);
    char line[256];
    while (fgets(line, 256, fp) != NULL) {
      int n = strlen(line);
      if (write(fd[1], line, n) != n) {
        perror("write");
        exit(1);
      }
    }
    if (ferror(fp)) {
      perror("ferror");
      exit(1);
    }
    close(fd[1]);
    if (waitpid(pid, NULL, 0) < 0) {
      perror("waitpid");
      exit(1);
    }
    fclose(fp);
    exit(0);
  } 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    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1230 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xddf(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1271 <main+0x81>
lea    0xdb5(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0x0,%rax
jge    129a <main+0xaa>
lea    0xd92(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    12c6 <main+0xd6>
lea    0xd6b(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jle    13b6 <main+0x1c6>
mov    -0x28(%rbp),%edi
call   1090 <close@plt>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   10b0 <fgets@plt>
cmp    $0x0,%rax
je     1346 <main+0x156>
lea    -0x130(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x134(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x134(%rbp),%edx
call   1040 <write@plt>
cmp    -0x134(%rbp),%eax
je     1341 <main+0x151>
lea    0xcf5(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    12d9 <main+0xe9>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     136f <main+0x17f>
lea    0xccd(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    $0x0,%rax
jge    13a6 <main+0x1b6>
lea    0xc9d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2c18(%rip),%rax
mov    (%rax),%rsi
call   1070 <dup2@plt>
mov    0x2c09(%rip),%rcx
cmp    (%rcx),%rax
je     13f6 <main+0x206>
lea    0xc55(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xc44(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1100 <execlp@plt>
cmp    $0x0,%rax
jge    1429 <main+0x239>
lea    0xc2c(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    142e <main+0x23e>
jmp    1433 <main+0x243>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
cmp    $0x2,%edi
jne    129a <main+0xaa>
mov    0x8(%rsi),%rdi
lea    0xe21(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12b3 <main+0xc3>
mov    %rax,%r14
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
js     12bc <main+0xcc>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     12c5 <main+0xd5>
jne    12ce <main+0xde>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2d81(%rip),%rbx
mov    (%rbx),%rsi
call   1070 <dup2@plt>
cmp    (%rbx),%rax
jne    131a <main+0x12a>
lea    0xdca(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1100 <execlp@plt>
test   %rax,%rax
js     1323 <main+0x133>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xd5c(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xd5d(%rip),%rdi
jmp    132a <main+0x13a>
lea    0xd5a(%rip),%rdi
jmp    132a <main+0x13a>
lea    0xd56(%rip),%rdi
jmp    132a <main+0x13a>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%r15
nop
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     1339 <main+0x149>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     12e0 <main+0xf0>
lea    0xd0f(%rip),%rdi
jmp    132a <main+0x13a>
lea    0xd1b(%rip),%rdi
jmp    132a <main+0x13a>
lea    0xd1c(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1369 <main+0x179>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
test   %rax,%rax
jns    1372 <main+0x182>
lea    0xccd(%rip),%rdi
jmp    132a <main+0x13a>
lea    0xcbd(%rip),%rdi
jmp    132a <main+0x13a>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: %s <file_name>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [5 x i8] c"pipe\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [7 x i8] c"ferror\00"
@.str.7 = [8 x i8] c"waitpid\00"
@STDIN_FILENO = global i64 0
@.str.8 = [5 x i8] c"dup2\00"
@.str.9 = [5 x i8] c"sort\00"
@.str.10 = [7 x i8] c"execlp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [2 x i32]
%9 = alloca i64
%10 = alloca [256 x i8]
%11 = alloca i32
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, 2
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** %6
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%33 = call i64 @pipe(i32* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = call i64 (...) @fork()
store i64 %39, i64* %9
%40 = load i64, i64* %9
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
%46 = load i64, i64* %9
%47 = icmp sgt i64 %46, 0
br i1 %47, label %48, label %92
48: ; preds = %45
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %70, %48
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%54 = load i32*, i32** %7
%55 = call i32* @fgets(i8* %53, i32 256, i32* %54)
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %71
57: ; preds = %52
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%59 = call i32 @strlen(i8* %58)
store i32 %59, i32* %11
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%61 = load i32, i32* %60
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%63 = load i32, i32* %11
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
%65 = load i32, i32* %11
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %70
67: ; preds = %57
%68 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %57
br label %52
71: ; preds = %52
%72 = load i32*, i32** %7
%73 = call i64 @ferror(i32* %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %71
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = load i64, i64* %9
%83 = call i64 @waitpid(i64 %82, i32* null, i32 0)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %78
%86 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%87 = call i32 @exit(i32 1)
unreachable
88: ; preds = %78
%89 = load i32*, i32** %7
%90 = call i32 @fclose(i32* %89)
%91 = call i32 @exit(i32 0)
unreachable
92: ; preds = %45
%93 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%94 = load i32, i32* %93
%95 = call i32 @close(i32 %94)
%96 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%97 = load i32, i32* %96
%98 = load i64, i64* @STDIN_FILENO
%99 = call i64 @dup2(i32 %97, i64 %98)
%100 = load i64, i64* @STDIN_FILENO
%101 = icmp ne i64 %99, %100
br i1 %101, label %102, label %105
102: ; preds = %92
%103 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%104 = call i32 @exit(i32 1)
unreachable
105: ; preds = %92
%106 = call i64 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i32* null)
%107 = icmp slt i64 %106, 0
br i1 %107, label %108, label %111
108: ; preds = %105
%109 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
%110 = call i32 @exit(i32 1)
unreachable
111: ; preds = %105
br label %112
112: ; preds = %111
br label %113
113: ; preds = %112
ret i32 0
}

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