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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ isspace(int);
scalar_t__ read_record(int /*<<< orphan*/ *, size_t, char *, int *);
int strlen(char *);
int read_hexfile(uint8_t *buf, size_t buflen, const char *filename) {
  FILE *fp;
  char line[1024];
  bool eof;
  if (NULL == (fp = fopen(filename, "ro")))
    return 1;
  while (NULL != fgets(line, sizeof(line), fp)) {
    char *p = (line + strlen(line)) - 1;
    while (p > line) {
      if (isspace((int)(*p)))
        *p = 0;
      p--;
    }
    if (0 != read_record(buf, buflen, line, &eof))
      goto fail;
    if (eof)
      break;
  }
  fclose(fp);
  return 0;
fail:
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<read_hexfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a5 <read_hexfile+0x45>
movl   $0x1,-0x4(%rbp)
jmp    12bc <read_hexfile+0x15c>
jmp    11aa <read_hexfile+0x4a>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1297 <read_hexfile+0x137>
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x448(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
lea    -0x430(%rbp),%rcx
cmp    %rcx,%rax
jbe    1252 <read_hexfile+0xf2>
mov    -0x440(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
je     123b <read_hexfile+0xdb>
mov    -0x440(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x440(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x440(%rbp)
jmp    1201 <read_hexfile+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x430(%rbp),%rdx
lea    -0x434(%rbp),%rcx
call   1050 <read_record@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1280 <read_hexfile+0x120>
jmp    12ac <read_hexfile+0x14c>
cmpl   $0x0,-0x434(%rbp)
je     1292 <read_hexfile+0x132>
jmp    1297 <read_hexfile+0x137>
jmp    11aa <read_hexfile+0x4a>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bc <read_hexfile+0x15c>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hexfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe82(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1238 <read_hexfile+0xd8>
mov    %rax,%r12
lea    0x10(%rsp),%rbx
xor    %ebp,%ebp
jmp    11cf <read_hexfile+0x6f>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
lea    0xc(%rsp),%rcx
call   1050 <read_record@plt>
xor    %ecx,%ecx
cmpl   $0x0,0xc(%rsp)
setne  %cl
test   %rax,%rax
lea    (%rcx,%rcx,2),%eax
mov    $0x6,%ecx
cmovne %ecx,%eax
test   %eax,%eax
jne    121f <read_hexfile+0xbf>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     122e <read_hexfile+0xce>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%r13
add    $0xf,%r13
jmp    1204 <read_hexfile+0xa4>
nopl   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r13
cmp    %rbx,%r13
jbe    11a0 <read_hexfile+0x40>
movsbl 0x0(%r13),%edi
call   1060 <isspace@plt>
test   %rax,%rax
je     1200 <read_hexfile+0xa0>
movb   $0x0,0x0(%r13)
jmp    1200 <read_hexfile+0xa0>
cmp    $0x3,%eax
je     122e <read_hexfile+0xce>
cmp    $0x6,%eax
jne    123f <read_hexfile+0xdf>
mov    $0x1,%ebp
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    123f <read_hexfile+0xdf>
mov    $0x1,%ebp
jmp    123f <read_hexfile+0xdf>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ro\00"
define i32 @read_hexfile(i32* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i8*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %7
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = icmp eq i32* null, %13
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 1, i32* %4
br label %62
16: ; preds = %3
br label %17
17: ; preds = %55, %16
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%19 = load i32*, i32** %8
%20 = call i32* @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp ne i32* null, %20
br i1 %21, label %22, label %56
22: ; preds = %17
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 -1
store i8* %28, i8** %11
br label %29
29: ; preds = %41, %22
%30 = load i8*, i8** %11
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = icmp ugt i8* %30, %31
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = load i8*, i8** %11
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = call i64 @isspace(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = load i8*, i8** %11
store i8 0, i8* %40
br label %41
41: ; preds = %39, %33
%42 = load i8*, i8** %11
%43 = getelementptr inbounds i8, i8* %42, i32 -1
store i8* %43, i8** %11
br label %29
44: ; preds = %29
%45 = load i32*, i32** %5
%46 = load i64, i64* %6
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = call i64 @read_record(i32* %45, i64 %46, i8* %47, i32* %10)
%49 = icmp ne i64 0, %48
br i1 %49, label %50, label %51
50: ; preds = %44
br label %59
51: ; preds = %44
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %56
55: ; preds = %51
br label %17
56: ; preds = %54, %17
%57 = load i32*, i32** %8
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %4
br label %62
59: ; preds = %50
%60 = load i32*, i32** %8
%61 = call i32 @fclose(i32* %60)
store i32 1, i32* %4
br label %62
62: ; preds = %59, %56, %15
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"ro\00"
define i32 @read_hexfile(i32* %0, i64 %1, i8* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = bitcast i32* %5 to i8*
%8 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %40, label %10
10: ; preds = %3, %29
%11 = call i32* @fgets(i8* %6, i32 1024, i32* %8)
%12 = icmp eq i32* %11, null
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = call i32 @strlen(i8* %6)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -1
%18 = icmp ugt i8* %17, %6
br i1 %18, label %19, label %29
19: ; preds = %13, %26
%20 = phi i8* [ %27, %26 ], [ %17, %13 ]
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = call i64 @isspace(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %19
store i8 0, i8* %20
br label %26
26: ; preds = %25, %19
%27 = getelementptr inbounds i8, i8* %20, i64 -1
%28 = icmp ugt i8* %27, %6
br i1 %28, label %19, label %29
29: ; preds = %26, %13
%30 = call i64 @read_record(i32* %0, i64 %1, i8* %6, i32* %5)
%31 = icmp eq i64 %30, 0
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 0
%34 = select i1 %33, i32 0, i32 3
%35 = select i1 %31, i32 %34, i32 6
switch i32 %35, label %40 [
i32 0, label %10
i32 3, label %37
i32 6, label %36
]
36: ; preds = %29
br label %37
37: ; preds = %10, %29, %36
%38 = phi i32 [ 1, %36 ], [ 0, %29 ], [ 0, %10 ]
%39 = call i32 @fclose(i32* %8)
br label %40
40: ; preds = %29, %37, %3
%41 = phi i32 [ 1, %3 ], [ %38, %37 ], [ undef, %29 ]
ret i32 %41
}