#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*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int);
int read_hex_file(const char *filename, unsigned char **out_ptr,
                  unsigned int *out_length) {
  FILE *input_file;
  char line[16];
  unsigned int offset = 0;
  unsigned char *data;
  unsigned 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 = (unsigned int)ftell(input_file);
  fseek(input_file, 0, SEEK_SET);
  data = malloc(file_length / 2);
  while (fgets(line, sizeof(line), input_file)) {
    unsigned int value = (unsigned int)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    12f0 <read_hex_file+0x170>
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),%edi
shr    $0x1,%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     12cc <read_hex_file+0x14c>
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)
mov    %ecx,%ecx
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)
mov    %ecx,%ecx
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)
mov    %ecx,%ecx
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)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    1215 <read_hex_file+0x95>
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     1282 <read_hex_file+0x102>
mov    %rax,%r12
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
call   1050 <ftell@plt>
mov    %rax,%rbp
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
shr    %ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1260 <read_hex_file+0xe0>
xor    %ebx,%ebx
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %rax,%rcx
shr    $0x18,%rcx
lea    0x1(%rbx),%edx
mov    %ebx,%esi
mov    %cl,0x0(%rbp,%rsi,1)
mov    %rax,%rcx
shr    $0x10,%rcx
lea    0x2(%rbx),%esi
mov    %cl,0x0(%rbp,%rdx,1)
lea    0x3(%rbx),%ecx
mov    %ah,0x0(%rbp,%rsi,1)
add    $0x4,%ebx
mov    %al,0x0(%rbp,%rcx,1)
mov    %r13,%rdi
mov    $0x10,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1210 <read_hex_file+0x90>
mov    %rbp,(%r15)
mov    %ebx,(%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    0xd79(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
jmp    1273 <read_hex_file+0xf3>

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 %84
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 i64 @ftell(i32* %24)
%26 = trunc i64 %25 to i32
store i32 %26, i32* %12
%27 = load i32*, i32** %8
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32, i32* %12
%31 = udiv i32 %30, 2
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %11
br label %33
33: ; preds = %38, %20
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%35 = load i32*, i32** %8
%36 = call i64 @fgets(i8* %34, i32 16, i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %77
38: ; preds = %33
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%40 = call i64 @strtoul(i8* %39, i32* null, i32 16)
%41 = trunc i64 %40 to i32
store i32 %41, i32* %13
%42 = load i32, i32* %13
%43 = lshr i32 %42, 24
%44 = and i32 %43, 255
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %11
%47 = load i32, i32* %10
%48 = add i32 %47, 1
store i32 %48, i32* %10
%49 = zext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 %45, i8* %50
%51 = load i32, i32* %13
%52 = lshr i32 %51, 16
%53 = and i32 %52, 255
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %11
%56 = load i32, i32* %10
%57 = add i32 %56, 1
store i32 %57, i32* %10
%58 = zext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 %54, i8* %59
%60 = load i32, i32* %13
%61 = lshr i32 %60, 8
%62 = and i32 %61, 255
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %11
%65 = load i32, i32* %10
%66 = add i32 %65, 1
store i32 %66, i32* %10
%67 = zext i32 %65 to i64
%68 = getelementptr inbounds i8, i8* %64, i64 %67
store i8 %63, i8* %68
%69 = load i32, i32* %13
%70 = and i32 %69, 255
%71 = trunc i32 %70 to i8
%72 = load i8*, i8** %11
%73 = load i32, i32* %10
%74 = add i32 %73, 1
store i32 %74, i32* %10
%75 = zext i32 %73 to i64
%76 = getelementptr inbounds i8, i8* %72, i64 %75
store i8 %71, i8* %76
br label %33
77: ; preds = %33
%78 = load i8*, i8** %11
%79 = load i8**, i8*** %6
store i8* %78, i8** %79
%80 = load i32, i32* %10
%81 = load i32*, i32** %7
store i32 %80, i32* %81
%82 = load i32*, i32** %8
%83 = call i32 @fclose(i32* %82)
store i32 1, i32* %4
br label %84
84: ; preds = %77, %18
%85 = load i32, i32* %4
ret i32 %85
}

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 %48
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %6, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %6)
%14 = trunc i64 %13 to i32
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %6, i32 0, i32 %15)
%17 = lshr i32 %14, 1
%18 = tail call i8* @malloc(i32 %17)
%19 = call i64 @fgets(i8* %5, i32 16, i32* %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %45, label %21
21: ; preds = %10, %21
%22 = phi i32 [ %40, %21 ], [ 0, %10 ]
%23 = call i64 @strtoul(i8* nocapture %5, i32* null, i32 16)
%24 = trunc i64 %23 to i8
%25 = lshr i64 %23, 24
%26 = trunc i64 %25 to i8
%27 = or i32 %22, 1
%28 = zext i32 %22 to i64
%29 = getelementptr inbounds i8, i8* %18, i64 %28
store i8 %26, i8* %29
%30 = lshr i64 %23, 16
%31 = trunc i64 %30 to i8
%32 = or i32 %22, 2
%33 = zext i32 %27 to i64
%34 = getelementptr inbounds i8, i8* %18, i64 %33
store i8 %31, i8* %34
%35 = lshr i64 %23, 8
%36 = trunc i64 %35 to i8
%37 = or i32 %22, 3
%38 = zext i32 %32 to i64
%39 = getelementptr inbounds i8, i8* %18, i64 %38
store i8 %36, i8* %39
%40 = add i32 %22, 4
%41 = zext i32 %37 to i64
%42 = getelementptr inbounds i8, i8* %18, i64 %41
store i8 %24, i8* %42
%43 = call i64 @fgets(i8* %5, i32 16, i32* %6)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %21
45: ; preds = %21, %10
%46 = phi i32 [ 0, %10 ], [ %40, %21 ]
store i8* %18, i8** %1
store i32 %46, i32* %2
%47 = call i32 @fclose(i32* %6)
br label %48
48: ; preds = %45, %8
%49 = phi i32 [ 1, %45 ], [ 0, %8 ]
ret i32 %49
}