#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *getKernelSource(char *filename, int *length) {
  FILE *file = fopen(filename, "r");
  if (!file) {
    fprintf(stderr, "Error, Could not open kernel source file\n");
    exit(EXIT_FAILURE);
  }
  fseek(file, 0, SEEK_END);
  int len = ftell(file) + 1;
  *length = len;
  rewind(file);
  char *source = (char *)calloc(sizeof(char), len);
  if (!source) {
    fprintf(stderr, "Error, Could not allocate memory for source string\n");
    exit(EXIT_FAILURE);
  }
  fread(source, sizeof(char), len, file);
  fclose(file);
  return source;
}


x86 assembly (clang, -O0):
<getKernelSource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e2 <getKernelSource+0x52>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x1c(%rbp),%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1256 <getKernelSource+0xc6>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getKernelSource>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     120c <getKernelSource+0x7c>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,(%r14)
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    $0x1,%edi
mov    %ebp,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     121e <getKernelSource+0x8e>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
jmp    122e <getKernelSource+0x9e>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Error, Could not open kernel source file\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.2 = [52 x i8] c"Error, Could not allocate memory for source string\0A\00"
define i8* @getKernelSource(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %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 %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %5
%22 = call i32 @ftell(i32* %21)
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = load i32*, i32** %4
store i32 %24, i32* %25
%26 = load i32*, i32** %5
%27 = call i32 @rewind(i32* %26)
%28 = load i32, i32* %6
%29 = call i64 @calloc(i32 1, i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %17
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %17
%39 = load i8*, i8** %7
%40 = load i32, i32* %6
%41 = load i32*, i32** %5
%42 = call i32 @fread(i8* %39, i32 1, i32 %40, i32* %41)
%43 = load i32*, i32** %5
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Error, Could not open kernel source file\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.2 = [52 x i8] c"Error, Could not allocate memory for source string\0A\00"
define i8* @getKernelSource(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i32 @ftell(i32* %3)
%14 = add nsw i32 %13, 1
store i32 %14, i32* %1
%15 = tail call i32 @rewind(i32* %3)
%16 = tail call i64 @calloc(i32 1, i32 %14)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %24
19: ; preds = %10
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = tail call i32 @fread(i8* %17, i32 1, i32 %14, i32* %3)
%26 = tail call i32 @fclose(i32* %3)
ret i8* %17
}