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

/* Variables and functions */
int /*<<< orphan*/ CL_PROGRAM_BUILD_LOG;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char *calloc(int, size_t);
scalar_t__ clBuildProgram(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ *clCreateProgramWithSource(int /*<<< orphan*/, int,
                                              char const **,
                                              int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *);
int /*<<< orphan*/ clGetProgramBuildInfo(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/, int /*<<< orphan*/,
                                         size_t, char *, size_t *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ stderr;
cl_program clLoadProgram(cl_context context, cl_device_id device,
                         const char *filename) {
  FILE *fp;
  size_t length;
  char *data;
  const char *source;
  size_t ret;
  fp = fopen(filename, "rb");
  if (fp == NULL)
    fprintf(stderr, "Error opening file\n");
  fseek(fp, 0, SEEK_END);
  length = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  data = (char *)malloc((length + 1) * sizeof(char));
  ret = fread(data, sizeof(char), length, fp);
  if (ret != length)
    fprintf(stderr, "Error reading file\n");
  data[length] = 0;
  source = &data[0];
  cl_program program =
      clCreateProgramWithSource(context, 1, &source, NULL, NULL);
  if (program == NULL) {
    fprintf(stderr, "Error creating program\n");
    return NULL;
  }
  if (clBuildProgram(program, 0, NULL, "-I . -cl-std=CL2.0", NULL, NULL) !=
      CL_SUCCESS) {
    cl_int err;
    size_t len;
    char *buffer;
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
    buffer = calloc(sizeof(char), len);
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, len, buffer,
                          NULL);
    fprintf(stderr, "Error building program %d: %s\n", err, buffer);
    return NULL;
  }
  free(data);
  fclose(fp);
  return program;
}


x86 assembly (clang, -O0):
<clLoadProgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1208 <clLoadProgram+0x48>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1291 <clLoadProgram+0xd1>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
lea    -0x38(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1050 <clCreateProgramWithSource@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12f1 <clLoadProgram+0x131>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13b8 <clLoadProgram+0x1f8>
mov    -0x48(%rbp),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%r9d
lea    0xd40(%rip),%rcx
mov    %r9,%rdx
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    0x2cab(%rip),%rcx
cmp    (%rcx),%rax
je     139e <clLoadProgram+0x1de>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2cb4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x58(%rbp),%r9
mov    %r8,%rcx
call   10a0 <clGetProgramBuildInfo@plt>
mov    -0x58(%rbp),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2c81(%rip),%rax
mov    (%rax),%edx
mov    -0x58(%rbp),%rcx
mov    -0x60(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   10a0 <clGetProgramBuildInfo@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
mov    -0x4c(%rbp),%edx
mov    -0x60(%rbp),%rcx
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13b8 <clLoadProgram+0x1f8>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clLoadProgram>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%r12d
lea    0xe27(%rip),%rsi
mov    %rdx,%rdi
call   10e0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1200 <clLoadProgram+0x40>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r15,%rdi
call   10b0 <ftell@plt>
mov    %rax,%rbp
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
lea    0x1(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1060 <fread@plt>
cmp    %rbp,%rax
je     126c <clLoadProgram+0xac>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
movb   $0x0,(%rbx,%rbp,1)
mov    %rbx,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %r12d,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <clCreateProgramWithSource@plt>
test   %rax,%rax
je     12cb <clLoadProgram+0x10b>
mov    %rax,%rbp
lea    0xda8(%rip),%rcx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
jne    12e6 <clLoadProgram+0x126>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
jmp    1348 <clLoadProgram+0x188>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1348 <clLoadProgram+0x188>
mov    0x2cf3(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%r9
mov    %rbp,%rdi
mov    %r14d,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <clGetProgramBuildInfo@plt>
mov    (%rsp),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    (%r15),%edx
mov    (%rsp),%rcx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %rax,%r8
xor    %r9d,%r9d
call   10a0 <clGetProgramBuildInfo@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
lea    0xd1a(%rip),%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Error opening file\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [20 x i8] c"Error reading file\0A\00"
@.str.3 = [24 x i8] c"Error creating program\0A\00"
@.str.4 = [19 x i8] c"-I . -cl-std=CL2.0\00"
@CL_SUCCESS = global i64 0
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.5 = [31 x i8] c"Error building program %d: %s\0A\00"
define i32* @clLoadProgram(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i64
%16 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%17 = load i8*, i8** %7
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %3
%25 = load i32*, i32** %8
%26 = load i32, i32* @SEEK_END
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i32*, i32** %8
%29 = call i64 @ftell(i32* %28)
store i64 %29, i64* %9
%30 = load i32*, i32** %8
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i64, i64* %9
%34 = add i64 %33, 1
%35 = mul i64 %34, 1
%36 = call i64 @malloc(i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %10
%38 = load i8*, i8** %10
%39 = load i64, i64* %9
%40 = load i32*, i32** %8
%41 = call i64 @fread(i8* %38, i32 1, i64 %39, i32* %40)
store i64 %41, i64* %12
%42 = load i64, i64* %12
%43 = load i64, i64* %9
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %24
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %45, %24
%49 = load i8*, i8** %10
%50 = load i64, i64* %9
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %10
%53 = getelementptr inbounds i8, i8* %52, i64 0
store i8* %53, i8** %11
%54 = load i32, i32* %5
%55 = call i32* @clCreateProgramWithSource(i32 %54, i32 1, i8** %11, i32* null, i32* null)
store i32* %55, i32** %13
%56 = load i32*, i32** %13
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32* null, i32** %4
br label %89
61: ; preds = %48
%62 = load i32*, i32** %13
%63 = call i64 @clBuildProgram(i32* %62, i32 0, i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32* null, i32* null)
%64 = load i64, i64* @CL_SUCCESS
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %83
66: ; preds = %61
%67 = load i32*, i32** %13
%68 = load i32, i32* %6
%69 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%70 = call i32 @clGetProgramBuildInfo(i32* %67, i32 %68, i32 %69, i64 0, i8* null, i64* %15)
%71 = load i64, i64* %15
%72 = call i8* @calloc(i32 1, i64 %71)
store i8* %72, i8** %16
%73 = load i32*, i32** %13
%74 = load i32, i32* %6
%75 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%76 = load i64, i64* %15
%77 = load i8*, i8** %16
%78 = call i32 @clGetProgramBuildInfo(i32* %73, i32 %74, i32 %75, i64 %76, i8* %77, i64* null)
%79 = load i32, i32* @stderr
%80 = load i32, i32* %14
%81 = load i8*, i8** %16
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %80, i8* %81)
store i32* null, i32** %4
br label %89
83: ; preds = %61
%84 = load i8*, i8** %10
%85 = call i32 @free(i8* %84)
%86 = load i32*, i32** %8
%87 = call i32 @fclose(i32* %86)
%88 = load i32*, i32** %13
store i32* %88, i32** %4
br label %89
89: ; preds = %83, %66, %58
%90 = load i32*, i32** %4
ret i32* %90
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Error opening file\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [20 x i8] c"Error reading file\0A\00"
@.str.3 = [24 x i8] c"Error creating program\0A\00"
@.str.4 = [19 x i8] c"-I . -cl-std=CL2.0\00"
@CL_SUCCESS = global i64 0
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.5 = [31 x i8] c"Error building program %d: %s\0A\00"
define i32* @clLoadProgram(i32 %0, i32 %1, i8* nocapture %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i8** %4 to i8*
%7 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %9, %3
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %7, i32 0, i32 %13)
%15 = tail call i64 @ftell(i32* %7)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %7, i32 0, i32 %16)
%18 = add i64 %15, 1
%19 = tail call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i64 @fread(i8* %20, i32 1, i64 %15, i32* %7)
%22 = icmp eq i64 %21, %15
br i1 %22, label %26, label %23
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %23, %12
%27 = getelementptr inbounds i8, i8* %20, i64 %15
store i8 0, i8* %27
store i8* %20, i8** %4
%28 = call i32* @clCreateProgramWithSource(i32 %0, i32 1, i8** %4, i32* null, i32* null)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %51
33: ; preds = %26
%34 = call i64 @clBuildProgram(i32* %28, i32 0, i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32* null, i32* null)
%35 = load i64, i64* @CL_SUCCESS
%36 = icmp eq i64 %34, %35
br i1 %36, label %48, label %37
37: ; preds = %33
%38 = bitcast i64* %5 to i8*
%39 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%40 = call i32 @clGetProgramBuildInfo(i32* %28, i32 %1, i32 %39, i64 0, i8* null, i64* %5)
%41 = load i64, i64* %5
%42 = call i8* @calloc(i32 1, i64 %41)
%43 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%44 = load i64, i64* %5
%45 = call i32 @clGetProgramBuildInfo(i32* %28, i32 %1, i32 %43, i64 %44, i8* %42, i64* null)
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 undef, i8* %42)
br label %51
48: ; preds = %33
%49 = call i32 @free(i8* %20)
%50 = call i32 @fclose(i32* %7)
br label %51
51: ; preds = %48, %37, %30
%52 = phi i32* [ null, %30 ], [ null, %37 ], [ %28, %48 ]
ret i32* %52
}