#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 /*<<< orphan*/ mkdir(char *, int);
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 != 5) {
    printf("Usage: %s <filename>.txt <chapter> <words>.txt <outdir>\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 out_d[256];
  strcpy(out_d, argv[4]);
  strcat(out_d, "temp/");
  mkdir(out_d, 0777);
  char tempFileName[80];
  strcpy(tempFileName, out_d);
  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    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     1250 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbf(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdda(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    129d <main+0x8d>
mov    0x2d56(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    -0x170(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x170(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   10f0 <strcat@plt>
lea    -0x170(%rbp),%rdi
lea    0xd4e(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x170(%rbp),%rdi
lea    0xd45(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x178(%rbp)
mov    0x2c98(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    -0x170(%rbp),%r8
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1f0(%rbp),%rdx
lea    0xd33(%rip),%rsi
call   1090 <fscanf@plt>
mov    %al,-0x1f1(%rbp)
movsbl %al,%eax
mov    0x2c61(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     16b6 <main+0x4a6>
lea    -0x180(%rbp),%rdi
call   10b0 <pipe@plt>
cmp    $0xffffffff,%eax
jne    13c1 <main+0x1b1>
mov    0x2c2a(%rip),%rax
mov    (%rax),%rdi
lea    0xcf0(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x4,%edi
call   1100 <exit@plt>
lea    -0x188(%rbp),%rdi
call   10b0 <pipe@plt>
cmp    $0xffffffff,%eax
jne    13f8 <main+0x1e8>
mov    0x2bf3(%rip),%rax
mov    (%rax),%rdi
lea    0xcd4(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x4,%edi
call   1100 <exit@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x200(%rbp)
cmpq   $0x0,-0x200(%rbp)
jne    14ae <main+0x29e>
mov    -0x184(%rbp),%edi
mov    0x2bcf(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x180(%rbp),%edi
mov    0x2b8b(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x180(%rbp),%edi
call   10a0 <close@plt>
mov    -0x17c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x188(%rbp),%edi
call   10a0 <close@plt>
mov    -0x184(%rbp),%edi
call   10a0 <close@plt>
lea    -0x1f0(%rbp),%rcx
lea    0xc61(%rip),%rsi
lea    0xc5f(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1110 <execlp@plt>
mov    0x2b3d(%rip),%rax
mov    (%rax),%rdi
lea    0xc43(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x3,%edi
call   1100 <exit@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x200(%rbp)
cmpq   $0x0,-0x200(%rbp)
jne    1533 <main+0x323>
mov    -0x17c(%rbp),%edi
mov    0x2b19(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    -0x180(%rbp),%edi
call   10a0 <close@plt>
mov    -0x17c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xbf7(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
mov    $0x0,%al
call   1110 <execlp@plt>
mov    0x2ab8(%rip),%rax
mov    (%rax),%rdi
lea    0xbdc(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x3,%edi
call   1100 <exit@plt>
mov    -0x180(%rbp),%edi
call   10a0 <close@plt>
mov    -0x17c(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1120 <wait@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1120 <wait@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %rax,-0x200(%rbp)
cmpq   $0x0,-0x200(%rbp)
jne    1692 <main+0x482>
mov    -0x184(%rbp),%edi
call   10a0 <close@plt>
movl   $0x1,-0x208(%rbp)
mov    0x2a35(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setbuf@plt>
mov    -0x188(%rbp),%edi
lea    -0x201(%rbp),%rsi
mov    $0x1,%edx
call   10c0 <read@plt>
cmp    $0x0,%rax
je     1674 <main+0x464>
movsbl -0x201(%rbp),%eax
cmp    $0x30,%eax
jl     163b <main+0x42b>
movsbl -0x201(%rbp),%eax
cmp    $0x39,%eax
jg     163b <main+0x42b>
cmpl   $0x0,-0x208(%rbp)
je     161a <main+0x40a>
mov    -0x178(%rbp),%rdi
lea    -0x1f0(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xb0e(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
movl   $0x0,-0x208(%rbp)
mov    -0x178(%rbp),%rdi
movsbl -0x201(%rbp),%edx
lea    0xaef(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
jmp    166f <main+0x45f>
movsbl -0x201(%rbp),%eax
cmp    $0x3a,%eax
jne    166a <main+0x45a>
mov    -0x178(%rbp),%rdi
lea    0xac8(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
movl   $0x1,-0x208(%rbp)
jmp    166f <main+0x45f>
jmp    159e <main+0x38e>
mov    -0x188(%rbp),%edi
call   10a0 <close@plt>
mov    -0x178(%rbp),%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x188(%rbp),%edi
call   10a0 <close@plt>
mov    -0x184(%rbp),%edi
call   10a0 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1120 <wait@plt>
jmp    1358 <main+0x148>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1100 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f8,%rsp
mov    %rsi,%rbp
cmp    $0x5,%edi
jne    1284 <main+0x54>
mov    0x18(%rbp),%rdi
lea    0xde5(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
jne    129e <main+0x6e>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rdi
mov    0x18(%rbp),%rdx
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
call   10e0 <fprintf@plt>
mov    $0x2,%edi
call   1110 <exit@plt>
mov    0x0(%rbp),%rsi
lea    0xd71(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    %rax,%rbx
mov    0x20(%rbp),%rsi
lea    0xf0(%rsp),%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
lea    0xd96(%rip),%rsi
mov    %r15,%rdi
call   1100 <strcat@plt>
mov    %r15,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xa0(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x10(%rbp),%rsi
mov    %r14,%rdi
call   1100 <strcat@plt>
lea    0xd61(%rip),%rsi
mov    %r14,%rdi
call   1100 <strcat@plt>
lea    0xd5c(%rip),%rsi
mov    %r14,%rdi
call   10f0 <fopen@plt>
mov    %rax,0x20(%rsp)
mov    0x2cae(%rip),%rax
movslq (%rax),%rdi
mov    0x8(%rbp),%rdx
mov    %rbp,0x28(%rsp)
mov    0x10(%rbp),%rcx
lea    0xd33(%rip),%rsi
mov    %r14,%r8
xor    %eax,%eax
call   10e0 <fprintf@plt>
lea    0xd5b(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    %rbx,%rdi
call   1090 <fscanf@plt>
mov    0x2c8a(%rip),%r15
cmp    (%r15),%al
jne    136a <main+0x13a>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xd22(%rip),%rbp
lea    0x30(%rsp),%r14
mov    %r12,%rdi
call   10b0 <pipe@plt>
cmp    $0xffffffff,%eax
je     1427 <main+0x1f7>
mov    %r13,%rdi
call   10b0 <pipe@plt>
cmp    $0xffffffff,%eax
je     143f <main+0x20f>
xor    %eax,%eax
call   1150 <fork@plt>
test   %rax,%rax
je     1469 <main+0x239>
xor    %eax,%eax
call   1150 <fork@plt>
test   %rax,%rax
je     14e9 <main+0x2b9>
mov    0x18(%rsp),%edi
call   10a0 <close@plt>
mov    0x1c(%rsp),%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   1140 <wait@plt>
xor    %edi,%edi
call   1140 <wait@plt>
xor    %eax,%eax
call   1150 <fork@plt>
test   %rax,%rax
je     1553 <main+0x323>
mov    0x10(%rsp),%edi
call   10a0 <close@plt>
mov    0x14(%rsp),%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   1140 <wait@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1090 <fscanf@plt>
cmp    (%r15),%al
jne    1380 <main+0x150>
jmp    135b <main+0x12b>
mov    0x2ba2(%rip),%rax
mov    (%rax),%rcx
lea    0xc68(%rip),%rdi
mov    $0x1a,%esi
jmp    1455 <main+0x225>
mov    0x2b8a(%rip),%rax
mov    (%rax),%rcx
lea    0xc6b(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
call   1120 <fwrite@plt>
mov    $0x4,%edi
call   1110 <exit@plt>
mov    0x14(%rsp),%edi
mov    0x2b7c(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    0x18(%rsp),%edi
mov    0x2b3a(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    0x18(%rsp),%edi
call   10a0 <close@plt>
mov    0x1c(%rsp),%edi
call   10a0 <close@plt>
mov    0x10(%rsp),%edi
call   10a0 <close@plt>
mov    0x14(%rsp),%edi
call   10a0 <close@plt>
lea    0xc1f(%rip),%rdi
lea    0xc1d(%rip),%rdx
lea    0x30(%rsp),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
xor    %eax,%eax
call   1130 <execlp@plt>
mov    0x2af8(%rip),%rax
mov    (%rax),%rcx
lea    0xbfe(%rip),%rdi
mov    $0x19,%esi
jmp    153f <main+0x30f>
mov    0x1c(%rsp),%edi
mov    0x2afc(%rip),%rax
mov    (%rax),%esi
call   1070 <dup2@plt>
mov    0x18(%rsp),%edi
call   10a0 <close@plt>
mov    0x1c(%rsp),%edi
call   10a0 <close@plt>
mov    0x28(%rsp),%rax
mov    0x8(%rax),%rdx
lea    0xbdd(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1130 <execlp@plt>
mov    0x2aa0(%rip),%rax
mov    (%rax),%rcx
lea    0xbc4(%rip),%rdi
mov    $0x18,%esi
mov    $0x1,%edx
call   1120 <fwrite@plt>
mov    $0x3,%edi
call   1110 <exit@plt>
mov    0x14(%rsp),%edi
call   10a0 <close@plt>
mov    0x2a65(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%ebx
mov    $0x1,%edx
call   10d0 <read@plt>
test   %rax,%rax
jne    159c <main+0x36c>
mov    0x10(%rsp),%edi
call   10a0 <close@plt>
mov    0x20(%rsp),%rdi
jmp    135e <main+0x12e>
lea    0xb74(%rip),%r14
lea    0x30(%rsp),%r13
lea    0xf(%rsp),%r15
lea    0xb6a(%rip),%r12
jmp    15e2 <main+0x3b2>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <fputc@plt>
xor    %ebx,%ebx
mov    0x10(%rsp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   10d0 <read@plt>
test   %rax,%rax
je     1589 <main+0x359>
movzbl 0xf(%rsp),%ebp
lea    -0x30(%rbp),%eax
cmp    $0x9,%al
ja     1620 <main+0x3f0>
test   %ebx,%ebx
mov    0x20(%rsp),%rbx
je     15c0 <main+0x390>
mov    0x28(%rsp),%rax
mov    0x10(%rax),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   10e0 <fprintf@plt>
jmp    15c0 <main+0x390>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x3a,%bpl
jne    15cc <main+0x39c>
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    0x20(%rsp),%rcx
call   1120 <fwrite@plt>
mov    $0x1,%ebx
jmp    15cc <main+0x39c>

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

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