#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 */
char *calloc(int, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
size_t kernel_size(char const *);
int /*<<< orphan*/ printf(char *, char const *);
char *kernel_read(const char *filename) {
  size_t size;
  char *file;
  FILE *fp;
  fp = fopen(filename, "r");
  if (!fp) {
    printf("Error: Could not open CL source file %s", filename);
    return NULL;
  }
  size = kernel_size(filename);
  file = calloc(1, size + 1);
  if (!file) {
    printf("Error: Could not allocate space for %s", filename);
    return NULL;
  }
  fread(file, size, 1, fp);
  fclose(fp);
  return file;
}


x86 assembly (clang, -O0):
<kernel_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a8 <kernel_read+0x48>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121a <kernel_read+0xba>
mov    -0x10(%rbp),%rdi
call   1070 <kernel_size@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f3 <kernel_read+0x93>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121a <kernel_read+0xba>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kernel_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11bd <kernel_read+0x5d>
mov    %rax,%r14
mov    %r15,%rdi
call   1070 <kernel_size@plt>
mov    %rax,%r12
lea    0x1(%rax),%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
test   %rax,%rax
je     11c6 <kernel_read+0x66>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11d7 <kernel_read+0x77>
lea    0xe3e(%rip),%rdi
jmp    11cd <kernel_read+0x6d>
lea    0xe5d(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [40 x i8] c"Error: Could not open CL source file %s\00"
@.str.2 = [39 x i8] c"Error: Could not allocate space for %s\00"
define i8* @kernel_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i8* null, i8** %2
br label %33
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i64 @kernel_size(i8* %15)
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = add i64 %17, 1
%19 = call i8* @calloc(i32 1, i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %14
%23 = load i8*, i8** %3
%24 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i8* null, i8** %2
br label %33
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = load i64, i64* %4
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %26, i64 %27, i32 1, i32* %28)
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %5
store i8* %32, i8** %2
br label %33
33: ; preds = %25, %22, %11
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [40 x i8] c"Error: Could not open CL source file %s\00"
@.str.2 = [39 x i8] c"Error: Could not allocate space for %s\00"
define i8* @kernel_read(i8* %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 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
6: ; preds = %1
%7 = tail call i64 @kernel_size(i8* %0)
%8 = add i64 %7, 1
%9 = tail call i8* @calloc(i32 1, i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %16
13: ; preds = %6
%14 = tail call i32 @fread(i8* %9, i64 %7, i32 1, i32* %2)
%15 = tail call i32 @fclose(i32* %2)
br label %16
16: ; preds = %13, %11, %4
%17 = phi i8* [ %9, %13 ], [ null, %11 ], [ null, %4 ]
ret i8* %17
}