#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;
char *bfm_malloc(long);
int /*<<< orphan*/ fatal_error(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *load_file(const char *path) {
  char *buf = NULL;
  FILE *file = fopen(path, "r");
  if (file) {
    if (fseek(file, 0L, SEEK_END) == 0) {
      long len = ftell(file);
      if (len == -1)
        return NULL;
      buf = bfm_malloc(len + 1);
      if (fseek(file, 0L, SEEK_SET) != 0)
        return NULL;
      size_t new_len = fread(buf, 1, len, file);
      if (ferror(file) != 0) {
        fatal_error(-1, "Input file could not be read.");
      } else {
        buf[new_len++] = '\0';
      }
    }
    fclose(file);
  }
  return buf;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     129e <load_file+0x11e>
mov    -0x20(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
jne    1295 <load_file+0x115>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11f8 <load_file+0x78>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <load_file+0x126>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <bfm_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     1236 <load_file+0xb6>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <load_file+0x126>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1279 <load_file+0xf9>
mov    $0xffffffff,%edi
lea    0xd93(%rip),%rsi
call   1080 <fatal_error@plt>
jmp    1290 <load_file+0x110>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x0,(%rax,%rcx,1)
jmp    1295 <load_file+0x115>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%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
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1235 <load_file+0xb5>
mov    %rax,%r12
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
je     11c8 <load_file+0x48>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rbx
jmp    1237 <load_file+0xb7>
mov    %r12,%rdi
call   1070 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1235 <load_file+0xb5>
mov    %rax,%r15
lea    0x1(%rax),%rdi
call   1060 <bfm_malloc@plt>
mov    %rax,%r14
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
test   %rax,%rax
jne    1237 <load_file+0xb7>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1246 <load_file+0xc6>
lea    0xdd9(%rip),%rsi
mov    $0xffffffff,%edi
call   1080 <fatal_error@plt>
jmp    11bb <load_file+0x3b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movb   $0x0,(%r14,%rbx,1)
jmp    11bb <load_file+0x3b>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [30 x i8] c"Input file could not be read.\00"
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%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** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %51
12: ; preds = %1
%13 = load i32*, i32** %5
%14 = load i32, i32* @SEEK_END
%15 = call i64 @fseek(i32* %13, i64 0, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %48
17: ; preds = %12
%18 = load i32*, i32** %5
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i8* null, i8** %2
br label %53
23: ; preds = %17
%24 = load i64, i64* %6
%25 = add nsw i64 %24, 1
%26 = call i8* @bfm_malloc(i64 %25)
store i8* %26, i8** %4
%27 = load i32*, i32** %5
%28 = load i32, i32* @SEEK_SET
%29 = call i64 @fseek(i32* %27, i64 0, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %23
store i8* null, i8** %2
br label %53
32: ; preds = %23
%33 = load i8*, i8** %4
%34 = load i64, i64* %6
%35 = load i32*, i32** %5
%36 = call i64 @fread(i8* %33, i32 1, i64 %34, i32* %35)
store i64 %36, i64* %7
%37 = load i32*, i32** %5
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @fatal_error(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %47
42: ; preds = %32
%43 = load i8*, i8** %4
%44 = load i64, i64* %7
%45 = add i64 %44, 1
store i64 %45, i64* %7
%46 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %46
br label %47
47: ; preds = %42, %40
br label %48
48: ; preds = %47, %12
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
br label %51
51: ; preds = %48, %1
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %53
53: ; preds = %51, %31, %22
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [30 x i8] c"Input file could not be read.\00"
define i8* @load_file(i8* nocapture %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 %28, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %25
8: ; preds = %4
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, -1
br i1 %10, label %28, label %11
11: ; preds = %8
%12 = add nuw nsw i64 %9, 1
%13 = tail call i8* @bfm_malloc(i64 %12)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @fseek(i32* %2, i64 0, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %11
%18 = tail call i64 @fread(i8* %13, i32 1, i64 %9, i32* %2)
%19 = tail call i64 @ferror(i32* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @fatal_error(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %25
23: ; preds = %17
%24 = getelementptr inbounds i8, i8* %13, i64 %18
store i8 0, i8* %24
br label %25
25: ; preds = %21, %23, %4
%26 = phi i8* [ null, %4 ], [ %13, %23 ], [ %13, %21 ]
%27 = tail call i32 @fclose(i32* %2)
br label %28
28: ; preds = %8, %11, %1, %25
%29 = phi i8* [ %26, %25 ], [ null, %1 ], [ null, %11 ], [ null, %8 ]
ret i8* %29
}