#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getFilename(char *, int);
scalar_t__ is_valid_int(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
char *strtok(char *, char *);
void parsefile() {
  int lineNumber, opCode, validLine;
  int sourceLineNumber = 0;
  char line[100];
  char *linePtr;
  size_t tokenNumber = 0;
  char *tokenPtr;
  char filename[30];
  printf("Enter name of file to load: ");
  getFilename(filename, 30);
  FILE *sourceFilePtr = fopen(filename, "r");
  if (sourceFilePtr == NULL) {
    printf("Error - unable to open file \"%s\"\n", filename);
    return;
  } else {
    while (fgets(line, sizeof line, sourceFilePtr) != NULL) {
      validLine = 0;
      sourceLineNumber++;
      linePtr = line;
      while (linePtr[0] == ' ')
        linePtr++;
      tokenNumber = 0;
      tokenPtr = strtok(linePtr, " \t");
      while (tokenPtr != NULL) {
        if (is_valid_int(tokenPtr) >= 0) {
          if (tokenNumber == 0) {
            sscanf(tokenPtr, "%d", &lineNumber);
            validLine++;
            printf("Line number is %d\n", lineNumber);
          } else if (tokenNumber == 1) {
            sscanf(tokenPtr, "%d", &opCode);
            validLine++;
            printf("Opcode is %06d\n", opCode);
          }
        }
        tokenPtr = strtok(NULL, " \t");
        tokenNumber++;
      }
      if (validLine == 2)
        printf("linenumber=%d add %d %06d here\n", sourceLineNumber, lineNumber,
               opCode);
    }
  }
  fclose(sourceFilePtr);
}


x86 assembly (clang, -O0):
<parsefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x90(%rbp)
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xc0(%rbp),%rdi
mov    $0x1e,%esi
call   10a0 <getFilename@plt>
lea    -0xc0(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xc8(%rbp)
cmpq   $0x0,-0xc8(%rbp)
jne    11fe <parsefile+0x7e>
lea    -0xc0(%rbp),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13a2 <parsefile+0x222>
jmp    1203 <parsefile+0x83>
lea    -0x80(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1391 <parsefile+0x211>
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1267 <parsefile+0xe7>
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x88(%rbp)
jmp    123d <parsefile+0xbd>
movq   $0x0,-0x90(%rbp)
mov    -0x88(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     136b <parsefile+0x1eb>
mov    -0x98(%rbp),%rdi
call   1070 <is_valid_int@plt>
cmp    $0x0,%rax
jl     133d <parsefile+0x1bd>
cmpq   $0x0,-0x90(%rbp)
jne    12f4 <parsefile+0x174>
mov    -0x98(%rbp),%rdi
lea    0xd78(%rip),%rsi
lea    -0x4(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%esi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1338 <parsefile+0x1b8>
cmpq   $0x1,-0x90(%rbp)
01
jne    1333 <parsefile+0x1b3>
mov    -0x98(%rbp),%rdi
lea    0xd34(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%esi
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1338 <parsefile+0x1b8>
jmp    133d <parsefile+0x1bd>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcf9(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x98(%rbp)
mov    -0x90(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
jmp    128c <parsefile+0x10c>
cmpl   $0x2,-0xc(%rbp)
jne    138c <parsefile+0x20c>
mov    -0x10(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <parsefile+0x83>
jmp    1396 <parsefile+0x216>
mov    -0xc8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parsefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xe68(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   10a0 <getFilename@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1306 <parsefile+0x186>
mov    %rax,%r14
lea    0x30(%rsp),%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12fc <parsefile+0x17c>
lea    0xe53(%rip),%rbx
lea    0xe4f(%rip),%r12
xor    %r13d,%r13d
mov    %r14,0x8(%rsp)
jmp    121b <parsefile+0x9b>
nop
lea    0x30(%rsp),%rdi
mov    $0x64,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12fc <parsefile+0x17c>
lea    0x2f(%rsp),%rdi
cmpb   $0x20,0x1(%rdi)
lea    0x1(%rdi),%rdi
je     1220 <parsefile+0xa0>
add    $0x1,%r13d
mov    %rbx,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1200 <parsefile+0x80>
mov    %rax,%rbp
xor    %r15d,%r15d
xor    %r14d,%r14d
jmp    1289 <parsefile+0x109>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rsp,%rdx
call   1060 <sscanf@plt>
add    $0x1,%r15d
mov    (%rsp),%esi
lea    0xdee(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
add    $0x1,%r14
test   %rax,%rax
je     12d0 <parsefile+0x150>
mov    %rbp,%rdi
call   1070 <is_valid_int@plt>
test   %rax,%rax
js     1273 <parsefile+0xf3>
cmp    $0x1,%r14
je     1250 <parsefile+0xd0>
test   %r14,%r14
jne    1273 <parsefile+0xf3>
mov    %rbp,%rdi
mov    %r12,%rsi
lea    0x4(%rsp),%rdx
call   1060 <sscanf@plt>
add    $0x1,%r15d
mov    0x4(%rsp),%esi
lea    0xd87(%rip),%rdi
jmp    126c <parsefile+0xec>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2,%r15d
mov    0x8(%rsp),%r14
jne    1200 <parsefile+0x80>
mov    0x4(%rsp),%edx
mov    (%rsp),%ecx
lea    0xd7d(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1200 <parsefile+0x80>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1319 <parsefile+0x199>
lea    0xd12(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Enter name of file to load: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Error - unable to open file \22%s\22\0A\00"
@.str.3 = [3 x i8] c" \09\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [19 x i8] c"Line number is %d\0A\00"
@.str.6 = [16 x i8] c"Opcode is %06d\0A\00"
@.str.7 = [32 x i8] c"linenumber=%d add %d %06d here\0A\00"
define void @parsefile() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [30 x i8]
%10 = alloca i32*
store i32 0, i32* %4
store i64 0, i64* %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%13 = call i32 @getFilename(i8* %12, i32 30)
%14 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %89
21: ; preds = %0
br label %22
22: ; preds = %84, %21
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = load i32*, i32** %10
%25 = call i32* @fgets(i8* %23, i32 100, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %85
27: ; preds = %22
store i32 0, i32* %3
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
store i8* %30, i8** %6
br label %31
31: ; preds = %37, %27
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 32
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %6
br label %31
40: ; preds = %31
store i64 0, i64* %7
%41 = load i8*, i8** %6
%42 = call i8* @strtok(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %42, i8** %8
br label %43
43: ; preds = %72, %40
%44 = load i8*, i8** %8
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %76
46: ; preds = %43
%47 = load i8*, i8** %8
%48 = call i64 @is_valid_int(i8* %47)
%49 = icmp sge i64 %48, 0
br i1 %49, label %50, label %72
50: ; preds = %46
%51 = load i64, i64* %7
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = load i8*, i8** %8
%55 = call i32 @sscanf(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 1
store i32 %57, i32* %3
%58 = load i32, i32* %1
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %58)
br label %71
60: ; preds = %50
%61 = load i64, i64* %7
%62 = icmp eq i64 %61, 1
br i1 %62, label %63, label %70
63: ; preds = %60
%64 = load i8*, i8** %8
%65 = call i32 @sscanf(i8* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%66 = load i32, i32* %3
%67 = add nsw i32 %66, 1
store i32 %67, i32* %3
%68 = load i32, i32* %2
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %68)
br label %70
70: ; preds = %63, %60
br label %71
71: ; preds = %70, %53
br label %72
72: ; preds = %71, %46
%73 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %73, i8** %8
%74 = load i64, i64* %7
%75 = add i64 %74, 1
store i64 %75, i64* %7
br label %43
76: ; preds = %43
%77 = load i32, i32* %3
%78 = icmp eq i32 %77, 2
br i1 %78, label %79, label %84
79: ; preds = %76
%80 = load i32, i32* %4
%81 = load i32, i32* %1
%82 = load i32, i32* %2
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %80, i32 %81, i32 %82)
br label %84
84: ; preds = %79, %76
br label %22
85: ; preds = %22
br label %86
86: ; preds = %85
%87 = load i32*, i32** %10
%88 = call i32 @fclose(i32* %87)
br label %89
89: ; preds = %86, %18
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"Enter name of file to load: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Error - unable to open file \22%s\22\0A\00"
@.str.3 = [3 x i8] c" \09\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [19 x i8] c"Line number is %d\0A\00"
@.str.6 = [16 x i8] c"Opcode is %06d\0A\00"
@.str.7 = [32 x i8] c"linenumber=%d add %d %06d here\0A\00"
define void @parsefile() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca [30 x i8]
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @getFilename(i8* %8, i32 30)
%11 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i32* @fgets(i8* %7, i32 100, i32* %11)
%15 = icmp eq i32* %14, null
br i1 %15, label %60, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %62
18: ; preds = %13, %57
%19 = phi i32 [ %26, %57 ], [ 0, %13 ]
br label %20
20: ; preds = %20, %18
%21 = phi i8* [ %7, %18 ], [ %24, %20 ]
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 32
%24 = getelementptr inbounds i8, i8* %21, i64 1
br i1 %23, label %20, label %25
25: ; preds = %20
%26 = add nuw nsw i32 %19, 1
%27 = call i8* @strtok(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %57, label %29
29: ; preds = %25, %46
%30 = phi i32 [ %47, %46 ], [ 0, %25 ]
%31 = phi i8* [ %48, %46 ], [ %27, %25 ]
%32 = phi i64 [ %49, %46 ], [ 0, %25 ]
%33 = call i64 @is_valid_int(i8* %31)
%34 = icmp sgt i64 %33, -1
br i1 %34, label %35, label %46
35: ; preds = %29
switch i64 %32, label %46 [
i64 0, label %36
i64 1, label %41
]
36: ; preds = %35
%37 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%38 = add nsw i32 %30, 1
%39 = load i32, i32* %1
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %39)
br label %46
41: ; preds = %35
%42 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%43 = add nsw i32 %30, 1
%44 = load i32, i32* %2
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %35, %36, %41, %29
%47 = phi i32 [ %38, %36 ], [ %43, %41 ], [ %30, %29 ], [ %30, %35 ]
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%49 = add i64 %32, 1
%50 = icmp eq i8* %48, null
br i1 %50, label %51, label %29
51: ; preds = %46
%52 = icmp eq i32 %47, 2
br i1 %52, label %53, label %57
53: ; preds = %51
%54 = load i32, i32* %1
%55 = load i32, i32* %2
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %26, i32 %54, i32 %55)
br label %57
57: ; preds = %25, %53, %51
%58 = call i32* @fgets(i8* %7, i32 100, i32* %11)
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %18
60: ; preds = %57, %13
%61 = call i32 @fclose(i32* %11)
br label %62
62: ; preds = %60, %16
ret void
}