#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ stderr;
char *load_file(const char *filename) {
  FILE *f = fopen(filename, "r");
  long f_size;
  char *f_buffer;
  size_t n_read_bytes;
  if (f == NULL) {
    fprintf(stderr, "Could not open file: %s\n", filename);
    return NULL;
  }
  fseek(f, 0, SEEK_END);
  f_size = ftell(f);
  fseek(f, 0, SEEK_SET);
  f_buffer = malloc(f_size + 1);
  if (f_buffer == NULL) {
    fprintf(stderr, "Could not allocate buffer for file: %s (%li bytes)\n",
            filename, f_size);
    fclose(f);
    return NULL;
  }
  n_read_bytes = fread(f_buffer, 1, f_size, f);
  fclose(f);
  return f_buffer;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <load_file+0x53>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1274 <load_file+0x104>
mov    -0x18(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1249 <load_file+0xd9>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1274 <load_file+0x104>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11e9 <load_file+0x79>
mov    %rax,%rbx
mov    0x2e52(%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    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1208 <load_file+0x98>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
jmp    1228 <load_file+0xb8>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1230 <load_file+0xc0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [52 x i8] c"Could not allocate buffer for file: %s (%li bytes)\0A\00"
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* null, i8** %2
br label %45
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %4
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i32*, i32** %4
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i64, i64* %5
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %37
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %3
%33 = load i64, i64* %5
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* %32, i64 %33)
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
store i8* null, i8** %2
br label %45
37: ; preds = %16
%38 = load i8*, i8** %6
%39 = load i64, i64* %5
%40 = load i32*, i32** %4
%41 = call i64 @fread(i8* %38, i32 1, i64 %39, i32* %40)
store i64 %41, i64* %7
%42 = load i32*, i32** %4
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %6
store i8* %44, i8** %2
br label %45
45: ; preds = %37, %30, %12
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [52 x i8] c"Could not allocate buffer for file: %s (%li bytes)\0A\00"
define noalias i8* @load_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i32 0, i32 %11)
%13 = add nsw i64 %10, 1
%14 = tail call i8* @malloc(i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %10)
%19 = tail call i32 @fclose(i32* %2)
br label %23
20: ; preds = %7
%21 = tail call i64 @fread(i8* %14, i32 1, i64 %10, i32* %2)
%22 = tail call i32 @fclose(i32* %2)
br label %23
23: ; preds = %20, %16, %4
%24 = phi i8* [ null, %4 ], [ null, %16 ], [ %14, %20 ]
ret i8* %24
}