#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ read_ihex_line(int /*<<< orphan*/ *, unsigned char *,
                          unsigned int *);
void load_ihex(const char *file, unsigned char *memory) {
  FILE *f = fopen(file, "r");
  unsigned start_addr = -1;
  while (!feof(f))
    if (read_ihex_line(f, memory, &start_addr)) {
      printf("Couldn't load %s as ihex\n", file);
      break;
    }
}


x86 assembly (clang, -O0):
<load_ihex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1189 <load_ihex+0x49>
jmp    11be <load_ihex+0x7e>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <read_ihex_line@plt>
cmp    $0x0,%rax
je     11b9 <load_ihex+0x79>
mov    -0x8(%rbp),%rsi
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
jmp    11be <load_ihex+0x7e>
jmp    116b <load_ihex+0x2b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_ihex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x4(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <feof@plt>
test   %eax,%eax
jne    119e <load_ihex+0x5e>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <read_ihex_line@plt>
test   %rax,%rax
je     1170 <load_ihex+0x30>
lea    0xe6c(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Couldn't load %s as ihex\0A\00"
define void @load_ihex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
store i32 -1, i32* %6
br label %9
9: ; preds = %22, %2
%10 = load i32*, i32** %5
%11 = call i32 @feof(i32* %10)
%12 = icmp ne i32 %11, 0
%13 = xor i1 %12, true
br i1 %13, label %14, label %23
14: ; preds = %9
%15 = load i32*, i32** %5
%16 = load i8*, i8** %4
%17 = call i64 @read_ihex_line(i32* %15, i8* %16, i32* %6)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %23
22: ; preds = %14
br label %9
23: ; preds = %19, %9
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Couldn't load %s as ihex\0A\00"
define void @load_ihex(i8* %0, i8* %1) {
%3 = alloca i32
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
br label %6
6: ; preds = %9, %2
%7 = call i32 @feof(i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = call i64 @read_ihex_line(i32* %4, i8* %1, i32* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %6, label %12
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %6, %12
ret void
}