#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ perror(char *);
unsigned int strtoul(char *, int /*<<< orphan*/ *, int);
int read_hex_file(const char *filename, unsigned char **out_ptr,
                  int *out_length) {
  FILE *input_file;
  char line[16];
  int offset = 0;
  unsigned char *data;
  int file_length;
  input_file = fopen(filename, "r");
  if (!input_file) {
    perror("Error opening input file\n");
    return 0;
  }
  fseek(input_file, 0, SEEK_END);
  file_length = ftell(input_file);
  fseek(input_file, 0, SEEK_SET);
  data = malloc(file_length / 2);
  while (fgets(line, sizeof(line), input_file)) {
    unsigned int value = strtoul(line, NULL, 16);
    data[offset++] = (value >> 24) & 0xff;
    data[offset++] = (value >> 16) & 0xff;
    data[offset++] = (value >> 8) & 0xff;
    data[offset++] = value & 0xff;
  }
  *out_ptr = data;
  *out_length = offset;
  fclose(input_file);
  return 1;
}


x86 assembly (clang, -O0):
<read_hex_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d2 <read_hex_file+0x52>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <read_hex_file+0x17b>
mov    -0x28(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x54(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x10,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12d7 <read_hex_file+0x157>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x50(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x44(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x50(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x44(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x50(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x44(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x50(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x44(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    121c <read_hex_file+0x9c>
mov    -0x50(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hex_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1261 <read_hex_file+0xe1>
mov    %rax,%r12
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebx
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %ebx,%edi
shr    $0x1f,%edi
add    %ebx,%edi
sar    %edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123f <read_hex_file+0xbf>
xor    %ebp,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
bswap  %eax
mov    %eax,0x0(%r13,%rbp,1)
add    $0x4,%rbp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1210 <read_hex_file+0x90>
mov    %r13,(%r15)
mov    %ebp,(%r14)
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
jmp    1252 <read_hex_file+0xd2>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Error opening input file\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_hex_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [16 x i8]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32 0, i32* %10
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %82
20: ; preds = %3
%21 = load i32*, i32** %8
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %8
%25 = call i32 @ftell(i32* %24)
store i32 %25, i32* %12
%26 = load i32*, i32** %8
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i32, i32* %12
%30 = sdiv i32 %29, 2
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %11
br label %32
32: ; preds = %37, %20
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%34 = load i32*, i32** %8
%35 = call i64 @fgets(i8* %33, i32 16, i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %75
37: ; preds = %32
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%39 = call i32 @strtoul(i8* %38, i32* null, i32 16)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = lshr i32 %40, 24
%42 = and i32 %41, 255
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %11
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 %43, i8* %48
%49 = load i32, i32* %13
%50 = lshr i32 %49, 16
%51 = and i32 %50, 255
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %11
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
store i32 %55, i32* %10
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 %52, i8* %57
%58 = load i32, i32* %13
%59 = lshr i32 %58, 8
%60 = and i32 %59, 255
%61 = trunc i32 %60 to i8
%62 = load i8*, i8** %11
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %62, i64 %65
store i8 %61, i8* %66
%67 = load i32, i32* %13
%68 = and i32 %67, 255
%69 = trunc i32 %68 to i8
%70 = load i8*, i8** %11
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 1
store i32 %72, i32* %10
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
store i8 %69, i8* %74
br label %32
75: ; preds = %32
%76 = load i8*, i8** %11
%77 = load i8**, i8*** %6
store i8* %76, i8** %77
%78 = load i32, i32* %10
%79 = load i32*, i32** %7
store i32 %78, i32* %79
%80 = load i32*, i32** %8
%81 = call i32 @fclose(i32* %80)
store i32 1, i32* %4
br label %82
82: ; preds = %75, %18
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Error opening input file\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_hex_file(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %45
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %6, i32 0, i32 %11)
%13 = tail call i32 @ftell(i32* %6)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %6, i32 0, i32 %14)
%16 = sdiv i32 %13, 2
%17 = tail call i8* @malloc(i32 %16)
%18 = call i64 @fgets(i8* %5, i32 16, i32* %6)
%19 = icmp eq i64 %18, 0
br i1 %19, label %42, label %20
20: ; preds = %10, %20
%21 = phi i64 [ %36, %20 ], [ 0, %10 ]
%22 = call i32 @strtoul(i8* nocapture %5, i32* null, i32 16)
%23 = lshr i32 %22, 24
%24 = trunc i32 %23 to i8
%25 = or i64 %21, 1
%26 = getelementptr inbounds i8, i8* %17, i64 %21
store i8 %24, i8* %26
%27 = lshr i32 %22, 16
%28 = trunc i32 %27 to i8
%29 = or i64 %21, 2
%30 = getelementptr inbounds i8, i8* %17, i64 %25
store i8 %28, i8* %30
%31 = lshr i32 %22, 8
%32 = trunc i32 %31 to i8
%33 = or i64 %21, 3
%34 = getelementptr inbounds i8, i8* %17, i64 %29
store i8 %32, i8* %34
%35 = trunc i32 %22 to i8
%36 = add nuw i64 %21, 4
%37 = getelementptr inbounds i8, i8* %17, i64 %33
store i8 %35, i8* %37
%38 = call i64 @fgets(i8* %5, i32 16, i32* %6)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %20
40: ; preds = %20
%41 = trunc i64 %36 to i32
br label %42
42: ; preds = %40, %10
%43 = phi i32 [ 0, %10 ], [ %41, %40 ]
store i8* %17, i8** %1
store i32 %43, i32* %2
%44 = call i32 @fclose(i32* %6)
br label %45
45: ; preds = %42, %8
%46 = phi i32 [ 1, %42 ], [ 0, %8 ]
ret i32 %46
}