#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 fscanf(int /*<<< orphan*/ *, char *, int *, ...);
int getc(int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
int readhexline(FILE *fd, void **data, size_t *len, long *address) {
  int checksum1, checksum2;
  int length, addr1, addr2, type;
  int j, byte;
  do {
    checksum1 = 0;
    if (fscanf(fd, ":%2x%2x%2x%2x", &length, &addr1, &addr2, &type) < 3) {
      return -2;
    }
  } while (type == 2);
  if (type == 0x01)
    return -3;
  if (type != 0x00) {
    return -4;
  }
  checksum1 += length;
  checksum1 += type;
  checksum1 += addr1;
  checksum1 += addr2;
  *address = addr1 * 0x100 + addr2;
  *data = malloc(length);
  *len = length;
  for (j = 0; j < length; j++) {
    if (fscanf(fd, "%2x", &byte) < 1) {
      return -2;
    }
    checksum1 += byte;
    ((unsigned char *)(*data))[j] = byte;
  }
  if (fscanf(fd, "%2x", &checksum2) < 1) {
    return -2;
  }
  checksum1 &= 0xFF;
  checksum1 = (~checksum1 + 1) & 0xFF;
  if (checksum1 != checksum2) {
    return -1;
  }
  while (!feof(fd)) {
    byte = getc(fd);
    if (byte != '\n' && byte != '\r') {
      ungetc(byte, fd);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<readhexline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe86(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x3c(%rbp),%r8
lea    -0x40(%rbp),%r9
mov    $0x0,%al
call   1030 <fscanf@plt>
cmp    $0x3,%eax
jge    11a6 <readhexline+0x56>
movl   $0xfffffffe,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
jmp    11ab <readhexline+0x5b>
cmpl   $0x2,-0x40(%rbp)
je     1168 <readhexline+0x18>
cmpl   $0x1,-0x40(%rbp)
jne    11cb <readhexline+0x7b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
cmpl   $0x0,-0x40(%rbp)
je     11e1 <readhexline+0x91>
movl   $0xfffffffc,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
mov    -0x34(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
shl    $0x8,%eax
add    -0x3c(%rbp),%eax
movslq %eax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x34(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    129d <readhexline+0x14d>
mov    -0x10(%rbp),%rdi
lea    0xdb7(%rip),%rsi
lea    -0x48(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
cmp    $0x1,%eax
jge    1273 <readhexline+0x123>
movl   $0xfffffffe,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
mov    -0x48(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x48(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x44(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    123c <readhexline+0xec>
mov    -0x10(%rbp),%rdi
lea    0xd62(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1030 <fscanf@plt>
cmp    $0x1,%eax
jge    12c8 <readhexline+0x178>
movl   $0xfffffffe,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
xor    $0xffffffff,%eax
add    $0x1,%eax
and    $0xff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     12fc <readhexline+0x1ac>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <readhexline+0x20c>
jmp    1301 <readhexline+0x1b1>
mov    -0x10(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    131f <readhexline+0x1cf>
jmp    1355 <readhexline+0x205>
mov    -0x10(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xa,-0x48(%rbp)
je     1350 <readhexline+0x200>
cmpl   $0xd,-0x48(%rbp)
je     1350 <readhexline+0x200>
mov    -0x48(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <ungetc@plt>
jmp    1355 <readhexline+0x205>
jmp    1301 <readhexline+0x1b1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhexline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x28(%rsp)
mov    %rdx,0x30(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rdi,%r14
lea    0xc(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0x14(%rsp),%rbx
lea    0x10(%rsp),%r13
mov    $0xfffffffe,%ebp
nopl   0x0(%rax)
mov    %r14,%rdi
lea    0xe66(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbx,%r8
mov    %r13,%r9
xor    %eax,%eax
call   1030 <fscanf@plt>
cmp    $0x3,%eax
jl     12ca <readhexline+0x17a>
mov    0x10(%rsp),%eax
cmp    $0x2,%eax
je     1190 <readhexline+0x40>
cmp    $0x1,%eax
je     12be <readhexline+0x16e>
test   %eax,%eax
jne    12c5 <readhexline+0x175>
movslq 0xc(%rsp),%rbx
mov    0x18(%rsp),%eax
lea    (%rax,%rbx,1),%r13d
mov    0x14(%rsp),%ecx
add    %ecx,%r13d
shl    $0x8,%eax
add    %ecx,%eax
cltq
mov    0x28(%rsp),%rcx
mov    %rax,(%rcx)
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    0x20(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x30(%rsp),%rax
mov    %rbx,(%rax)
test   %rbx,%rbx
jle    1258 <readhexline+0x108>
lea    0xdf4(%rip),%r15
lea    0x8(%rsp),%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <fscanf@plt>
test   %eax,%eax
jle    12ca <readhexline+0x17a>
mov    0x8(%rsp),%eax
add    %eax,%r13d
mov    0x20(%rsp),%rcx
mov    (%rcx),%rcx
mov    %al,(%rcx,%rbx,1)
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1220 <readhexline+0xd0>
lea    0xdab(%rip),%rsi
lea    0x1c(%rsp),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <fscanf@plt>
test   %eax,%eax
jle    12ca <readhexline+0x17a>
neg    %r13d
movzbl %r13b,%eax
mov    $0xffffffff,%ebp
cmp    0x1c(%rsp),%eax
jne    12ca <readhexline+0x17a>
mov    %r14,%rdi
call   1040 <feof@plt>
xor    %ebp,%ebp
jmp    1298 <readhexline+0x148>
mov    %r14,%rdi
call   1040 <feof@plt>
test   %eax,%eax
jne    12ca <readhexline+0x17a>
mov    %r14,%rdi
call   1070 <getc@plt>
mov    %eax,0x8(%rsp)
cmp    $0xd,%eax
je     1290 <readhexline+0x140>
cmp    $0xa,%eax
je     1290 <readhexline+0x140>
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <ungetc@plt>
jmp    12ca <readhexline+0x17a>
mov    $0xfffffffd,%ebp
jmp    12ca <readhexline+0x17a>
mov    $0xfffffffc,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c":%2x%2x%2x%2x\00"
@.str.1 = [4 x i8] c"%2x\00"
define i32 @readhexline(i32* %0, i8** %1, i64* %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i64*
%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 i32* %0, i32** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i64* %3, i64** %9
br label %18
18: ; preds = %24, %4
store i32 0, i32* %10
%19 = load i32*, i32** %6
%20 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %12, i32* %13, i32* %14, i32* %15)
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -2, i32* %5
br label %119
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = load i32, i32* %15
%26 = icmp eq i32 %25, 2
br i1 %26, label %18, label %27
27: ; preds = %24
%28 = load i32, i32* %15
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -3, i32* %5
br label %119
31: ; preds = %27
%32 = load i32, i32* %15
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 -4, i32* %5
br label %119
35: ; preds = %31
%36 = load i32, i32* %12
%37 = load i32, i32* %10
%38 = add nsw i32 %37, %36
store i32 %38, i32* %10
%39 = load i32, i32* %15
%40 = load i32, i32* %10
%41 = add nsw i32 %40, %39
store i32 %41, i32* %10
%42 = load i32, i32* %13
%43 = load i32, i32* %10
%44 = add nsw i32 %43, %42
store i32 %44, i32* %10
%45 = load i32, i32* %14
%46 = load i32, i32* %10
%47 = add nsw i32 %46, %45
store i32 %47, i32* %10
%48 = load i32, i32* %13
%49 = mul nsw i32 %48, 256
%50 = load i32, i32* %14
%51 = add nsw i32 %49, %50
%52 = sext i32 %51 to i64
%53 = load i64*, i64** %9
store i64 %52, i64* %53
%54 = load i32, i32* %12
%55 = call i8* @malloc(i32 %54)
%56 = load i8**, i8*** %7
store i8* %55, i8** %56
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = load i64*, i64** %8
store i64 %58, i64* %59
store i32 0, i32* %16
br label %60
60: ; preds = %80, %35
%61 = load i32, i32* %16
%62 = load i32, i32* %12
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %83
64: ; preds = %60
%65 = load i32*, i32** %6
%66 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%67 = icmp slt i32 %66, 1
br i1 %67, label %68, label %69
68: ; preds = %64
store i32 -2, i32* %5
br label %119
69: ; preds = %64
%70 = load i32, i32* %17
%71 = load i32, i32* %10
%72 = add nsw i32 %71, %70
store i32 %72, i32* %10
%73 = load i32, i32* %17
%74 = trunc i32 %73 to i8
%75 = load i8**, i8*** %7
%76 = load i8*, i8** %75
%77 = load i32, i32* %16
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8 %74, i8* %79
br label %80
80: ; preds = %69
%81 = load i32, i32* %16
%82 = add nsw i32 %81, 1
store i32 %82, i32* %16
br label %60
83: ; preds = %60
%84 = load i32*, i32** %6
%85 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %84, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %11)
%86 = icmp slt i32 %85, 1
br i1 %86, label %87, label %88
87: ; preds = %83
store i32 -2, i32* %5
br label %119
88: ; preds = %83
%89 = load i32, i32* %10
%90 = and i32 %89, 255
store i32 %90, i32* %10
%91 = load i32, i32* %10
%92 = xor i32 %91, -1
%93 = add nsw i32 %92, 1
%94 = and i32 %93, 255
store i32 %94, i32* %10
%95 = load i32, i32* %10
%96 = load i32, i32* %11
%97 = icmp ne i32 %95, %96
br i1 %97, label %98, label %99
98: ; preds = %88
store i32 -1, i32* %5
br label %119
99: ; preds = %88
br label %100
100: ; preds = %117, %99
%101 = load i32*, i32** %6
%102 = call i32 @feof(i32* %101)
%103 = icmp ne i32 %102, 0
%104 = xor i1 %103, true
br i1 %104, label %105, label %118
105: ; preds = %100
%106 = load i32*, i32** %6
%107 = call i32 @getc(i32* %106)
store i32 %107, i32* %17
%108 = load i32, i32* %17
%109 = icmp ne i32 %108, 10
br i1 %109, label %110, label %117
110: ; preds = %105
%111 = load i32, i32* %17
%112 = icmp ne i32 %111, 13
br i1 %112, label %113, label %117
113: ; preds = %110
%114 = load i32, i32* %17
%115 = load i32*, i32** %6
%116 = call i32 @ungetc(i32 %114, i32* %115)
br label %118
117: ; preds = %110, %105
br label %100
118: ; preds = %113, %100
store i32 0, i32* %5
br label %119
119: ; preds = %118, %98, %87, %68, %34, %30, %22
%120 = load i32, i32* %5
ret i32 %120
}

LLVM IR (-O2):
@.str = [14 x i8] c":%2x%2x%2x%2x\00"
@.str.1 = [4 x i8] c"%2x\00"
define i32 @readhexline(i32* nocapture %0, i8** nocapture %1, i64* nocapture writeonly %2, i64* nocapture writeonly %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = bitcast i32* %5 to i8*
%12 = bitcast i32* %6 to i8*
%13 = bitcast i32* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = bitcast i32* %9 to i8*
%16 = bitcast i32* %10 to i8*
br label %17
17: ; preds = %20, %4
%18 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %7, i32* %8, i32* %9)
%19 = icmp slt i32 %18, 3
br i1 %19, label %68, label %20
20: ; preds = %17
%21 = load i32, i32* %9
switch i32 %21, label %70 [
i32 2, label %17
i32 1, label %68
i32 0, label %22
]
22: ; preds = %20
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = add nsw i32 %24, %23
%26 = load i32, i32* %8
%27 = add nsw i32 %25, %26
%28 = shl nsw i32 %24, 8
%29 = add nsw i32 %28, %26
%30 = sext i32 %29 to i64
store i64 %30, i64* %3
%31 = call i8* @malloc(i32 %23)
store i8* %31, i8** %1
%32 = sext i32 %23 to i64
store i64 %32, i64* %2
%33 = icmp sgt i32 %23, 0
br i1 %33, label %34, label %49
34: ; preds = %22, %39
%35 = phi i64 [ %45, %39 ], [ 0, %22 ]
%36 = phi i32 [ %41, %39 ], [ %27, %22 ]
%37 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %10)
%38 = icmp slt i32 %37, 1
br i1 %38, label %70, label %39
39: ; preds = %34
%40 = load i32, i32* %10
%41 = add nsw i32 %40, %36
%42 = trunc i32 %40 to i8
%43 = load i8*, i8** %1
%44 = getelementptr inbounds i8, i8* %43, i64 %35
store i8 %42, i8* %44
%45 = add nuw nsw i64 %35, 1
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = icmp slt i64 %45, %47
br i1 %48, label %34, label %49
49: ; preds = %39, %22
%50 = phi i32 [ %27, %22 ], [ %41, %39 ]
%51 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%52 = icmp slt i32 %51, 1
br i1 %52, label %70, label %53
53: ; preds = %49
%54 = sub i32 0, %50
%55 = and i32 %54, 255
%56 = load i32, i32* %5
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %70
58: ; preds = %53
%59 = call i32 @feof(i32* %0)
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %70
61: ; preds = %58, %63
%62 = call i32 @getc(i32* %0)
store i32 %62, i32* %10
switch i32 %62, label %66 [
i32 13, label %63
i32 10, label %63
]
63: ; preds = %61, %61
%64 = call i32 @feof(i32* %0)
%65 = icmp eq i32 %64, 0
br i1 %65, label %61, label %70
66: ; preds = %61
%67 = call i32 @ungetc(i32 %62, i32* %0)
br label %70
68: ; preds = %17, %20
%69 = phi i32 [ -3, %20 ], [ -2, %17 ]
br label %70
70: ; preds = %34, %63, %20, %68, %58, %66, %53, %49
%71 = phi i32 [ -2, %49 ], [ -1, %53 ], [ 0, %66 ], [ 0, %58 ], [ %69, %68 ], [ -4, %20 ], [ 0, %63 ], [ -2, %34 ]
ret i32 %71
}