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

/* Variables and functions */
int /*<<< orphan*/ CL_PROGRAM_BUILD_LOG;
scalar_t__ check_cl_err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ clBuildProgram(int /*<<< orphan*/ *, int,
                                  int /*<<< orphan*/ *, char const *,
                                  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*/ clReleaseProgram(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
cl_program build_program(const char *src, cl_context context,
                         cl_device_id device, const char *options) {
  cl_int err;
  cl_program program = clCreateProgramWithSource(context, 1, &src, NULL, &err);
  if (check_cl_err(err, "Failed to create program from source")) {
    return NULL;
  }
  err = clBuildProgram(program, 1, &device, options, NULL, NULL);
  if (check_cl_err(err, "Failed to build program for device")) {
    size_t sz;
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &sz);
    char *log = malloc(sizeof(char) * sz + 1);
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sz, log, NULL);
    log[sz] = '\0';
    fprintf(stderr, "BUILD LOG: %s\n", log);
    free(log);
    clReleaseProgram(program);
    return NULL;
  }
  return program;
}


x86 assembly (clang, -O0):
<build_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%r8
call   1060 <clCreateProgramWithSource@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe43(%rip),%rsi
call   1050 <check_cl_err@plt>
cmp    $0x0,%rax
je     11d9 <build_program+0x59>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <build_program+0x139>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe21(%rip),%rsi
call   1050 <check_cl_err@plt>
cmp    $0x0,%rax
je     12b1 <build_program+0x131>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x38(%rbp),%r9
mov    %r8,%rcx
call   1090 <clGetProgramBuildInfo@plt>
mov    -0x38(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1090 <clGetProgramBuildInfo@plt>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xdbb(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
call   1070 <clReleaseProgram@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <build_program+0x139>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_program>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r15
mov    %rdi,0x18(%rsp)
mov    %edx,0x8(%rsp)
xor    %r12d,%r12d
lea    0x18(%rsp),%rdx
lea    0xc(%rsp),%r8
mov    %esi,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
call   1060 <clCreateProgramWithSource@plt>
mov    %rax,%r14
mov    0xc(%rsp),%edi
lea    0xe3e(%rip),%rsi
call   1050 <check_cl_err@plt>
test   %rax,%rax
jne    128a <build_program+0x10a>
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %eax,0xc(%rsp)
lea    0xe2f(%rip),%rsi
mov    %eax,%edi
call   1050 <check_cl_err@plt>
test   %rax,%rax
je     1287 <build_program+0x107>
mov    0x8(%rsp),%esi
mov    0x2dd7(%rip),%r13
mov    0x0(%r13),%edx
xor    %r12d,%r12d
lea    0x10(%rsp),%r9
mov    %r14,%rdi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <clGetProgramBuildInfo@plt>
mov    0x10(%rsp),%r15
lea    0x1(%r15),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%esi
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    %r15,%rcx
mov    %rax,%r8
xor    %r9d,%r9d
call   1090 <clGetProgramBuildInfo@plt>
mov    0x10(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1070 <clReleaseProgram@plt>
jmp    128a <build_program+0x10a>
mov    %r14,%r12
mov    %r12,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Failed to create program from source\00"
@.str.1 = [35 x i8] c"Failed to build program for device\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@stderr = global i32 0
@.str.2 = [15 x i8] c"BUILD LOG: %s\0A\00"
define i32* @build_program(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i32, i32* %7
%15 = call i32* @clCreateProgramWithSource(i32 %14, i32 1, i8** %6, i32* null, i32* %10)
store i32* %15, i32** %11
%16 = load i32, i32* %10
%17 = call i64 @check_cl_err(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
store i32* null, i32** %5
br label %55
20: ; preds = %4
%21 = load i32*, i32** %11
%22 = load i8*, i8** %9
%23 = call i32 @clBuildProgram(i32* %21, i32 1, i32* %8, i8* %22, i32* null, i32* null)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = call i64 @check_cl_err(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %20
%28 = load i32*, i32** %11
%29 = load i32, i32* %8
%30 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%31 = call i32 @clGetProgramBuildInfo(i32* %28, i32 %29, i32 %30, i64 0, i8* null, i64* %12)
%32 = load i64, i64* %12
%33 = mul i64 1, %32
%34 = add i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %13
%37 = load i32*, i32** %11
%38 = load i32, i32* %8
%39 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%40 = load i64, i64* %12
%41 = load i8*, i8** %13
%42 = call i32 @clGetProgramBuildInfo(i32* %37, i32 %38, i32 %39, i64 %40, i8* %41, i64* null)
%43 = load i8*, i8** %13
%44 = load i64, i64* %12
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %13
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %13
%50 = call i32 @free(i8* %49)
%51 = load i32*, i32** %11
%52 = call i32 @clReleaseProgram(i32* %51)
store i32* null, i32** %5
br label %55
53: ; preds = %20
%54 = load i32*, i32** %11
store i32* %54, i32** %5
br label %55
55: ; preds = %53, %27, %19
%56 = load i32*, i32** %5
ret i32* %56
}

LLVM IR (-O2):
@.str = [37 x i8] c"Failed to create program from source\00"
@.str.1 = [35 x i8] c"Failed to build program for device\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@stderr = global i32 0
@.str.2 = [15 x i8] c"BUILD LOG: %s\0A\00"
define i32* @build_program(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i32 %2, i32* %6
%9 = bitcast i32* %7 to i8*
%10 = call i32* @clCreateProgramWithSource(i32 %1, i32 1, i8** %5, i32* null, i32* %7)
%11 = load i32, i32* %7
%12 = call i64 @check_cl_err(i32 %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %4
%15 = call i32 @clBuildProgram(i32* %10, i32 1, i32* %6, i8* %3, i32* null, i32* null)
store i32 %15, i32* %7
%16 = call i64 @check_cl_err(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %36, label %18
18: ; preds = %14
%19 = bitcast i64* %8 to i8*
%20 = load i32, i32* %6
%21 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%22 = call i32 @clGetProgramBuildInfo(i32* %10, i32 %20, i32 %21, i64 0, i8* null, i64* %8)
%23 = load i64, i64* %8
%24 = trunc i64 %23 to i32
%25 = add i32 %24, 1
%26 = call i8* @malloc(i32 %25)
%27 = load i32, i32* %6
%28 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%29 = call i32 @clGetProgramBuildInfo(i32* %10, i32 %27, i32 %28, i64 %23, i8* %26, i64* null)
%30 = load i64, i64* %8
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%34 = call i32 @free(i8* %26)
%35 = call i32 @clReleaseProgram(i32* %10)
br label %36
36: ; preds = %14, %4, %18
%37 = phi i32* [ null, %18 ], [ null, %4 ], [ %10, %14 ]
ret i32* %37
}