#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, int);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ read_hex(int /*<<< orphan*/ *, int, int *);
int /*<<< orphan*/ stderr;
bool read_ihex_line(FILE *f, unsigned char *memory, unsigned *start_addr) {
  int ch, count, addr, type, v, chk;
  do {
    ch = fgetc(f);
    if (ch < 0)
      return false;
  } while (ch == '\n' || ch == '\r');
  if (ch != ':') {
    fprintf(stderr, "Expected ':' at pos %ld, got %d", ftell(f) - 1, ch);
    return true;
  }
  if (read_hex(f, 2, &count) || read_hex(f, 4, &addr) || read_hex(f, 2, &type))
    return true;
  if (type == 5)
    *start_addr = addr;
  while (count-- > 0) {
    if (read_hex(f, 2, &v))
      return true;
    if (2048 <= addr && addr < 65536)
      memory[addr++] = v;
  }
  if (read_hex(f, 2, &chk))
    return true;
  return false;
}


x86 assembly (clang, -O0):
<read_ihex_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1176 <read_ihex_line+0x36>
movl   $0x0,-0x4(%rbp)
jmp    1300 <read_ihex_line+0x1c0>
jmp    117b <read_ihex_line+0x3b>
mov    $0x1,%al
cmpl   $0xa,-0x24(%rbp)
mov    %al,-0x39(%rbp)
je     1194 <read_ihex_line+0x54>
cmpl   $0xd,-0x24(%rbp)
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1154 <read_ihex_line+0x14>
cmpl   $0x3a,-0x24(%rbp)
je     11e3 <read_ihex_line+0xa3>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
sub    $0x1,%rdx
mov    -0x24(%rbp),%ecx
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <read_ihex_line+0x1c0>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
lea    -0x28(%rbp),%rdx
call   1030 <read_hex@plt>
cmp    $0x0,%rax
jne    1237 <read_ihex_line+0xf7>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
lea    -0x2c(%rbp),%rdx
call   1030 <read_hex@plt>
cmp    $0x0,%rax
jne    1237 <read_ihex_line+0xf7>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
lea    -0x30(%rbp),%rdx
call   1030 <read_hex@plt>
cmp    $0x0,%rax
je     1243 <read_ihex_line+0x103>
movl   $0x1,-0x4(%rbp)
jmp    1300 <read_ihex_line+0x1c0>
cmpl   $0x5,-0x30(%rbp)
jne    1256 <read_ihex_line+0x116>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    125b <read_ihex_line+0x11b>
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x28(%rbp)
cmp    $0x0,%eax
jle    12d1 <read_ihex_line+0x191>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
lea    -0x34(%rbp),%rdx
call   1030 <read_hex@plt>
cmp    $0x0,%rax
je     1297 <read_ihex_line+0x157>
movl   $0x1,-0x4(%rbp)
jmp    1300 <read_ihex_line+0x1c0>
mov    $0x800,%eax
cmp    -0x2c(%rbp),%eax
jg     12cc <read_ihex_line+0x18c>
cmpl   $0x10000,-0x2c(%rbp)
jge    12cc <read_ihex_line+0x18c>
mov    -0x34(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    125b <read_ihex_line+0x11b>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
lea    -0x38(%rbp),%rdx
call   1030 <read_hex@plt>
cmp    $0x0,%rax
je     12f9 <read_ihex_line+0x1b9>
movl   $0x1,-0x4(%rbp)
jmp    1300 <read_ihex_line+0x1c0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ihex_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <fgetc@plt>
xor    %r12d,%r12d
test   %eax,%eax
js     1235 <read_ihex_line+0xf5>
mov    %eax,%ebp
jmp    1182 <read_ihex_line+0x42>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1235 <read_ihex_line+0xf5>
cmp    $0xa,%ebp
je     1170 <read_ihex_line+0x30>
cmp    $0xd,%ebp
je     1170 <read_ihex_line+0x30>
cmp    $0x3a,%ebp
jne    1208 <read_ihex_line+0xc8>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <read_hex@plt>
mov    $0x1,%r12d
test   %rax,%rax
jne    1235 <read_ihex_line+0xf5>
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <read_hex@plt>
test   %rax,%rax
jne    1235 <read_ihex_line+0xf5>
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <read_hex@plt>
test   %rax,%rax
jne    1235 <read_ihex_line+0xf5>
cmpl   $0x5,0x18(%rsp)
jne    11ee <read_ihex_line+0xae>
mov    0x10(%rsp),%eax
mov    %eax,(%r15)
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
test   %eax,%eax
jle    128d <read_ihex_line+0x14d>
lea    0x14(%rsp),%rbp
jmp    1254 <read_ihex_line+0x114>
mov    0x2dd1(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1060 <ftell@plt>
lea    -0x1(%rax),%rdx
lea    0xddb(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
test   %eax,%eax
jle    128d <read_ihex_line+0x14d>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %rbp,%rdx
call   1030 <read_hex@plt>
test   %rax,%rax
jne    1235 <read_ihex_line+0xf5>
mov    0x10(%rsp),%eax
lea    -0x800(%rax),%ecx
cmp    $0xf7ff,%ecx
ja     1245 <read_ihex_line+0x105>
movzbl 0x14(%rsp),%ecx
lea    0x1(%rax),%edx
mov    %edx,0x10(%rsp)
mov    %cl,(%r14,%rax,1)
jmp    1245 <read_ihex_line+0x105>
lea    0x1c(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <read_hex@plt>
xor    %r12d,%r12d
test   %rax,%rax
setne  %r12b
jmp    1235 <read_ihex_line+0xf5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Expected ':' at pos %ld, got %d\00"
define i32 @read_ihex_line(i32* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
br label %14
14: ; preds = %27, %3
%15 = load i32*, i32** %5
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 0, i32* %4
br label %89
20: ; preds = %14
br label %21
21: ; preds = %20
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 10
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 13
br label %27
27: ; preds = %24, %21
%28 = phi i1 [ true, %21 ], [ %26, %24 ]
br i1 %28, label %14, label %29
29: ; preds = %27
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 58
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32*, i32** %5
%35 = call i64 @ftell(i32* %34)
%36 = sub nsw i64 %35, 1
%37 = load i32, i32* %8
%38 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %36, i32 %37)
store i32 1, i32* %4
br label %89
39: ; preds = %29
%40 = load i32*, i32** %5
%41 = call i64 @read_hex(i32* %40, i32 2, i32* %9)
%42 = icmp ne i64 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = load i32*, i32** %5
%45 = call i64 @read_hex(i32* %44, i32 4, i32* %10)
%46 = icmp ne i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = load i32*, i32** %5
%49 = call i64 @read_hex(i32* %48, i32 2, i32* %11)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47, %43, %39
store i32 1, i32* %4
br label %89
52: ; preds = %47
%53 = load i32, i32* %11
%54 = icmp eq i32 %53, 5
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %10
%57 = load i32*, i32** %7
store i32 %56, i32* %57
br label %58
58: ; preds = %55, %52
br label %59
59: ; preds = %82, %58
%60 = load i32, i32* %9
%61 = add nsw i32 %60, -1
store i32 %61, i32* %9
%62 = icmp sgt i32 %60, 0
br i1 %62, label %63, label %83
63: ; preds = %59
%64 = load i32*, i32** %5
%65 = call i64 @read_hex(i32* %64, i32 2, i32* %12)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %63
store i32 1, i32* %4
br label %89
68: ; preds = %63
%69 = load i32, i32* %10
%70 = icmp sle i32 2048, %69
br i1 %70, label %71, label %82
71: ; preds = %68
%72 = load i32, i32* %10
%73 = icmp slt i32 %72, 65536
br i1 %73, label %74, label %82
74: ; preds = %71
%75 = load i32, i32* %12
%76 = trunc i32 %75 to i8
%77 = load i8*, i8** %6
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
%80 = sext i32 %78 to i64
%81 = getelementptr inbounds i8, i8* %77, i64 %80
store i8 %76, i8* %81
br label %82
82: ; preds = %74, %71, %68
br label %59
83: ; preds = %59
%84 = load i32*, i32** %5
%85 = call i64 @read_hex(i32* %84, i32 2, i32* %13)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %83
store i32 1, i32* %4
br label %89
88: ; preds = %83
store i32 0, i32* %4
br label %89
89: ; preds = %88, %87, %67, %51, %32, %19
%90 = load i32, i32* %4
ret i32 %90
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Expected ':' at pos %ld, got %d\00"
define i32 @read_ihex_line(i32* %0, i8* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = tail call i32 @fgetc(i32* %0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %65, label %16
16: ; preds = %3, %18
%17 = phi i32 [ %19, %18 ], [ %14, %3 ]
switch i32 %17, label %21 [
i32 13, label %18
i32 10, label %18
i32 58, label %26
]
18: ; preds = %16, %16
%19 = tail call i32 @fgetc(i32* %0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %65, label %16
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i64 @ftell(i32* %0)
%24 = add nsw i64 %23, -1
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %24, i32 %17)
br label %65
26: ; preds = %16
%27 = call i64 @read_hex(i32* %0, i32 2, i32* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %65
29: ; preds = %26
%30 = call i64 @read_hex(i32* %0, i32 4, i32* %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %65
32: ; preds = %29
%33 = call i64 @read_hex(i32* %0, i32 2, i32* %6)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %65
35: ; preds = %32
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 5
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %35
%41 = load i32, i32* %4
%42 = add nsw i32 %41, -1
store i32 %42, i32* %4
%43 = icmp sgt i32 %41, 0
br i1 %43, label %44, label %61
44: ; preds = %40, %57
%45 = call i64 @read_hex(i32* %0, i32 2, i32* %7)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %65
47: ; preds = %44
%48 = load i32, i32* %5
%49 = add i32 %48, -2048
%50 = icmp ult i32 %49, 63488
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* %7
%53 = trunc i32 %52 to i8
%54 = add nuw nsw i32 %48, 1
store i32 %54, i32* %5
%55 = zext i32 %48 to i64
%56 = getelementptr inbounds i8, i8* %1, i64 %55
store i8 %53, i8* %56
br label %57
57: ; preds = %51, %47
%58 = load i32, i32* %4
%59 = add nsw i32 %58, -1
store i32 %59, i32* %4
%60 = icmp sgt i32 %58, 0
br i1 %60, label %44, label %61
61: ; preds = %57, %40
%62 = call i64 @read_hex(i32* %0, i32 2, i32* %8)
%63 = icmp ne i64 %62, 0
%64 = zext i1 %63 to i32
br label %65
65: ; preds = %18, %44, %3, %61, %26, %29, %32, %21
%66 = phi i32 [ 1, %21 ], [ 1, %32 ], [ 1, %29 ], [ 1, %26 ], [ %64, %61 ], [ 0, %3 ], [ 1, %44 ], [ 0, %18 ]
ret i32 %66
}