#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 */
char EOF;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
char fscanf(int /*<<< orphan*/ *, char *, char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc != 4) {
    printf("Usage: %s <filename>.txt <chapter> <words>.txt\n", argv[0]);
    exit(1);
  }
  FILE *wordsFile;
  wordsFile = fopen(argv[3], "r");
  if (!wordsFile) {
    fprintf(stderr, "%s could not be found\n", argv[3]);
    exit(2);
  }
  char tempFileName[80];
  strcpy(tempFileName, "./temp/");
  strcat(tempFileName, argv[2]);
  strcat(tempFileName, "_temp.txt");
  FILE *tempF = fopen(tempFileName, "w");
  fprintf(stdout, "\tsw - Reading '%s' and writing chapter %s words to '%s'\n",
          argv[1], argv[2], tempFileName);
  int catGrepPipe[2];
  int grepSortPipe[2];
  char word[100];
  char c;
  pid_t pid;
  while ((c = fscanf(wordsFile, "%s", word)) != EOF) {
    if (pipe(catGrepPipe) == -1) {
      fprintf(stderr, "Couldn't pipe catGrepPipe\n");
      exit(4);
    }
    if (pipe(grepSortPipe) == -1) {
      fprintf(stderr, "Couldn't pipe grepSortPipe\n");
      exit(4);
    }
    pid = fork();
    if (pid == 0) {
      dup2(grepSortPipe[1], STDOUT_FILENO);
      dup2(catGrepPipe[0], STDIN_FILENO);
      close(catGrepPipe[0]);
      close(catGrepPipe[1]);
      close(grepSortPipe[0]);
      close(grepSortPipe[1]);
      execlp("grep", "grep", "-no", word, NULL);
      fprintf(stderr, "-----grep not executed !\n");
      exit(3);
    }
    pid = fork();
    if (pid == 0) {
      dup2(catGrepPipe[1], STDOUT_FILENO);
      close(catGrepPipe[0]);
      close(catGrepPipe[1]);
      execlp("cat", "cat", argv[1], NULL);
      fprintf(stderr, "-----cat not executed !\n");
      exit(3);
    }
    close(catGrepPipe[0]);
    close(catGrepPipe[1]);
    wait(NULL);
    wait(NULL);
    pid = fork();
    if (pid == 0) {
      close(grepSortPipe[1]);
      char c;
      int firstNumber = 1;
      setbuf(stdout, NULL);
      while (read(grepSortPipe[0], &c, sizeof(char))) {
        if (c >= '0' && c <= '9') {
          if (firstNumber) {
            fprintf(tempF, "%s:%s-", word, argv[2]);
            firstNumber = 0;
          }
          fprintf(tempF, "%c", c);
        } else if (c == ':') {
          fprintf(tempF, "#\n");
          firstNumber = 1;
        }
      }
      close(grepSortPipe[0]);
      fclose(tempF);
      exit(0);
    }
    close(grepSortPipe[0]);
    close(grepSortPipe[1]);
    wait(NULL);
  }
  fclose(wordsFile);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1240 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdcf(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xde1(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128d <main+0x8d>
mov    0x2d66(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
lea    -0x70(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   10e0 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x78(%rbp)
mov    0x2cef(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    -0x70(%rbp),%r8
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0xf0(%rbp),%rdx
lea    0xd86(%rip),%rsi
call   1080 <fscanf@plt>
mov    %al,-0xf1(%rbp)
movsbl %al,%eax
mov    0x2cbb(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1635 <main+0x435>
lea    -0x80(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1364 <main+0x164>
mov    0x2c87(%rip),%rax
mov    (%rax),%rdi
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x4,%edi
call   10f0 <exit@plt>
lea    -0x88(%rbp),%rdi
call   10a0 <pipe@plt>
cmp    $0xffffffff,%eax
jne    139b <main+0x19b>
mov    0x2c50(%rip),%rax
mov    (%rax),%rdi
lea    0xd2a(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x4,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
jne    1448 <main+0x248>
mov    -0x84(%rbp),%edi
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x80(%rbp),%edi
mov    0x2beb(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x80(%rbp),%edi
call   1090 <close@plt>
mov    -0x7c(%rbp),%edi
call   1090 <close@plt>
mov    -0x88(%rbp),%edi
call   1090 <close@plt>
mov    -0x84(%rbp),%edi
call   1090 <close@plt>
lea    -0xf0(%rbp),%rcx
lea    0xcc0(%rip),%rsi
lea    0xcbe(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1100 <execlp@plt>
mov    0x2ba3(%rip),%rax
mov    (%rax),%rdi
lea    0xca2(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x3,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
jne    14c4 <main+0x2c4>
mov    -0x7c(%rbp),%edi
mov    0x2b82(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x80(%rbp),%edi
call   1090 <close@plt>
mov    -0x7c(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xc5f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1100 <execlp@plt>
mov    0x2b27(%rip),%rax
mov    (%rax),%rdi
lea    0xc44(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x3,%edi
call   10f0 <exit@plt>
mov    -0x80(%rbp),%edi
call   1090 <close@plt>
mov    -0x7c(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
jne    1611 <main+0x411>
mov    -0x84(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x108(%rbp)
mov    0x2aaa(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    -0x88(%rbp),%edi
lea    -0x101(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
cmp    $0x0,%rax
je     15f6 <main+0x3f6>
movsbl -0x101(%rbp),%eax
cmp    $0x30,%eax
jl     15c0 <main+0x3c0>
movsbl -0x101(%rbp),%eax
cmp    $0x39,%eax
jg     15c0 <main+0x3c0>
cmpl   $0x0,-0x108(%rbp)
je     15a2 <main+0x3a2>
mov    -0x78(%rbp),%rdi
lea    -0xf0(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xb7f(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
movl   $0x0,-0x108(%rbp)
mov    -0x78(%rbp),%rdi
movsbl -0x101(%rbp),%edx
lea    0xb63(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
jmp    15f1 <main+0x3f1>
movsbl -0x101(%rbp),%eax
cmp    $0x3a,%eax
jne    15ec <main+0x3ec>
mov    -0x78(%rbp),%rdi
lea    0xb3f(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
movl   $0x1,-0x108(%rbp)
jmp    15f1 <main+0x3f1>
jmp    1529 <main+0x329>
mov    -0x88(%rbp),%edi
call   1090 <close@plt>
mov    -0x78(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x88(%rbp),%edi
call   1090 <close@plt>
mov    -0x84(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
jmp    12fe <main+0xfe>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
mov    %rsi,%rbp
cmp    $0x4,%edi
jne    1274 <main+0x54>
mov    0x18(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
jne    128e <main+0x6e>
mov    0x2d7b(%rip),%rax
mov    (%rax),%rdi
mov    0x18(%rbp),%rdx
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
mov    0x0(%rbp),%rsi
lea    0xd81(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    %rax,%rbx
lea    0xdb1(%rip),%rsi
lea    0xa0(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    0x10(%rbp),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
lea    0xd96(%rip),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
lea    0xd91(%rip),%rsi
mov    %r14,%rdi
call   10e0 <fopen@plt>
mov    %rax,0x20(%rsp)
mov    0x2cea(%rip),%rax
movslq (%rax),%rdi
mov    0x8(%rbp),%rdx
mov    %rbp,0x28(%rsp)
mov    0x10(%rbp),%rcx
lea    0xd68(%rip),%rsi
mov    %r14,%r8
xor    %eax,%eax
call   10d0 <fprintf@plt>
lea    0xd90(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <fscanf@plt>
mov    0x2cc6(%rip),%r15
cmp    (%r15),%al
jne    132e <main+0x10e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xd57(%rip),%rbp
lea    0x30(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   10a0 <pipe@plt>
cmp    $0xffffffff,%eax
je     13f7 <main+0x1d7>
mov    %r13,%rdi
call   10a0 <pipe@plt>
cmp    $0xffffffff,%eax
je     140f <main+0x1ef>
xor    %eax,%eax
call   1140 <fork@plt>
test   %rax,%rax
je     1439 <main+0x219>
xor    %eax,%eax
call   1140 <fork@plt>
test   %rax,%rax
je     14b9 <main+0x299>
mov    0x18(%rsp),%edi
call   1090 <close@plt>
mov    0x1c(%rsp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1130 <wait@plt>
xor    %edi,%edi
call   1130 <wait@plt>
xor    %eax,%eax
call   1140 <fork@plt>
test   %rax,%rax
je     1523 <main+0x303>
mov    0x10(%rsp),%edi
call   1090 <close@plt>
mov    0x14(%rsp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1130 <wait@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1080 <fscanf@plt>
cmp    (%r15),%al
jne    1350 <main+0x130>
jmp    131f <main+0xff>
mov    0x2bd2(%rip),%rax
mov    (%rax),%rcx
lea    0xc91(%rip),%rdi
mov    $0x1a,%esi
jmp    1425 <main+0x205>
mov    0x2bba(%rip),%rax
mov    (%rax),%rcx
lea    0xc94(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0x4,%edi
call   1100 <exit@plt>
mov    0x14(%rsp),%edi
mov    0x2bac(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x18(%rsp),%edi
mov    0x2b6a(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x18(%rsp),%edi
call   1090 <close@plt>
mov    0x1c(%rsp),%edi
call   1090 <close@plt>
mov    0x10(%rsp),%edi
call   1090 <close@plt>
mov    0x14(%rsp),%edi
call   1090 <close@plt>
lea    0xc48(%rip),%rdi
lea    0xc46(%rip),%rdx
lea    0x30(%rsp),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
xor    %eax,%eax
call   1120 <execlp@plt>
mov    0x2b28(%rip),%rax
mov    (%rax),%rcx
lea    0xc27(%rip),%rdi
mov    $0x19,%esi
jmp    150f <main+0x2ef>
mov    0x1c(%rsp),%edi
mov    0x2b2c(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x18(%rsp),%edi
call   1090 <close@plt>
mov    0x1c(%rsp),%edi
call   1090 <close@plt>
mov    0x28(%rsp),%rax
mov    0x8(%rax),%rdx
lea    0xc06(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1120 <execlp@plt>
mov    0x2ad0(%rip),%rax
mov    (%rax),%rcx
lea    0xbed(%rip),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0x3,%edi
call   1100 <exit@plt>
mov    0x14(%rsp),%edi
call   1090 <close@plt>
mov    0x2a95(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%ebx
mov    $0x1,%edx
call   10c0 <read@plt>
test   %rax,%rax
jne    156c <main+0x34c>
mov    0x10(%rsp),%edi
call   1090 <close@plt>
mov    0x20(%rsp),%rdi
jmp    1322 <main+0x102>
lea    0xb9d(%rip),%r14
lea    0x30(%rsp),%r13
lea    0xf(%rsp),%r15
lea    0xb93(%rip),%r12
jmp    15b2 <main+0x392>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <fputc@plt>
xor    %ebx,%ebx
mov    0x10(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   10c0 <read@plt>
test   %rax,%rax
je     1559 <main+0x339>
movzbl 0xf(%rsp),%ebp
lea    -0x30(%rbp),%eax
cmp    $0x9,%al
ja     15f0 <main+0x3d0>
test   %ebx,%ebx
mov    0x20(%rsp),%rbx
je     1590 <main+0x370>
mov    0x28(%rsp),%rax
mov    0x10(%rax),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   10d0 <fprintf@plt>
jmp    1590 <main+0x370>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x3a,%bpl
jne    159c <main+0x37c>
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    0x20(%rsp),%rcx
call   1110 <fwrite@plt>
mov    $0x1,%ebx
jmp    159c <main+0x37c>

LLVM IR (-O0):
@.str = [48 x i8] c"Usage: %s <filename>.txt <chapter> <words>.txt\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.2 = [23 x i8] c"%s could not be found\0A\00"
@.str.3 = [8 x i8] c"./temp/\00"
@.str.4 = [10 x i8] c"_temp.txt\00"
@.str.5 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.6 = [57 x i8] c"\09sw - Reading '%s' and writing chapter %s words to '%s'\0A\00"
@.str.7 = [3 x i8] c"%s\00"
@EOF = global i8 0
@.str.8 = [27 x i8] c"Couldn't pipe catGrepPipe\0A\00"
@.str.9 = [28 x i8] c"Couldn't pipe grepSortPipe\0A\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.10 = [5 x i8] c"grep\00"
@.str.11 = [4 x i8] c"-no\00"
@.str.12 = [26 x i8] c"-----grep not executed !\0A\00"
@.str.13 = [4 x i8] c"cat\00"
@.str.14 = [25 x i8] c"-----cat not executed !\0A\00"
@.str.15 = [7 x i8] c"%s:%s-\00"
@.str.16 = [3 x i8] c"%c\00"
@.str.17 = [3 x i8] c"#\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [80 x i8]
%8 = alloca i32*
%9 = alloca [2 x i32]
%10 = alloca [2 x i32]
%11 = alloca [100 x i8]
%12 = alloca i8
%13 = alloca i64
%14 = alloca i8
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 3
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp ne i32* %29, null
br i1 %30, label %38, label %31
31: ; preds = %24
%32 = load i32*, i32** @stderr
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %24
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%40 = call i32 @strcpy(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%41 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32 @strcat(i8* %41, i8* %44)
%46 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%47 = call i32 @strcat(i8* %46, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %49, i32** %8
%50 = load i32, i32* @stdout
%51 = sext i32 %50 to i64
%52 = inttoptr i64 %51 to i32*
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0), i8* %55, i8* %58, i8* %59)
br label %61
61: ; preds = %199, %38
%62 = load i32*, i32** %6
%63 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%64 = call signext i8 @fscanf(i32* %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %63)
store i8 %64, i8* %12
%65 = sext i8 %64 to i32
%66 = load i8, i8* @EOF
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %65, %67
br i1 %68, label %69, label %207
69: ; preds = %61
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%71 = call i32 @pipe(i32* %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %77
73: ; preds = %69
%74 = load i32*, i32** @stderr
%75 = call i32 (i32*, i8*, ...) @fprintf(i32* %74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
%76 = call i32 @exit(i32 4)
unreachable
77: ; preds = %69
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%79 = call i32 @pipe(i32* %78)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %85
81: ; preds = %77
%82 = load i32*, i32** @stderr
%83 = call i32 (i32*, i8*, ...) @fprintf(i32* %82, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
%84 = call i32 @exit(i32 4)
unreachable
85: ; preds = %77
%86 = call i64 (...) @fork()
store i64 %86, i64* %13
%87 = load i64, i64* %13
%88 = icmp eq i64 %87, 0
br i1 %88, label %89, label %115
89: ; preds = %85
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%91 = load i32, i32* %90
%92 = load i32, i32* @STDOUT_FILENO
%93 = call i32 @dup2(i32 %91, i32 %92)
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%95 = load i32, i32* %94
%96 = load i32, i32* @STDIN_FILENO
%97 = call i32 @dup2(i32 %95, i32 %96)
%98 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%99 = load i32, i32* %98
%100 = call i32 @close(i32 %99)
%101 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%102 = load i32, i32* %101
%103 = call i32 @close(i32 %102)
%104 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%105 = load i32, i32* %104
%106 = call i32 @close(i32 %105)
%107 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%108 = load i32, i32* %107
%109 = call i32 @close(i32 %108)
%110 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%111 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %110, i8* null)
%112 = load i32*, i32** @stderr
%113 = call i32 (i32*, i8*, ...) @fprintf(i32* %112, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.12, i64 0, i64 0))
%114 = call i32 @exit(i32 3)
unreachable
115: ; preds = %85
%116 = call i64 (...) @fork()
store i64 %116, i64* %13
%117 = load i64, i64* %13
%118 = icmp eq i64 %117, 0
br i1 %118, label %119, label %137
119: ; preds = %115
%120 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%121 = load i32, i32* %120
%122 = load i32, i32* @STDOUT_FILENO
%123 = call i32 @dup2(i32 %121, i32 %122)
%124 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%125 = load i32, i32* %124
%126 = call i32 @close(i32 %125)
%127 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%128 = load i32, i32* %127
%129 = call i32 @close(i32 %128)
%130 = load i8**, i8*** %5
%131 = getelementptr inbounds i8*, i8** %130, i64 1
%132 = load i8*, i8** %131
%133 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0), i8* %132, i8* null)
%134 = load i32*, i32** @stderr
%135 = call i32 (i32*, i8*, ...) @fprintf(i32* %134, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.14, i64 0, i64 0))
%136 = call i32 @exit(i32 3)
unreachable
137: ; preds = %115
%138 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%139 = load i32, i32* %138
%140 = call i32 @close(i32 %139)
%141 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%142 = load i32, i32* %141
%143 = call i32 @close(i32 %142)
%144 = call i32 @wait(i32* null)
%145 = call i32 @wait(i32* null)
%146 = call i64 (...) @fork()
store i64 %146, i64* %13
%147 = load i64, i64* %13
%148 = icmp eq i64 %147, 0
br i1 %148, label %149, label %199
149: ; preds = %137
%150 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%151 = load i32, i32* %150
%152 = call i32 @close(i32 %151)
store i32 1, i32* %15
%153 = load i32, i32* @stdout
%154 = call i32 @setbuf(i32 %153, i32* null)
br label %155
155: ; preds = %191, %149
%156 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%157 = load i32, i32* %156
%158 = call i64 @read(i32 %157, i8* %14, i32 1)
%159 = icmp ne i64 %158, 0
br i1 %159, label %160, label %192
160: ; preds = %155
%161 = load i8, i8* %14
%162 = sext i8 %161 to i32
%163 = icmp sge i32 %162, 48
br i1 %163, label %164, label %183
164: ; preds = %160
%165 = load i8, i8* %14
%166 = sext i8 %165 to i32
%167 = icmp sle i32 %166, 57
br i1 %167, label %168, label %183
168: ; preds = %164
%169 = load i32, i32* %15
%170 = icmp ne i32 %169, 0
br i1 %170, label %171, label %178
171: ; preds = %168
%172 = load i32*, i32** %8
%173 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%174 = load i8**, i8*** %5
%175 = getelementptr inbounds i8*, i8** %174, i64 2
%176 = load i8*, i8** %175
%177 = call i32 (i32*, i8*, ...) @fprintf(i32* %172, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0), i8* %173, i8* %176)
store i32 0, i32* %15
br label %178
178: ; preds = %171, %168
%179 = load i32*, i32** %8
%180 = load i8, i8* %14
%181 = sext i8 %180 to i32
%182 = call i32 (i32*, i8*, ...) @fprintf(i32* %179, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32 %181)
br label %191
183: ; preds = %164, %160
%184 = load i8, i8* %14
%185 = sext i8 %184 to i32
%186 = icmp eq i32 %185, 58
br i1 %186, label %187, label %190
187: ; preds = %183
%188 = load i32*, i32** %8
%189 = call i32 (i32*, i8*, ...) @fprintf(i32* %188, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.17, i64 0, i64 0))
store i32 1, i32* %15
br label %190
190: ; preds = %187, %183
br label %191
191: ; preds = %190, %178
br label %155
192: ; preds = %155
%193 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%194 = load i32, i32* %193
%195 = call i32 @close(i32 %194)
%196 = load i32*, i32** %8
%197 = call i32 @fclose(i32* %196)
%198 = call i32 @exit(i32 0)
unreachable
199: ; preds = %137
%200 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%201 = load i32, i32* %200
%202 = call i32 @close(i32 %201)
%203 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%204 = load i32, i32* %203
%205 = call i32 @close(i32 %204)
%206 = call i32 @wait(i32* null)
br label %61
207: ; preds = %61
%208 = load i32*, i32** %6
%209 = call i32 @fclose(i32* %208)
%210 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [48 x i8] c"Usage: %s <filename>.txt <chapter> <words>.txt\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.2 = [23 x i8] c"%s could not be found\0A\00"
@.str.3 = [8 x i8] c"./temp/\00"
@.str.4 = [10 x i8] c"_temp.txt\00"
@.str.5 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.6 = [57 x i8] c"\09sw - Reading '%s' and writing chapter %s words to '%s'\0A\00"
@.str.7 = [3 x i8] c"%s\00"
@EOF = global i8 0
@.str.8 = [27 x i8] c"Couldn't pipe catGrepPipe\0A\00"
@.str.9 = [28 x i8] c"Couldn't pipe grepSortPipe\0A\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.10 = [5 x i8] c"grep\00"
@.str.11 = [4 x i8] c"-no\00"
@.str.12 = [26 x i8] c"-----grep not executed !\0A\00"
@.str.13 = [4 x i8] c"cat\00"
@.str.14 = [25 x i8] c"-----cat not executed !\0A\00"
@.str.15 = [7 x i8] c"%s:%s-\00"
@.str.17 = [3 x i8] c"#\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = alloca [100 x i8]
%7 = alloca i8
%8 = icmp eq i32 %0, 4
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32*, i32** @stderr
%20 = load i8*, i8** %14
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %13
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strcat(i8* %24, i8* %27)
%29 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @stdout
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i32*
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = load i8*, i8** %26
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0), i8* %35, i8* %36, i8* %24)
%38 = bitcast [2 x i32]* %4 to i8*
%39 = bitcast [2 x i32]* %5 to i8*
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%41 = call signext i8 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %40)
%42 = load i8, i8* @EOF
%43 = icmp eq i8 %41, %42
br i1 %43, label %154, label %44
44: ; preds = %23
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
br label %49
49: ; preds = %44, %145
%50 = call i32 @pipe(i32* %45)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32*, i32** @stderr
%54 = call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i64 26, i64 1, i32* %53)
%55 = call i32 @exit(i32 4)
unreachable
56: ; preds = %49
%57 = call i32 @pipe(i32* %46)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i32*, i32** @stderr
%61 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i64 27, i64 1, i32* %60)
%62 = call i32 @exit(i32 4)
unreachable
63: ; preds = %56
%64 = call i64 (...) @fork()
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %85
66: ; preds = %63
%67 = load i32, i32* %48
%68 = load i32, i32* @STDOUT_FILENO
%69 = call i32 @dup2(i32 %67, i32 %68)
%70 = load i32, i32* %45
%71 = load i32, i32* @STDIN_FILENO
%72 = call i32 @dup2(i32 %70, i32 %71)
%73 = load i32, i32* %45
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %47
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %46
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %48
%80 = call i32 @close(i32 %79)
%81 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %40, i8* null)
%82 = load i32*, i32** @stderr
%83 = call i64 @fwrite(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.12, i64 0, i64 0), i64 25, i64 1, i32* %82)
%84 = call i32 @exit(i32 3)
unreachable
85: ; preds = %63
%86 = call i64 (...) @fork()
%87 = icmp eq i64 %86, 0
br i1 %87, label %88, label %101
88: ; preds = %85
%89 = load i32, i32* %47
%90 = load i32, i32* @STDOUT_FILENO
%91 = call i32 @dup2(i32 %89, i32 %90)
%92 = load i32, i32* %45
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %47
%95 = call i32 @close(i32 %94)
%96 = load i8*, i8** %34
%97 = call i32 (i8*, i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.13, i64 0, i64 0), i8* %96, i8* null)
%98 = load i32*, i32** @stderr
%99 = call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.14, i64 0, i64 0), i64 24, i64 1, i32* %98)
%100 = call i32 @exit(i32 3)
unreachable
101: ; preds = %85
%102 = load i32, i32* %45
%103 = call i32 @close(i32 %102)
%104 = load i32, i32* %47
%105 = call i32 @close(i32 %104)
%106 = call i32 @wait(i32* null)
%107 = call i32 @wait(i32* null)
%108 = call i64 (...) @fork()
%109 = icmp eq i64 %108, 0
br i1 %109, label %110, label %145
110: ; preds = %101
%111 = load i32, i32* %48
%112 = call i32 @close(i32 %111)
%113 = load i32, i32* @stdout
%114 = call i32 @setbuf(i32 %113, i32* null)
%115 = load i32, i32* %46
%116 = call i64 @read(i32 %115, i8* %7, i32 1)
%117 = icmp eq i64 %116, 0
br i1 %117, label %140, label %118
118: ; preds = %110, %135
%119 = phi i32 [ %136, %135 ], [ 1, %110 ]
%120 = load i8, i8* %7
%121 = add i8 %120, -48
%122 = icmp ult i8 %121, 10
br i1 %122, label %123, label %131
123: ; preds = %118
%124 = icmp eq i32 %119, 0
br i1 %124, label %128, label %125
125: ; preds = %123
%126 = load i8*, i8** %26
%127 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0), i8* %40, i8* %126)
br label %128
128: ; preds = %125, %123
%129 = zext i8 %120 to i32
%130 = call i32 @fputc(i32 %129, i32* %30)
br label %135
131: ; preds = %118
%132 = icmp eq i8 %120, 58
br i1 %132, label %133, label %135
133: ; preds = %131
%134 = call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.17, i64 0, i64 0), i64 2, i64 1, i32* %30)
br label %135
135: ; preds = %131, %133, %128
%136 = phi i32 [ 0, %128 ], [ 1, %133 ], [ %119, %131 ]
%137 = load i32, i32* %46
%138 = call i64 @read(i32 %137, i8* %7, i32 1)
%139 = icmp eq i64 %138, 0
br i1 %139, label %140, label %118
140: ; preds = %135, %110
%141 = load i32, i32* %46
%142 = call i32 @close(i32 %141)
%143 = call i32 @fclose(i32* %30)
%144 = call i32 @exit(i32 0)
unreachable
145: ; preds = %101
%146 = load i32, i32* %46
%147 = call i32 @close(i32 %146)
%148 = load i32, i32* %48
%149 = call i32 @close(i32 %148)
%150 = call i32 @wait(i32* null)
%151 = call signext i8 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %40)
%152 = load i8, i8* @EOF
%153 = icmp eq i8 %151, %152
br i1 %153, label %154, label %49
154: ; preds = %145, %23
%155 = call i32 @fclose(i32* %16)
%156 = call i32 @exit(i32 0)
unreachable
}