#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 scalar_t__ cl_int;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *readSource(char *kernelPath) {
  cl_int status;
  FILE *fp;
  char *source;
  long int size;
  printf("Program file is: %s\n", kernelPath);
  fp = fopen(kernelPath, "rb");
  if (!fp) {
    printf("Could not open kernel file\n");
    exit(-1);
  }
  status = fseek(fp, 0, SEEK_END);
  if (status != 0) {
    printf("Error seeking to end of file\n");
    exit(-1);
  }
  size = ftell(fp);
  if (size < 0) {
    printf("Error getting file position\n");
    exit(-1);
  }
  rewind(fp);
  source = (char *)malloc(size + 1);
  int i;
  for (i = 0; i < size + 1; i++) {
    source[i] = '\0';
  }
  if (source == NULL) {
    printf("Error allocating space for the kernel source\n");
    exit(-1);
  }
  fread(source, 1, size, fp);
  source[size] = '\0';
  return source;
}


x86 assembly (clang, -O0):
<readSource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <readSource+0x55>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1210 <readSource+0x90>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1240 <readSource+0xc0>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jge    1290 <readSource+0x110>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1261 <readSource+0xe1>
cmpq   $0x0,-0x20(%rbp)
jne    12b3 <readSource+0x133>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readSource>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe4e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe52(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     124b <readSource+0xab>
mov    %rax,%r14
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    1254 <readSource+0xb4>
mov    %r14,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     125d <readSource+0xbd>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%r12
mov    %r12,%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1070 <memset@plt>
test   %rbx,%rbx
je     1266 <readSource+0xc6>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc6(%rip),%rdi
jmp    126d <readSource+0xcd>
lea    0xe21(%rip),%rdi
jmp    126d <readSource+0xcd>
lea    0xdfc(%rip),%rdi
jmp    126d <readSource+0xcd>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Program file is: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [28 x i8] c"Could not open kernel file\0A\00"
@SEEK_END = global i32 0
@.str.3 = [30 x i8] c"Error seeking to end of file\0A\00"
@.str.4 = [29 x i8] c"Error getting file position\0A\00"
@.str.5 = [46 x i8] c"Error allocating space for the kernel source\0A\00"
define i8* @readSource(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i32 0, i32 %19)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %17
%27 = load i32*, i32** %4
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %26
%35 = load i32*, i32** %4
%36 = call i32 @rewind(i32* %35)
%37 = load i64, i64* %6
%38 = add nsw i64 %37, 1
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %5
store i32 0, i32* %7
br label %41
41: ; preds = %52, %34
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = load i64, i64* %6
%45 = add nsw i64 %44, 1
%46 = icmp slt i64 %43, %45
br i1 %46, label %47, label %55
47: ; preds = %41
%48 = load i8*, i8** %5
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %47
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %41
55: ; preds = %41
%56 = load i8*, i8** %5
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %55
%62 = load i8*, i8** %5
%63 = load i64, i64* %6
%64 = load i32*, i32** %4
%65 = call i32 @fread(i8* %62, i32 1, i64 %63, i32* %64)
%66 = load i8*, i8** %5
%67 = load i64, i64* %6
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8 0, i8* %68
%69 = load i8*, i8** %5
ret i8* %69
}

LLVM IR (-O2):
@.str = [21 x i8] c"Program file is: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@str = [27 x i8] c"Could not open kernel file\00"
@str.6 = [45 x i8] c"Error allocating space for the kernel source\00"
@str.7 = [28 x i8] c"Error getting file position\00"
@str.8 = [29 x i8] c"Error seeking to end of file\00"
define i8* @readSource(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @fseek(i32* %3, i32 0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %8
%16 = tail call i64 @ftell(i32* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = tail call i32 @rewind(i32* %3)
%23 = add nuw nsw i64 %16, 1
%24 = tail call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.6, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %21
%31 = tail call i32 @fread(i8* %25, i32 1, i64 %16, i32* %3)
%32 = getelementptr inbounds i8, i8* %25, i64 %16
store i8 0, i8* %32
ret i8* %25
}