#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_build_status;

/* Variables and functions */
int /*<<< orphan*/ CL_PROGRAM_BUILD_LOG;
int /*<<< orphan*/ CL_PROGRAM_BUILD_STATUS;
int CL_SUCCESS;
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ clGetProgramBuildInfo(int /*<<< orphan*/, int /*<<< orphan*/,
                                         int /*<<< orphan*/, size_t, ...);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
void chk(cl_int status, const char *cmd, cl_device_id *dev,
         cl_program *program) {
  if (status != CL_SUCCESS) {
    printf("%s failed (%d)\n", cmd, status);
    if (program && dev) {
      char *programLog;
      size_t logSize;
      clGetProgramBuildInfo(*program, *dev, CL_PROGRAM_BUILD_STATUS,
                            sizeof(cl_build_status), &status, NULL);
      clGetProgramBuildInfo(*program, *dev, CL_PROGRAM_BUILD_LOG, 0, NULL,
                            &logSize);
      programLog = (char *)calloc(logSize + 1, sizeof(char));
      clGetProgramBuildInfo(*program, *dev, CL_PROGRAM_BUILD_LOG, logSize + 1,
                            programLog, NULL);
      printf("Build failed; status=%d, programLog:nn%s", status, programLog);
      free(programLog);
    }
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
je     1261 <chk+0x111>
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1257 <chk+0x107>
cmpq   $0x0,-0x18(%rbp)
je     1257 <chk+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%ecx
lea    -0x4(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
mov    $0x0,%al
call   1060 <clGetProgramBuildInfo@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x30(%rbp),%r9
mov    %r8,%rcx
mov    $0x0,%al
call   1060 <clGetProgramBuildInfo@plt>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
mov    $0x0,%al
call   1060 <clGetProgramBuildInfo@plt>
mov    -0x4(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chk>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,0xc(%rsp)
mov    0x2e6a(%rip),%rax
cmp    %edi,(%rax)
jne    1176 <chk+0x26>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %edi,%edx
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbx,%rbx
je     1233 <chk+0xe3>
test   %r14,%r14
je     1233 <chk+0xe3>
mov    (%r14),%edi
mov    (%rbx),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%r8
mov    $0x4,%ecx
xor    %r9d,%r9d
xor    %eax,%eax
call   1060 <clGetProgramBuildInfo@plt>
mov    (%r14),%edi
mov    (%rbx),%esi
mov    0x2e1c(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%r9
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %eax,%eax
call   1060 <clGetProgramBuildInfo@plt>
mov    0x10(%rsp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    (%r14),%edi
mov    (%rbx),%esi
mov    (%r12),%edx
mov    0x10(%rsp),%rcx
add    $0x1,%rcx
mov    %rax,%r8
xor    %r9d,%r9d
xor    %eax,%eax
call   1060 <clGetProgramBuildInfo@plt>
mov    0xc(%rsp),%esi
lea    0xdef(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@CL_SUCCESS = global i32 0
@.str = [16 x i8] c"%s failed (%d)\0A\00"
@CL_PROGRAM_BUILD_STATUS = global i32 0
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.1 = [41 x i8] c"Build failed; status=%d, programLog:nn%s\00"
define void @chk(i32 %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%11 = load i32, i32* %5
%12 = load i32, i32* @CL_SUCCESS
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %56
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = load i32, i32* %5
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
%18 = load i32*, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %54
20: ; preds = %14
%21 = load i32*, i32** %7
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %54
23: ; preds = %20
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = load i32, i32* @CL_PROGRAM_BUILD_STATUS
%29 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %25, i32 %27, i32 %28, i64 4, i32* %5, i8* null)
%30 = load i32*, i32** %8
%31 = load i32, i32* %30
%32 = load i32*, i32** %7
%33 = load i32, i32* %32
%34 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%35 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %31, i32 %33, i32 %34, i64 0, i8* null, i64* %10)
%36 = load i64, i64* %10
%37 = add i64 %36, 1
%38 = call i64 @calloc(i64 %37, i32 1)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %9
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = load i32*, i32** %7
%43 = load i32, i32* %42
%44 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%45 = load i64, i64* %10
%46 = add i64 %45, 1
%47 = load i8*, i8** %9
%48 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %41, i32 %43, i32 %44, i64 %46, i8* %47, i8* null)
%49 = load i32, i32* %5
%50 = load i8*, i8** %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %49, i8* %50)
%52 = load i8*, i8** %9
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %23, %20, %14
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %4
ret void
}

LLVM IR (-O2):
@CL_SUCCESS = global i32 0
@.str = [16 x i8] c"%s failed (%d)\0A\00"
@CL_PROGRAM_BUILD_STATUS = global i32 0
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.1 = [41 x i8] c"Build failed; status=%d, programLog:nn%s\00"
define void @chk(i32 %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i64
store i32 %0, i32* %5
%7 = load i32, i32* @CL_SUCCESS
%8 = icmp eq i32 %7, %0
br i1 %8, label %39, label %9
9: ; preds = %4
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %0)
%11 = icmp ne i32* %3, null
%12 = icmp ne i32* %2, null
%13 = and i1 %12, %11
br i1 %13, label %14, label %37
14: ; preds = %9
%15 = bitcast i64* %6 to i8*
%16 = load i32, i32* %3
%17 = load i32, i32* %2
%18 = load i32, i32* @CL_PROGRAM_BUILD_STATUS
%19 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %16, i32 %17, i32 %18, i64 4, i32* %5, i8* null)
%20 = load i32, i32* %3
%21 = load i32, i32* %2
%22 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%23 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %20, i32 %21, i32 %22, i64 0, i8* null, i64* %6)
%24 = load i64, i64* %6
%25 = add i64 %24, 1
%26 = call i64 @calloc(i64 %25, i32 1)
%27 = inttoptr i64 %26 to i8*
%28 = load i32, i32* %3
%29 = load i32, i32* %2
%30 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%31 = load i64, i64* %6
%32 = add i64 %31, 1
%33 = call i32 (i32, i32, i32, i64, ...) @clGetProgramBuildInfo(i32 %28, i32 %29, i32 %30, i64 %32, i8* %27, i8* null)
%34 = load i32, i32* %5
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %34, i8* %27)
%36 = call i32 @free(i8* %27)
br label %37
37: ; preds = %14, %9
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %4
ret void
}