#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int parseHex(int /*<<< orphan*/ *, int);
char parseUntilColon(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int parseIntelHex(char *hexfile, char buffer[65536 + 256], int *startAddr,
                  int *endAddr) {
  int address, base, d, segment, i, lineLen, sum;
  FILE *input;
  input = fopen(hexfile, "r");
  if (input == NULL) {
    fprintf(stderr, "error opening %s: %s\n", hexfile, strerror(errno));
    return 1;
  }
  while (parseUntilColon(input) == ':') {
    sum = 0;
    sum += lineLen = parseHex(input, 2);
    base = address = parseHex(input, 4);
    sum += address >> 8;
    sum += address;
    sum += segment = parseHex(input, 2);
    if (segment != 0)
      continue;
    for (i = 0; i < lineLen; i++) {
      d = parseHex(input, 2);
      buffer[address++] = d;
      sum += d;
    }
    sum += parseHex(input, 2);
    if ((sum & 0xff) != 0) {
      fprintf(stderr, "Warning: Checksum error between address 0x%x and 0x%x\n",
              base, address);
    }
    if (*startAddr > base)
      *startAddr = base;
    if (*endAddr < address)
      *endAddr = address;
  }
  fclose(input);
  return 0;
}


x86 assembly (clang, -O0):
<parseIntelHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11dd <parseIntelHex+0x7d>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1340 <parseIntelHex+0x1e0>
jmp    11e2 <parseIntelHex+0x82>
mov    -0x50(%rbp),%rdi
call   1080 <parseUntilColon@plt>
movsbl %al,%eax
cmp    $0x3a,%eax
jne    1330 <parseIntelHex+0x1d0>
movl   $0x0,-0x44(%rbp)
mov    -0x50(%rbp),%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
mov    %eax,-0x40(%rbp)
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x50(%rbp),%rdi
mov    $0x4,%esi
call   1060 <parseHex@plt>
mov    %eax,-0x2c(%rbp)
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
sar    $0x8,%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x50(%rbp),%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
mov    %eax,-0x38(%rbp)
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1264 <parseIntelHex+0x104>
jmp    11e2 <parseIntelHex+0x82>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    12b9 <parseIntelHex+0x159>
mov    -0x50(%rbp),%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
mov    %eax,-0x34(%rbp)
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)
mov    -0x34(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    126b <parseIntelHex+0x10b>
mov    -0x50(%rbp),%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
je     12fb <parseIntelHex+0x19b>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x30(%rbp),%eax
jle    1313 <parseIntelHex+0x1b3>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
jge    132b <parseIntelHex+0x1cb>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e2 <parseIntelHex+0x82>
mov    -0x50(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parseIntelHex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
lea    0xe7d(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1293 <parseIntelHex+0x133>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <parseUntilColon@plt>
cmp    $0x3a,%al
jne    1287 <parseIntelHex+0x127>
mov    %r14,0x8(%rsp)
mov    %r12,(%rsp)
jmp    11c0 <parseIntelHex+0x60>
nop
mov    %rbx,%rdi
call   1080 <parseUntilColon@plt>
cmp    $0x3a,%al
jne    1287 <parseIntelHex+0x127>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x4,%esi
call   1060 <parseHex@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
test   %eax,%eax
jne    11b0 <parseIntelHex+0x50>
mov    %r13d,%r15d
sar    $0x8,%r15d
add    %r13d,%r15d
add    %ebp,%r15d
test   %ebp,%ebp
jle    123b <parseIntelHex+0xdb>
movslq %r13d,%r14
add    0x10(%rsp),%r14
mov    %ebp,%r12d
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
mov    %al,(%r14,%rbp,1)
add    %eax,%r15d
add    $0x1,%rbp
cmp    %ebp,%r12d
jne    1210 <parseIntelHex+0xb0>
add    %r13d,%ebp
mov    0x8(%rsp),%r14
mov    (%rsp),%r12
jmp    123e <parseIntelHex+0xde>
mov    %r13d,%ebp
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <parseHex@plt>
add    %al,%r15b
je     126c <parseIntelHex+0x10c>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
mov    %r13d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    %r13d,(%r12)
jle    1276 <parseIntelHex+0x116>
mov    %r13d,(%r12)
cmp    %ebp,(%r14)
jge    11b0 <parseIntelHex+0x50>
mov    %ebp,(%r14)
jmp    11b0 <parseIntelHex+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    12c5 <parseIntelHex+0x165>
mov    0x2d46(%rip),%rax
mov    (%rax),%ebx
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd51(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [55 x i8] c"Warning: Checksum error between address 0x%x and 0x%x\0A\00"
define i32 @parseIntelHex(i8* %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %17
%20 = load i32*, i32** %17
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %4
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %6
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %26)
store i32 1, i32* %5
br label %109
28: ; preds = %4
br label %29
29: ; preds = %105, %54, %28
%30 = load i32*, i32** %17
%31 = call signext i8 @parseUntilColon(i32* %30)
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 58
br i1 %33, label %34, label %106
34: ; preds = %29
store i32 0, i32* %16
%35 = load i32*, i32** %17
%36 = call i32 @parseHex(i32* %35, i32 2)
store i32 %36, i32* %15
%37 = load i32, i32* %16
%38 = add nsw i32 %37, %36
store i32 %38, i32* %16
%39 = load i32*, i32** %17
%40 = call i32 @parseHex(i32* %39, i32 4)
store i32 %40, i32* %10
store i32 %40, i32* %11
%41 = load i32, i32* %10
%42 = ashr i32 %41, 8
%43 = load i32, i32* %16
%44 = add nsw i32 %43, %42
store i32 %44, i32* %16
%45 = load i32, i32* %10
%46 = load i32, i32* %16
%47 = add nsw i32 %46, %45
store i32 %47, i32* %16
%48 = load i32*, i32** %17
%49 = call i32 @parseHex(i32* %48, i32 2)
store i32 %49, i32* %13
%50 = load i32, i32* %16
%51 = add nsw i32 %50, %49
store i32 %51, i32* %16
%52 = load i32, i32* %13
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %34
br label %29
55: ; preds = %34
store i32 0, i32* %14
br label %56
56: ; preds = %73, %55
%57 = load i32, i32* %14
%58 = load i32, i32* %15
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %76
60: ; preds = %56
%61 = load i32*, i32** %17
%62 = call i32 @parseHex(i32* %61, i32 2)
store i32 %62, i32* %12
%63 = load i32, i32* %12
%64 = trunc i32 %63 to i8
%65 = load i8*, i8** %7
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
store i8 %64, i8* %69
%70 = load i32, i32* %12
%71 = load i32, i32* %16
%72 = add nsw i32 %71, %70
store i32 %72, i32* %16
br label %73
73: ; preds = %60
%74 = load i32, i32* %14
%75 = add nsw i32 %74, 1
store i32 %75, i32* %14
br label %56
76: ; preds = %56
%77 = load i32*, i32** %17
%78 = call i32 @parseHex(i32* %77, i32 2)
%79 = load i32, i32* %16
%80 = add nsw i32 %79, %78
store i32 %80, i32* %16
%81 = load i32, i32* %16
%82 = and i32 %81, 255
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %76
%85 = load i32, i32* @stderr
%86 = load i32, i32* %11
%87 = load i32, i32* %10
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %86, i32 %87)
br label %89
89: ; preds = %84, %76
%90 = load i32*, i32** %8
%91 = load i32, i32* %90
%92 = load i32, i32* %11
%93 = icmp sgt i32 %91, %92
br i1 %93, label %94, label %97
94: ; preds = %89
%95 = load i32, i32* %11
%96 = load i32*, i32** %8
store i32 %95, i32* %96
br label %97
97: ; preds = %94, %89
%98 = load i32*, i32** %9
%99 = load i32, i32* %98
%100 = load i32, i32* %10
%101 = icmp slt i32 %99, %100
br i1 %101, label %102, label %105
102: ; preds = %97
%103 = load i32, i32* %10
%104 = load i32*, i32** %9
store i32 %103, i32* %104
br label %105
105: ; preds = %102, %97
br label %29
106: ; preds = %29
%107 = load i32*, i32** %17
%108 = call i32 @fclose(i32* %107)
store i32 0, i32* %5
br label %109
109: ; preds = %106, %22
%110 = load i32, i32* %5
ret i32 %110
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"error opening %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [55 x i8] c"Warning: Checksum error between address 0x%x and 0x%x\0A\00"
define i32 @parseIntelHex(i8* %0, i8* nocapture writeonly %1, i32* nocapture %2, i32* nocapture %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call signext i8 @parseUntilColon(i32* %5)
%9 = icmp eq i8 %8, 58
br i1 %9, label %15, label %61
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %13)
br label %63
15: ; preds = %7, %58
%16 = tail call i32 @parseHex(i32* %5, i32 2)
%17 = tail call i32 @parseHex(i32* %5, i32 4)
%18 = tail call i32 @parseHex(i32* %5, i32 2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %58
20: ; preds = %15
%21 = ashr i32 %17, 8
%22 = add i32 %17, %16
%23 = add i32 %22, %21
%24 = icmp sgt i32 %16, 0
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = sext i32 %17 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %33, %27 ]
%29 = phi i32 [ %23, %25 ], [ %35, %27 ]
%30 = phi i32 [ 0, %25 ], [ %36, %27 ]
%31 = tail call i32 @parseHex(i32* %5, i32 2)
%32 = trunc i32 %31 to i8
%33 = add nsw i64 %28, 1
%34 = getelementptr inbounds i8, i8* %1, i64 %28
store i8 %32, i8* %34
%35 = add nsw i32 %31, %29
%36 = add nuw nsw i32 %30, 1
%37 = icmp eq i32 %36, %16
br i1 %37, label %38, label %27
38: ; preds = %27
%39 = trunc i64 %33 to i32
br label %40
40: ; preds = %38, %20
%41 = phi i32 [ %17, %20 ], [ %39, %38 ]
%42 = phi i32 [ %23, %20 ], [ %35, %38 ]
%43 = tail call i32 @parseHex(i32* %5, i32 2)
%44 = add nsw i32 %43, %42
%45 = and i32 %44, 255
%46 = icmp eq i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %40
%48 = load i32, i32* @stderr
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %41)
br label %50
50: ; preds = %47, %40
%51 = load i32, i32* %2
%52 = icmp sgt i32 %51, %17
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 %17, i32* %2
br label %54
54: ; preds = %53, %50
%55 = load i32, i32* %3
%56 = icmp slt i32 %55, %41
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 %41, i32* %3
br label %58
58: ; preds = %54, %57, %15
%59 = tail call signext i8 @parseUntilColon(i32* %5)
%60 = icmp eq i8 %59, 58
br i1 %60, label %15, label %61
61: ; preds = %58, %7
%62 = tail call i32 @fclose(i32* %5)
br label %63
63: ; preds = %61, %10
%64 = phi i32 [ 1, %10 ], [ 0, %61 ]
ret i32 %64
}