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

/* Variables and functions */
int /*<<< orphan*/ CL_PROGRAM_BUILD_LOG;
scalar_t__ CL_SUCCESS;
scalar_t__ clBuildProgram(int /*<<< orphan*/, int, int /*<<< orphan*/ *, char *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *clCreateKernel(int /*<<< orphan*/, char *, scalar_t__ *);
int /*<<< orphan*/ clCreateProgramWithSource(int /*<<< orphan*/, int,
                                             char const **,
                                             int /*<<< orphan*/ *,
                                             scalar_t__ *);
int /*<<< orphan*/ clError(char *, scalar_t__);
int /*<<< orphan*/ clGetProgramBuildInfo(int /*<<< orphan*/, int /*<<< orphan*/,
                                         int /*<<< orphan*/, int, char *,
                                         size_t *);
int /*<<< orphan*/ clReleaseProgram(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *load_program_source(char *);
int /*<<< orphan*/ stderr;
cl_kernel buildKernel(char *sourceFile, char *kernelName, char *options,
                      cl_context context, cl_device_id device, cl_int *error) {
  cl_int err;
  char *source = load_program_source(sourceFile);
  cl_program program =
      clCreateProgramWithSource(context, 1, (const char **)&source, NULL, &err);
  clError("Error creating program", err);
  err = clBuildProgram(program, 1, &device, options, NULL, NULL);
  if (CL_SUCCESS != err) {
    static char s[1048576];
    size_t len;
    fprintf(stderr, "Error building program\n");
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(s), s,
                          &len);
    fprintf(stderr, "Build log:\n%s\n", s);
    free(source);
    clReleaseProgram(program);
    *error = err;
    return NULL;
  }
  cl_kernel kernel = clCreateKernel(program, kernelName, &err);
  clError("Error creating kernel", err);
  free(source);
  clReleaseProgram(program);
  return kernel;
}


x86 assembly (clang, -O0):
<buildKernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <load_program_source@plt>
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
lea    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
call   1060 <clCreateProgramWithSource@plt>
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1050 <clError@plt>
mov    -0x44(%rbp),%edi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
cmp    -0x38(%rbp),%rax
je     129f <buildKernel+0x10f>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x44(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    $0x100000,%ecx
lea    0x2e41(%rip),%r8
lea    -0x50(%rbp),%r9
call   1090 <clGetProgramBuildInfo@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
lea    0x2e21(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x44(%rbp),%edi
call   1070 <clReleaseProgram@plt>
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12dc <buildKernel+0x14c>
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   10a0 <clCreateKernel@plt>
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xd80(%rip),%rdi
call   1050 <clError@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x44(%rbp),%edi
call   1070 <clReleaseProgram@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buildKernel>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r9,%r14
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %r8d,0xc(%rsp)
call   10b0 <load_program_source@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdx
mov    %rsp,%r8
mov    %ebp,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
call   1060 <clCreateProgramWithSource@plt>
mov    %eax,%ebp
mov    (%rsp),%rsi
lea    0xe27(%rip),%rdi
call   1050 <clError@plt>
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    $0x1,%esi
mov    %rbx,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %rax,(%rsp)
mov    0x2dcd(%rip),%rcx
cmp    %rax,(%rcx)
jne    123b <buildKernel+0xab>
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %r15,%rsi
call   10a0 <clCreateKernel@plt>
mov    %rax,%rbx
mov    (%rsp),%rsi
lea    0xe1b(%rip),%rdi
call   1050 <clError@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%edi
call   1070 <clReleaseProgram@plt>
jmp    12ab <buildKernel+0x11b>
mov    0x2d96(%rip),%r12
mov    (%r12),%edi
lea    0xdca(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0xc(%rsp),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    0x2e26(%rip),%r15
lea    0x18(%rsp),%r9
mov    %ebp,%edi
mov    $0x100000,%ecx
mov    %r15,%r8
call   1090 <clGetProgramBuildInfo@plt>
mov    (%r12),%edi
lea    0xda6(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%edi
call   1070 <clReleaseProgram@plt>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Error creating program\00"
@CL_SUCCESS = global i64 0
@buildKernel.s = internal global [1048576 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error building program\0A\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.2 = [15 x i8] c"Build log:\0A%s\0A\00"
@.str.3 = [22 x i8] c"Error creating kernel\00"
define i32* @buildKernel(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i64* %5) {
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i64
%18 = alloca i32*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i64* %5, i64** %13
%19 = load i8*, i8** %8
%20 = call i8* @load_program_source(i8* %19)
store i8* %20, i8** %15
%21 = load i32, i32* %11
%22 = call i32 @clCreateProgramWithSource(i32 %21, i32 1, i8** %15, i32* null, i64* %14)
store i32 %22, i32* %16
%23 = load i64, i64* %14
%24 = call i32 @clError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %23)
%25 = load i32, i32* %16
%26 = load i8*, i8** %10
%27 = call i64 @clBuildProgram(i32 %25, i32 1, i32* %12, i8* %26, i32* null, i32* null)
store i64 %27, i64* %14
%28 = load i64, i64* @CL_SUCCESS
%29 = load i64, i64* %14
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %46
31: ; preds = %6
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %16
%35 = load i32, i32* %12
%36 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%37 = call i32 @clGetProgramBuildInfo(i32 %34, i32 %35, i32 %36, i32 1048576, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0), i64* %17)
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0))
%40 = load i8*, i8** %15
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %16
%43 = call i32 @clReleaseProgram(i32 %42)
%44 = load i64, i64* %14
%45 = load i64*, i64** %13
store i64 %44, i64* %45
store i32* null, i32** %7
br label %57
46: ; preds = %6
%47 = load i32, i32* %16
%48 = load i8*, i8** %9
%49 = call i32* @clCreateKernel(i32 %47, i8* %48, i64* %14)
store i32* %49, i32** %18
%50 = load i64, i64* %14
%51 = call i32 @clError(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %50)
%52 = load i8*, i8** %15
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %16
%55 = call i32 @clReleaseProgram(i32 %54)
%56 = load i32*, i32** %18
store i32* %56, i32** %7
br label %57
57: ; preds = %46, %31
%58 = load i32*, i32** %7
ret i32* %58
}

LLVM IR (-O2):
@.str = [23 x i8] c"Error creating program\00"
@CL_SUCCESS = global i64 0
@buildKernel.s = internal global [1048576 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error building program\0A\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.2 = [15 x i8] c"Build log:\0A%s\0A\00"
@.str.3 = [22 x i8] c"Error creating kernel\00"
define i32* @buildKernel(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i64* nocapture writeonly %5) {
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i32 %4, i32* %7
%11 = bitcast i64* %8 to i8*
%12 = bitcast i8** %9 to i8*
%13 = tail call i8* @load_program_source(i8* %0)
store i8* %13, i8** %9
%14 = call i32 @clCreateProgramWithSource(i32 %3, i32 1, i8** %9, i32* null, i64* %8)
%15 = load i64, i64* %8
%16 = call i32 @clError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %15)
%17 = call i64 @clBuildProgram(i32 %14, i32 1, i32* %7, i8* %2, i32* null, i32* null)
store i64 %17, i64* %8
%18 = load i64, i64* @CL_SUCCESS
%19 = icmp eq i64 %18, %17
br i1 %19, label %33, label %20
20: ; preds = %6
%21 = bitcast i64* %10 to i8*
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %7
%25 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%26 = call i32 @clGetProgramBuildInfo(i32 %14, i32 %24, i32 %25, i32 1048576, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0), i64* %10)
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0))
%29 = load i8*, i8** %9
%30 = call i32 @free(i8* %29)
%31 = call i32 @clReleaseProgram(i32 %14)
%32 = load i64, i64* %8
store i64 %32, i64* %5
br label %40
33: ; preds = %6
%34 = call i32* @clCreateKernel(i32 %14, i8* %1, i64* %8)
%35 = load i64, i64* %8
%36 = call i32 @clError(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %35)
%37 = load i8*, i8** %9
%38 = call i32 @free(i8* %37)
%39 = call i32 @clReleaseProgram(i32 %14)
br label %40
40: ; preds = %33, %20
%41 = phi i32* [ null, %20 ], [ %34, %33 ]
ret i32* %41
}