#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 *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read_binary_file(const char *filename, unsigned char **out_ptr,
                     int *out_length) {
  FILE *input_file;
  unsigned char *data;
  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 = 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    %eax,%r13d
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 %42
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 i32 @ftell(i32* %21)
store i32 %22, i32* %10
%23 = load i32*, i32** %8
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32, i32* %10
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = load i32, i32* %10
%30 = load i32*, i32** %8
%31 = call i32 @fread(i8* %28, i32 %29, i32 1, i32* %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %17
%34 = 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 %42
35: ; preds = %17
%36 = load i8*, i8** %9
%37 = load i8**, i8*** %6
store i8* %36, i8** %37
%38 = load i32, i32* %10
%39 = load i32*, i32** %7
store i32 %38, i32* %39
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
store i32 1, i32* %4
br label %42
42: ; preds = %35, %33, %15
%43 = load i32, i32* %4
ret i32 %43
}

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 %21
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %4)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %4, i32 0, i32 %12)
%14 = tail call i8* @malloc(i32 %11)
%15 = tail call i32 @fread(i8* %14, i32 %11, i32 1, i32* %4)
%16 = icmp eq i32 %15, 1
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %21
19: ; preds = %8
store i8* %14, i8** %1
store i32 %11, i32* %2
%20 = tail call i32 @fclose(i32* %4)
br label %21
21: ; preds = %19, %17, %6
%22 = phi i32 [ 0, %17 ], [ 1, %19 ], [ 0, %6 ]
ret i32 %22
}