#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*/ exit(int);
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 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);
    exit(1);
  }
  cl_kernel kernel = clCreateKernel(program, kernelName, &err);
  clError("Error creating kernel", err);
  free(source);
  return kernel;
}


x86 assembly (clang, -O0):
<buildKernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <load_program_source@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
call   1060 <clCreateProgramWithSource@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1050 <clError@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
je     127c <buildKernel+0xec>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    $0x100000,%ecx
lea    0x2e45(%rip),%r8
lea    -0x40(%rbp),%r9
call   1080 <clGetProgramBuildInfo@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
lea    0x2e25(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <clCreateKernel@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xda4(%rip),%rdi
call   1050 <clError@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buildKernel>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %r8d,0x4(%rsp)
call   10a0 <load_program_source@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
call   1060 <clCreateProgramWithSource@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rsi
lea    0xe2b(%rip),%rdi
call   1050 <clError@plt>
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    $0x1,%esi
mov    %rbx,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %rax,0x8(%rsp)
mov    0x2dd0(%rip),%rcx
cmp    %rax,(%rcx)
jne    123c <buildKernel+0xac>
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
call   1090 <clCreateKernel@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rsi
lea    0xe1c(%rip),%rdi
call   1050 <clError@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d95(%rip),%r14
mov    (%r14),%edi
lea    0xdca(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x4(%rsp),%esi
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
lea    0x2e28(%rip),%rbx
lea    0x18(%rsp),%r9
mov    %ebp,%edi
mov    $0x100000,%ecx
mov    %rbx,%r8
call   1080 <clGetProgramBuildInfo@plt>
mov    (%r14),%edi
lea    0xda9(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i8*, i8** %6
%17 = call i8* @load_program_source(i8* %16)
store i8* %17, i8** %12
%18 = load i32, i32* %9
%19 = call i32 @clCreateProgramWithSource(i32 %18, i32 1, i8** %12, i32* null, i64* %11)
store i32 %19, i32* %13
%20 = load i64, i64* %11
%21 = call i32 @clError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %20)
%22 = load i32, i32* %13
%23 = load i8*, i8** %8
%24 = call i64 @clBuildProgram(i32 %22, i32 1, i32* %10, i8* %23, i32* null, i32* null)
store i64 %24, i64* %11
%25 = load i64, i64* @CL_SUCCESS
%26 = load i64, i64* %11
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %5
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %13
%32 = load i32, i32* %10
%33 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%34 = call i32 @clGetProgramBuildInfo(i32 %31, i32 %32, i32 %33, i32 1048576, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0), i64* %14)
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, 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))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %5
%39 = load i32, i32* %13
%40 = load i8*, i8** %7
%41 = call i32 @clCreateKernel(i32 %39, i8* %40, i64* %11)
store i32 %41, i32* %15
%42 = load i64, i64* %11
%43 = call i32 @clError(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %42)
%44 = load i8*, i8** %12
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* %15
ret i32 %46
}

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) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 %4, i32* %6
%10 = bitcast i64* %7 to i8*
%11 = bitcast i8** %8 to i8*
%12 = tail call i8* @load_program_source(i8* %0)
store i8* %12, i8** %8
%13 = call i32 @clCreateProgramWithSource(i32 %3, i32 1, i8** %8, i32* null, i64* %7)
%14 = load i64, i64* %7
%15 = call i32 @clError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %14)
%16 = call i64 @clBuildProgram(i32 %13, i32 1, i32* %6, i8* %2, i32* null, i32* null)
store i64 %16, i64* %7
%17 = load i64, i64* @CL_SUCCESS
%18 = icmp eq i64 %17, %16
br i1 %18, label %29, label %19
19: ; preds = %5
%20 = bitcast i64* %9 to i8*
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %6
%24 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%25 = call i32 @clGetProgramBuildInfo(i32 %13, i32 %23, i32 %24, i32 1048576, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @buildKernel.s, i64 0, i64 0), i64* %9)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, 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))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %5
%30 = call i32 @clCreateKernel(i32 %13, i8* %1, i64* %7)
%31 = load i64, i64* %7
%32 = call i32 @clError(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %31)
%33 = load i8*, i8** %8
%34 = call i32 @free(i8* %33)
ret i32 %30
}