#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *readKernelBin(size_t *binary_size, char *filename) {
  int i = 0;
  int filename_size = 0;
  FILE *input = NULL;
  size_t size = 0;
  char *binary = NULL;
  printf("Reading in binary kernel from %s\n", filename);
  input = fopen(filename, "rb");
  if (input == NULL) {
    fprintf(stderr, "Unable to open %s for reading.\n", filename);
  }
  fseek(input, 0L, SEEK_END);
  size = ftell(input);
  rewind(input);
  binary = (char *)malloc(size);
  fread(binary, sizeof(char), size, input);
  fclose(input);
  if (binary_size != NULL) {
    *binary_size = size;
  }
  return (binary);
}


x86 assembly (clang, -O0):
<readKernelBin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120e <readKernelBin+0x7e>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1c(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x8(%rbp)
je     127c <readKernelBin+0xec>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readKernelBin>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe71(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d9 <readKernelBin+0x49>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     122d <readKernelBin+0x9d>
mov    %r15,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Reading in binary kernel from %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"Unable to open %s for reading.\0A\00"
@SEEK_END = global i32 0
define i8* @readKernelBin(i64* %0, i8* %1) {
%3 = alloca i64*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
store i64* %0, i64** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32* null, i32** %7
store i64 0, i64* %8
store i8* null, i8** %9
%10 = load i8*, i8** %4
%11 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %2
%21 = load i32*, i32** %7
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i64 0, i32 %22)
%24 = load i32*, i32** %7
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %8
%26 = load i32*, i32** %7
%27 = call i32 @rewind(i32* %26)
%28 = load i64, i64* %8
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = load i64, i64* %8
%33 = load i32*, i32** %7
%34 = call i32 @fread(i8* %31, i32 1, i64 %32, i32* %33)
%35 = load i32*, i32** %7
%36 = call i32 @fclose(i32* %35)
%37 = load i64*, i64** %3
%38 = icmp ne i64* %37, null
br i1 %38, label %39, label %42
39: ; preds = %20
%40 = load i64, i64* %8
%41 = load i64*, i64** %3
store i64 %40, i64* %41
br label %42
42: ; preds = %39, %20
%43 = load i8*, i8** %9
ret i8* %43
}

LLVM IR (-O2):
@.str = [34 x i8] c"Reading in binary kernel from %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"Unable to open %s for reading.\0A\00"
@SEEK_END = global i32 0
define i8* @readKernelBin(i64* writeonly %0, i8* %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %1)
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %9
9: ; preds = %6, %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %4, i64 0, i32 %10)
%12 = tail call i64 @ftell(i32* %4)
%13 = tail call i32 @rewind(i32* %4)
%14 = tail call i64 @malloc(i64 %12)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i32 1, i64 %12, i32* %4)
%17 = tail call i32 @fclose(i32* %4)
%18 = icmp eq i64* %0, null
br i1 %18, label %20, label %19
19: ; preds = %9
store i64 %12, i64* %0
br label %20
20: ; preds = %19, %9
ret i8* %15
}