#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ perror(char *);
bool read_binary_file(const char *filename, unsigned char **out_ptr,
                      unsigned int *out_length) {
  FILE *input_file;
  unsigned char *data;
  unsigned int file_length;
  input_file = fopen(filename, "r");
  if (!input_file) {
    perror("Error opening input file");
    return false;
  }
  fseek(input_file, 0, SEEK_END);
  file_length = (unsigned int)ftell(input_file);
  fseek(input_file, 0, SEEK_SET);
  data = malloc(file_length);
  if (fread(data, file_length, 1, input_file) != 1) {
    perror("Error reading file");
    fclose(input_file);
    free(data);
    return false;
  }
  *out_ptr = data;
  *out_length = file_length;
  fclose(input_file);
  return true;
}


x86 assembly (clang, -O0):
<read_binary_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cb <read_binary_file+0x4b>
lea    0xe48(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <read_binary_file+0xf7>
mov    -0x28(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x34(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1253 <read_binary_file+0xd3>
lea    0xdeb(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <read_binary_file+0xf7>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_binary_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1220 <read_binary_file+0xa0>
mov    %rax,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%rbp
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1231 <read_binary_file+0xb1>
mov    %r13,(%r15)
mov    %ebp,(%r14)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
xor    %r12d,%r12d
jmp    120e <read_binary_file+0x8e>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    120e <read_binary_file+0x8e>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"Error opening input file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"Error reading file\00"
define i32 @read_binary_file(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %24
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %4)
%12 = trunc i64 %11 to i32
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %4, i32 0, i32 %13)
%15 = tail call i8* @malloc(i32 %12)
%16 = tail call i32 @fread(i8* %15, i32 %12, i32 1, i32* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %22, label %18
18: ; preds = %8
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @fclose(i32* %4)
%21 = tail call i32 @free(i8* %15)
br label %24
22: ; preds = %8
store i8* %15, i8** %1
store i32 %12, i32* %2
%23 = tail call i32 @fclose(i32* %4)
br label %24
24: ; preds = %22, %18, %6
%25 = phi i32 [ 0, %18 ], [ 1, %22 ], [ 0, %6 ]
ret i32 %25
}