#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 */
scalar_t__ EOF;
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
scalar_t__ malloc(unsigned int);
scalar_t__ realloc(unsigned int *, unsigned int);
unsigned int *readhexfile(char *fn, unsigned int *blen) {
  FILE *f;
  unsigned int *buf;
  unsigned int len = 0, size = 65536;
  unsigned int X;
  f = fopen(fn, "r");
  if (!f)
    error("Cannot open hex file");
  buf = (unsigned int *)malloc(size * 4);
  while (fscanf(f, "%x\n", &X) != EOF) {
    buf[len++] = X;
    if (len > size - 10) {
      size *= 2;
      buf = (unsigned int *)realloc(buf, size * 4);
    }
  }
  fclose(f);
  *blen = len;
  return buf;
}


x86 assembly (clang, -O0):
<readhexfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x10000,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a9 <readhexfile+0x49>
lea    0xe5e(%rip),%rdi
call   1070 <error@plt>
mov    -0x28(%rbp),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe54(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
je     1225 <readhexfile+0xc5>
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
sub    $0xa,%ecx
cmp    %ecx,%eax
jbe    1220 <readhexfile+0xc0>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
shl    $0x2,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11b8 <readhexfile+0x58>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhexfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1191 <readhexfile+0x31>
lea    0xe76(%rip),%rdi
call   1070 <error@plt>
mov    $0x40000,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
lea    0xe72(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
call   1040 <fscanf@plt>
mov    0x2e2f(%rip),%rbx
cmp    (%rbx),%rax
mov    %rbp,0x10(%rsp)
jne    11e8 <readhexfile+0x88>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %r14d,(%rax)
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    $0x10000,%ebp
lea    0xe20(%rip),%r12
jmp    1215 <readhexfile+0xb5>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r12,%rsi
lea    0xc(%rsp),%rdx
call   1040 <fscanf@plt>
cmp    (%rbx),%rax
je     11c6 <readhexfile+0x66>
mov    0xc(%rsp),%eax
mov    %r14d,%ecx
add    $0x1,%r14d
mov    %eax,0x0(%r13,%rcx,4)
lea    -0xa(%rbp),%eax
cmp    %eax,%r14d
jbe    1200 <readhexfile+0xa0>
mov    %r13,%rdi
mov    %r15,%r13
mov    %r12,%r15
lea    0x0(%rbp,%rbp,1),%r12d
shl    $0x3,%ebp
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %r12d,%ebp
mov    %r15,%r12
mov    %r13,%r15
mov    %rax,%r13
jmp    1200 <readhexfile+0xa0>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [21 x i8] c"Cannot open hex file\00"
@.str.2 = [4 x i8] c"%x\0A\00"
@EOF = global i64 0
define i32* @readhexfile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %7
store i32 65536, i32* %8
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %8
%18 = mul i32 %17, 4
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %6
br label %21
21: ; preds = %45, %16
%22 = load i32*, i32** %5
%23 = call i64 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%24 = load i64, i64* @EOF
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %46
26: ; preds = %21
%27 = load i32, i32* %9
%28 = load i32*, i32** %6
%29 = load i32, i32* %7
%30 = add i32 %29, 1
store i32 %30, i32* %7
%31 = zext i32 %29 to i64
%32 = getelementptr inbounds i32, i32* %28, i64 %31
store i32 %27, i32* %32
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = sub i32 %34, 10
%36 = icmp ugt i32 %33, %35
br i1 %36, label %37, label %45
37: ; preds = %26
%38 = load i32, i32* %8
%39 = mul i32 %38, 2
store i32 %39, i32* %8
%40 = load i32*, i32** %6
%41 = load i32, i32* %8
%42 = mul i32 %41, 4
%43 = call i64 @realloc(i32* %40, i32 %42)
%44 = inttoptr i64 %43 to i32*
store i32* %44, i32** %6
br label %45
45: ; preds = %37, %26
br label %21
46: ; preds = %21
%47 = load i32*, i32** %5
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %7
%50 = load i32*, i32** %4
store i32 %49, i32* %50
%51 = load i32*, i32** %6
ret i32* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [21 x i8] c"Cannot open hex file\00"
@.str.2 = [4 x i8] c"%x\0A\00"
@EOF = global i64 0
define i32* @readhexfile(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%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 %7, label %9
7: ; preds = %2
%8 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i64 @malloc(i32 262144)
%11 = inttoptr i64 %10 to i32*
%12 = call i64 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %36, label %15
15: ; preds = %9, %30
%16 = phi i32 [ %32, %30 ], [ 65536, %9 ]
%17 = phi i32 [ %20, %30 ], [ 0, %9 ]
%18 = phi i32* [ %31, %30 ], [ %11, %9 ]
%19 = load i32, i32* %3
%20 = add i32 %17, 1
%21 = zext i32 %17 to i64
%22 = getelementptr inbounds i32, i32* %18, i64 %21
store i32 %19, i32* %22
%23 = add i32 %16, -10
%24 = icmp ugt i32 %20, %23
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = shl i32 %16, 1
%27 = shl i32 %16, 3
%28 = call i64 @realloc(i32* %18, i32 %27)
%29 = inttoptr i64 %28 to i32*
br label %30
30: ; preds = %25, %15
%31 = phi i32* [ %29, %25 ], [ %18, %15 ]
%32 = phi i32 [ %26, %25 ], [ %16, %15 ]
%33 = call i64 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%34 = load i64, i64* @EOF
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %15
36: ; preds = %30, %9
%37 = phi i32* [ %11, %9 ], [ %31, %30 ]
%38 = phi i32 [ 0, %9 ], [ %20, %30 ]
%39 = call i32 @fclose(i32* %5)
store i32 %38, i32* %1
ret i32* %37
}