#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*/ fatal(char *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
char *load_kernel_source(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    fatal("Error opening kernel source file\n");
  }
  if (fseek(file, 0, SEEK_END)) {
    fatal("Error reading kernel source file\n");
  }
  size_t size = ftell(file);
  char *source = (char *)malloc(size + 1);
  fseek(file, 0, SEEK_SET);
  if (fread(source, 1, size, file) != size) {
    fatal("Error reading kernel source file\n");
  }
  source[size] = '\0';
  return source;
}


x86 assembly (clang, -O0):
<load_kernel_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1197 <load_kernel_source+0x37>
lea    0xe70(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11c1 <load_kernel_source+0x61>
lea    0xe68(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
je     121f <load_kernel_source+0xbf>
lea    0xe0a(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_kernel_source>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1185 <load_kernel_source+0x25>
lea    0xe82(%rip),%rdi
call   1080 <fatal@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     11a9 <load_kernel_source+0x49>
lea    0xe80(%rip),%rdi
call   1080 <fatal@plt>
mov    %r14,%rdi
call   1040 <ftell@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    %rbx,%rax
je     11f7 <load_kernel_source+0x97>
lea    0xe32(%rip),%rdi
call   1080 <fatal@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"Error opening kernel source file\0A\00"
@SEEK_END = global i32 0
@.str.2 = [34 x i8] c"Error reading kernel source file\0A\00"
@SEEK_SET = global i32 0
define i8* @load_kernel_source(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 %4, label %6
4: ; preds = %1
%5 = tail call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %2, i32 0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = tail call i64 @ftell(i32* %2)
%14 = add i64 %13, 1
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @fseek(i32* %2, i32 0, i32 %17)
%19 = tail call i64 @fread(i8* %16, i32 1, i64 %13, i32* %2)
%20 = icmp eq i64 %19, %13
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = tail call i32 @fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = getelementptr inbounds i8, i8* %16, i64 %13
store i8 0, i8* %24
ret i8* %16
}