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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int loadLine(char *, int, char *, int);
int /*<<< orphan*/ stderr;
void loadHexFile(char *name) {
  FILE *f;
  char buf[120];
  int lineCount = 0;
  int expectedAddr = -1;
  f = fopen(name, "r");
  if (!f) {
    fprintf(stderr, "can't open hex file '%s'\n", name);
    exit(2);
  }
  while (fgets(buf, sizeof(buf), f)) {
    lineCount++;
    expectedAddr = loadLine(name, lineCount, buf, expectedAddr);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<loadHexFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0xffffffff,-0x98(%rbp)
ff ff ff
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c5 <loadHexFile+0x65>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    11ca <loadHexFile+0x6a>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x78,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     121f <loadHexFile+0xbf>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x94(%rbp),%esi
lea    -0x90(%rbp),%rdx
mov    -0x98(%rbp),%ecx
call   1070 <loadLine@plt>
mov    %eax,-0x98(%rbp)
jmp    11ca <loadHexFile+0x6a>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadHexFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%r14
lea    0xe87(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f1 <loadHexFile+0x91>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x78,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11d9 <loadHexFile+0x79>
mov    $0xffffffff,%ebx
mov    $0x1,%ebp
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %ebx,%ecx
call   1070 <loadLine@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    $0x78,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    11b0 <loadHexFile+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"can't open hex file '%s'\0A\00"
define void @loadHexFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [120 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 -1, i32* %6
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 2)
unreachable
16: ; preds = %1
br label %17
17: ; preds = %22, %16
%18 = getelementptr inbounds [120 x i8], [120 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %3
%20 = call i64 @fgets(i8* %18, i32 120, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %17
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = load i8*, i8** %2
%26 = load i32, i32* %5
%27 = getelementptr inbounds [120 x i8], [120 x i8]* %4, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @loadLine(i8* %25, i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %6
br label %17
30: ; preds = %17
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"can't open hex file '%s'\0A\00"
define void @loadHexFile(i8* %0) {
%2 = alloca [120 x i8]
%3 = getelementptr inbounds [120 x i8], [120 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 120, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %20, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %6, %13
%14 = phi i32 [ %17, %13 ], [ -1, %6 ]
%15 = phi i32 [ %16, %13 ], [ 0, %6 ]
%16 = add nuw nsw i32 %15, 1
%17 = call i32 @loadLine(i8* %0, i32 %16, i8* %3, i32 %14)
%18 = call i64 @fgets(i8* %3, i32 120, i32* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %13
20: ; preds = %13, %6
%21 = call i32 @fclose(i32* %4)
ret void
}