#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*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ perror(char *);
int 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 0;
  }
  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");
    return 0;
  }
  *out_ptr = data;
  *out_length = file_length;
  fclose(input_file);
  return 1;
}


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    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bb <read_binary_file+0x4b>
lea    0xe58(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <read_binary_file+0xe5>
mov    -0x28(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x34(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1231 <read_binary_file+0xc1>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <read_binary_file+0xe5>
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   1040 <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    0xe78(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1216 <read_binary_file+0xa6>
mov    %rax,%rbx
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r13
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r13d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1227 <read_binary_file+0xb7>
mov    %rbp,(%r15)
mov    %r13d,(%r14)
mov    %rbx,%rdi
call   1040 <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    0xde5(%rip),%rdi
call   1090 <perror@plt>
xor    %r12d,%r12d
jmp    1204 <read_binary_file+0x94>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
jmp    1204 <read_binary_file+0x94>

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 %43
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 %36
34: ; preds = %17
%35 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %43
36: ; preds = %17
%37 = load i8*, i8** %9
%38 = load i8**, i8*** %6
store i8* %37, i8** %38
%39 = load i32, i32* %10
%40 = load i32*, i32** %7
store i32 %39, i32* %40
%41 = load i32*, i32** %8
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %4
br label %43
43: ; preds = %36, %34, %15
%44 = load i32, i32* %4
ret i32 %44
}

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 %22
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 %20, 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))
br label %22
20: ; preds = %8
store i8* %15, i8** %1
store i32 %12, i32* %2
%21 = tail call i32 @fclose(i32* %4)
br label %22
22: ; preds = %20, %18, %6
%23 = phi i32 [ 0, %18 ], [ 1, %20 ], [ 0, %6 ]
ret i32 %23
}