#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 const *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int readhex(int /*<<< orphan*/ *, unsigned long *, unsigned char *);
char *strerror(int /*<<< orphan*/);
char *read_hexfile(const char *filename, unsigned long *lastaddr) {
  char *data;
  FILE *fp;
  int len;
  int x;
  unsigned char line[256];
  unsigned long addr = 0;
  data = malloc(0x40000);
  if (data == NULL) {
    printf(
        "Memory allocation error, could not get %d bytes for flash-buffer!\n",
        0x40000);
    return NULL;
  }
  *lastaddr = 0;
  memset(data, 0xff, 0x40000);
  if (NULL == (fp = fopen(filename, "r"))) {
    printf("File \"%s\" open failed: %s!\n\n", filename, strerror(errno));
    free(data);
    return NULL;
  }
  printf("Reading       : %s... ", filename);
  while ((len = readhex(fp, &addr, line)) >= 0) {
    if (len) {
      if (addr + len > 0x40000) {
        fclose(fp);
        free(data);
        printf("\n  Hex-file too large for target!\n");
        return NULL;
      }
      for (x = 0; x < len; x++) {
        data[x + addr] = line[x];
      }
      addr += len;
      if (*lastaddr < (addr - 1)) {
        *lastaddr = addr - 1;
      }
      addr++;
    }
  }
  fclose(fp);
  printf("File read.\n");
  return data;
}


x86 assembly (clang, -O0):
<read_hexfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x138(%rbp)
mov    $0x40000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <read_hexfile+0x57>
lea    0xe42(%rip),%rdi
mov    $0x40000,%esi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1397 <read_hexfile+0x217>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    $0xff,%esi
mov    $0x40000,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    125e <read_hexfile+0xde>
mov    -0x10(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x140(%rbp),%rsi
mov    %rax,%rdx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1397 <read_hexfile+0x217>
mov    -0x10(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    -0x138(%rbp),%rsi
call   1080 <readhex@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     1378 <read_hexfile+0x1f8>
cmpl   $0x0,-0x2c(%rbp)
je     1373 <read_hexfile+0x1f3>
mov    -0x138(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
cmp    $0x40000,%rax
jbe    12e4 <read_hexfile+0x164>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1397 <read_hexfile+0x217>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1322 <read_hexfile+0x1a2>
movslq -0x30(%rbp),%rax
mov    -0x130(%rbp,%rax,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    -0x138(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12eb <read_hexfile+0x16b>
movslq -0x2c(%rbp),%rax
add    -0x138(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x138(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1361 <read_hexfile+0x1e1>
mov    -0x138(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x138(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x138(%rbp)
jmp    1270 <read_hexfile+0xf0>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hexfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r15
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
mov    $0x40000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     128c <read_hexfile+0xec>
mov    %rax,%r14
movq   $0x0,(%r15)
mov    %rax,%rdi
mov    $0xff,%esi
mov    $0x40000,%edx
call   1070 <memset@plt>
lea    0xe4d(%rip),%rsi
mov    %r12,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12ba <read_hexfile+0x11a>
mov    %rax,%rbx
lea    0xe51(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   10a0 <readhex@plt>
test   %eax,%eax
js     12a4 <read_hexfile+0x104>
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    125b <read_hexfile+0xbb>
nopl   (%rax)
add    $0x1,%rbp
mov    %rbp,0x8(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10a0 <readhex@plt>
test   %eax,%eax
js     12a4 <read_hexfile+0x104>
test   %eax,%eax
je     1249 <read_hexfile+0xa9>
mov    0x8(%rsp),%rdi
mov    %eax,%edx
lea    (%rdi,%rdx,1),%rbp
cmp    $0x40001,%rbp
jae    12e6 <read_hexfile+0x146>
add    %r14,%rdi
mov    %r13,%rsi
call   1080 <memcpy@plt>
lea    -0x1(%rbp),%rax
cmp    %rax,(%r15)
jae    1240 <read_hexfile+0xa0>
mov    %rax,(%r15)
jmp    1240 <read_hexfile+0xa0>
lea    0xd6d(%rip),%rdi
xor    %r14d,%r14d
mov    $0x40000,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1305 <read_hexfile+0x165>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xdc6(%rip),%rdi
call   1040 <puts@plt>
jmp    1305 <read_hexfile+0x165>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd76(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1302 <read_hexfile+0x162>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xd87(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [67 x i8] c"Memory allocation error, could not get %d bytes for flash-buffer!\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"File \22%s\22 open failed: %s!\0A\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Reading : %s... \00"
@.str.4 = [35 x i8] c"\0A Hex-file too large for target!\0A\00"
@.str.5 = [12 x i8] c"File read.\0A\00"
define i8* @read_hexfile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i64 0, i64* %11
%12 = call i8* @malloc(i32 262144)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i32 262144)
store i8* null, i8** %3
br label %96
17: ; preds = %2
%18 = load i64*, i64** %5
store i64 0, i64* %18
%19 = load i8*, i8** %6
%20 = call i32 @memset(i8* %19, i32 255, i32 262144)
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %7
%23 = icmp eq i32* null, %22
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %27)
%29 = load i8*, i8** %6
%30 = call i32 @free(i8* %29)
store i8* null, i8** %3
br label %96
31: ; preds = %17
%32 = load i8*, i8** %4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %90, %31
%35 = load i32*, i32** %7
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%37 = call i32 @readhex(i32* %35, i64* %11, i8* %36)
store i32 %37, i32* %8
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %91
39: ; preds = %34
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %90
42: ; preds = %39
%43 = load i64, i64* %11
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = add i64 %43, %45
%47 = icmp ugt i64 %46, 262144
br i1 %47, label %48, label %54
48: ; preds = %42
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %3
br label %96
54: ; preds = %42
store i32 0, i32* %9
br label %55
55: ; preds = %70, %54
%56 = load i32, i32* %9
%57 = load i32, i32* %8
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %73
59: ; preds = %55
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = load i8*, i8** %6
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = load i64, i64* %11
%68 = add i64 %66, %67
%69 = getelementptr inbounds i8, i8* %64, i64 %68
store i8 %63, i8* %69
br label %70
70: ; preds = %59
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %55
73: ; preds = %55
%74 = load i32, i32* %8
%75 = sext i32 %74 to i64
%76 = load i64, i64* %11
%77 = add i64 %76, %75
store i64 %77, i64* %11
%78 = load i64*, i64** %5
%79 = load i64, i64* %78
%80 = load i64, i64* %11
%81 = sub i64 %80, 1
%82 = icmp ult i64 %79, %81
br i1 %82, label %83, label %87
83: ; preds = %73
%84 = load i64, i64* %11
%85 = sub i64 %84, 1
%86 = load i64*, i64** %5
store i64 %85, i64* %86
br label %87
87: ; preds = %83, %73
%88 = load i64, i64* %11
%89 = add i64 %88, 1
store i64 %89, i64* %11
br label %90
90: ; preds = %87, %39
br label %34
91: ; preds = %34
%92 = load i32*, i32** %7
%93 = call i32 @fclose(i32* %92)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%95 = load i8*, i8** %6
store i8* %95, i8** %3
br label %96
96: ; preds = %91, %48, %24, %15
%97 = load i8*, i8** %3
ret i8* %97
}

LLVM IR (-O2):
@.str = [67 x i8] c"Memory allocation error, could not get %d bytes for flash-buffer!\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [29 x i8] c"File \22%s\22 open failed: %s!\0A\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Reading : %s... \00"
@str = [11 x i8] c"File read.\00"
@str.6 = [34 x i8] c"\0A Hex-file too large for target!\00"
define i8* @read_hexfile(i8* %0, i64* nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = alloca i64
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%8 = tail call dereferenceable_or_null(262144) i8* @malloc(i32 262144)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i32 262144)
br label %51
12: ; preds = %2
store i64 0, i64* %1
%13 = tail call i32 @memset(i8* %8, i32 255, i32 262144)
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %18)
%20 = tail call i32 @free(i8* %8)
br label %51
21: ; preds = %12
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%23 = call i32 @readhex(i32* %14, i64* %5, i8* %6)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %48
25: ; preds = %21, %45
%26 = phi i32 [ %46, %45 ], [ %23, %21 ]
%27 = icmp eq i32 %26, 0
br i1 %27, label %45, label %28
28: ; preds = %25
%29 = load i64, i64* %5
%30 = zext i32 %26 to i64
%31 = add i64 %29, %30
%32 = icmp ugt i64 %31, 262144
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = call i32 @fclose(i32* %14)
%35 = call i32 @free(i8* %8)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
br label %51
37: ; preds = %28
%38 = getelementptr i8, i8* %8, i64 %29
%39 = load i64, i64* %1
%40 = add nsw i64 %31, -1
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %37
store i64 %40, i64* %1
br label %43
43: ; preds = %42, %37
%44 = add nuw nsw i64 %31, 1
store i64 %44, i64* %5
br label %45
45: ; preds = %43, %25
%46 = call i32 @readhex(i32* %14, i64* %5, i8* %6)
%47 = icmp sgt i32 %46, -1
br i1 %47, label %25, label %48
48: ; preds = %45, %21
%49 = call i32 @fclose(i32* %14)
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %51
51: ; preds = %48, %33, %16, %10
%52 = phi i8* [ null, %10 ], [ null, %16 ], [ null, %33 ], [ %8, %48 ]
ret i8* %52
}