#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*/ assert(unsigned char *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
unsigned char *read_file(const char *filename, size_t *out_file_size) {
  FILE *fp = fopen(filename, "rb");
  unsigned char *data;
  size_t file_size;
  if (!fp) {
    perror("Error opening file");
    return NULL;
  }
  fseek(fp, 0L, SEEK_END);
  file_size = ftell(fp);
  fseek(fp, 0L, SEEK_SET);
  data = malloc(file_size);
  assert(data);
  size_t bytes_read = fread(data, sizeof(unsigned char), file_size, fp);
  if (bytes_read != file_size) {
    printf("Error reading data from file\n");
    return NULL;
  }
  if (out_file_size) {
    *out_file_size = file_size;
  }
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <read_file+0x48>
lea    0xe4d(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_file+0xf6>
mov    -0x20(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1258 <read_file+0xd8>
lea    0xdd0(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_file+0xf6>
cmpq   $0x0,-0x18(%rbp)
je     126e <read_file+0xee>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1227 <read_file+0xa7>
mov    %rax,%rbx
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <assert@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1209 <read_file+0x89>
test   %r14,%r14
je     1218 <read_file+0x98>
mov    %r12,(%r14)
jmp    1218 <read_file+0x98>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd5(%rip),%rdi
call   10a0 <perror@plt>
jmp    1215 <read_file+0x95>

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

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