#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *getenv(char *);
char *malloc(size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncat(char *, char const *, int);
char *oclGetProgramSrcFromFile(const char *fileName, size_t *sizeFile) {
  FILE *file;
  char Path[512];
  Path[0] = '\0';
  char *p = getenv("OCL_KERNELS_PATH");
  if (p != NULL) {
    strncat(&Path[0], p, 512 - 1);
  }
  strncat(&Path[0], fileName, 512 - 1);
  if ((file = fopen(Path, "r")) == NULL) {
    fprintf(stderr, "Can not open kernel source file:%s\n", &Path[0]);
    exit(1);
  }
  size_t size;
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  *sizeFile = size;
  fseek(file, 0, SEEK_SET);
  char *str = malloc(size + 1);
  fread(str, size, 1, file);
  str[size] = '\0';
  fclose(file);
  return str;
}


x86 assembly (clang, -O0):
<oclGetProgramSrcFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x220(%rbp)
lea    0xe3f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     11f3 <oclGetProgramSrcFromFile+0x53>
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rsi
mov    $0x1ff,%edx
call   1060 <strncat@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x1ff,%edx
call   1060 <strncat@plt>
lea    -0x220(%rbp),%rdi
lea    0xdfb(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    124f <oclGetProgramSrcFromFile+0xaf>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    -0x220(%rbp),%rdx
lea    0xdd3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x230(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
mov    -0x230(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x238(%rbp),%rax
mov    -0x230(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x238(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<oclGetProgramSrcFromFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movb   $0x0,(%rsp)
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d9 <oclGetProgramSrcFromFile+0x39>
mov    %rsp,%rdi
mov    %rax,%rsi
mov    $0x1ff,%edx
call   1060 <strncat@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1ff,%edx
call   1060 <strncat@plt>
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1276 <oclGetProgramSrcFromFile+0xd6>
mov    %rax,%r15
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    %rax,(%r14)
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x1(%r12),%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    %rsp,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"OCL_KERNELS_PATH\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"Can not open kernel source file:%s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @oclGetProgramSrcFromFile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i8 0, i8* %10
%11 = call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = load i8*, i8** %7
%17 = call i32 @strncat(i8* %15, i8* %16, i32 511)
br label %18
18: ; preds = %14, %2
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%20 = load i8*, i8** %3
%21 = call i32 @strncat(i8* %19, i8* %20, i32 511)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %5
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = load i32*, i32** %5
%32 = load i32, i32* @SEEK_END
%33 = call i32 @fseek(i32* %31, i32 0, i32 %32)
%34 = load i32*, i32** %5
%35 = call i64 @ftell(i32* %34)
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = load i64*, i64** %4
store i64 %36, i64* %37
%38 = load i32*, i32** %5
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %38, i32 0, i32 %39)
%41 = load i64, i64* %8
%42 = add i64 %41, 1
%43 = call i8* @malloc(i64 %42)
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = load i64, i64* %8
%46 = load i32*, i32** %5
%47 = call i32 @fread(i8* %44, i64 %45, i32 1, i32* %46)
%48 = load i8*, i8** %9
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
%51 = load i32*, i32** %5
%52 = call i32 @fclose(i32* %51)
%53 = load i8*, i8** %9
ret i8* %53
}

LLVM IR (-O2):
@.str = [17 x i8] c"OCL_KERNELS_PATH\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"Can not open kernel source file:%s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @oclGetProgramSrcFromFile(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
store i8 0, i8* %4
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = call i32 @strncat(i8* %4, i8* %5, i32 511)
br label %9
9: ; preds = %7, %2
%10 = call i32 @strncat(i8* %4, i8* %0, i32 511)
%11 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %11, i32 0, i32 %18)
%20 = call i64 @ftell(i32* %11)
store i64 %20, i64* %1
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %11, i32 0, i32 %21)
%23 = add i64 %20, 1
%24 = call i8* @malloc(i64 %23)
%25 = call i32 @fread(i8* %24, i64 %20, i32 1, i32* %11)
%26 = getelementptr inbounds i8, i8* %24, i64 %20
store i8 0, i8* %26
%27 = call i32 @fclose(i32* %11)
ret i8* %24
}