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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int grep_fork(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int linec = 0, batch_count;
  FILE *sort_data;
  if (argc == 1) {
    sort_data = fopen("cs308a2_grep_data_2", "r");
  } else {
    sort_data = fopen(argv[1], "r");
  }
  if (sort_data == NULL) {
    printf("usage: %s [grep_data]\n", argv[0]);
    perror("fopen");
    exit(1);
  }
  while ((batch_count = grep_fork(sort_data)) != 0) {
    printf("Counted %d lines in this batch\n", batch_count);
    linec += batch_count;
    printf("Total lines so far: %d\n", linec);
  }
  fclose(sort_data);
  printf("\n\nTotal line count: %d\n", linec);
  printf("Done\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11a3 <main+0x43>
lea    0xe72(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
jmp    11bb <main+0x5b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe62(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f1 <main+0x91>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f6 <main+0x96>
mov    -0x20(%rbp),%rdi
call   1050 <grep_fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     123b <main+0xdb>
mov    -0x18(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f6 <main+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jne    1189 <main+0x19>
lea    0xe79(%rip),%rdi
jmp    118d <main+0x1d>
mov    0x8(%rbx),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11c5 <main+0x55>
mov    (%rbx),%rsi
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe77(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <grep_fork@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1218 <main+0xa8>
mov    %eax,%ebx
lea    0xe54(%rip),%r14
lea    0xe6d(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    %ebx,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
call   1060 <grep_fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11f0 <main+0x80>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xe44(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"cs308a2_grep_data_2\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"usage: %s [grep_data]\0A\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [32 x i8] c"Counted %d lines in this batch\0A\00"
@.str.5 = [24 x i8] c"Total lines so far: %d\0A\00"
@.str.6 = [24 x i8] c"\0A\0ATotal line count: %d\0A\00"
@.str.7 = [6 x i8] c"Done\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %8
br label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
br label %18
18: ; preds = %13, %11
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
br label %29
29: ; preds = %33, %28
%30 = load i32*, i32** %8
%31 = call i32 @grep_fork(i32* %30)
store i32 %31, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = add nsw i32 %37, %36
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %39)
br label %29
41: ; preds = %29
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %6
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"cs308a2_grep_data_2\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"usage: %s [grep_data]\0A\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [32 x i8] c"Counted %d lines in this batch\0A\00"
@.str.5 = [24 x i8] c"Total lines so far: %d\0A\00"
@.str.6 = [24 x i8] c"\0A\0ATotal line count: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [5 x i8] c"Done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %2, %4
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i32 @grep_fork(i32* %9)
%13 = icmp eq i32 %12, 0
br i1 %13, label %27, label %19
14: ; preds = %7
%15 = load i8*, i8** %1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11, %19
%20 = phi i32 [ %25, %19 ], [ %12, %11 ]
%21 = phi i32 [ %23, %19 ], [ 0, %11 ]
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%23 = add nsw i32 %20, %21
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = tail call i32 @grep_fork(i32* %9)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %19
27: ; preds = %19, %11
%28 = phi i32 [ 0, %11 ], [ %23, %19 ]
%29 = tail call i32 @fclose(i32* %9)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
}