#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 *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int read_file(char *filename, char **content) {
  char *buffer = 0;
  int length = 0;
  int bytes_read = 0;
  FILE *file_handler = fopen(filename, "rb");
  if (file_handler) {
    fseek(file_handler, 0, SEEK_END);
    length = ftell(file_handler);
    fseek(file_handler, 0, SEEK_SET);
    buffer = malloc(length + 1);
    if (buffer) {
      bytes_read = fread(buffer, 1, length, file_handler);
    }
    fclose(file_handler);
  }
  buffer[bytes_read] = '\0';
  (*content) = buffer;
  return bytes_read;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1214 <read_file+0xb4>
mov    -0x28(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120b <read_file+0xab>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xe8d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e5 <read_file+0x85>
mov    %rax,%r12
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
call   1050 <ftell@plt>
mov    %eax,%r13d
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r13),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11db <read_file+0x7b>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    11ea <read_file+0x8a>
xor    %r15d,%r15d
xor    %ebx,%ebx
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r14)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @read_file(i8* nocapture %0, i8** nocapture 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 %19, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i32 @ftell(i32* %3)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = add nsw i32 %8, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %5
%15 = tail call i32 @fread(i8* %12, i32 1, i32 %8, i32* %3)
br label %16
16: ; preds = %14, %5
%17 = phi i32 [ %15, %14 ], [ 0, %5 ]
%18 = tail call i32 @fclose(i32* %3)
br label %19
19: ; preds = %16, %2
%20 = phi i32 [ %17, %16 ], [ 0, %2 ]
%21 = phi i8* [ %12, %16 ], [ null, %2 ]
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
store i8* %21, i8** %1
ret i32 %20
}