#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_uint;
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*/ clBuildProgram(int /*<<< orphan*/, int, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/
clCreateProgramWithBinary(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/ *, size_t const *,
                          unsigned char const **, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ oclCheckStatus(int /*<<< orphan*/, char *);
unsigned char *oclGetProgramBinFromFile(char const *, size_t *);
cl_program oclCreateProgramFromBinary(cl_context context, cl_device_id device,
                                      const char *filename) {
  size_t length;
  unsigned char *binary = oclGetProgramBinFromFile(filename, &length);
  cl_int status;
  cl_int binary_status;
  cl_program program = clCreateProgramWithBinary(
      context, (cl_uint)1, &device, (const size_t *)&length,
      (const unsigned char **)&binary, &binary_status, &status);
  oclCheckStatus(status, "clCreateProgramWithBinary failed.");
  status = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
  oclCheckStatus(status, "clBuildProgram failed.");
  return program;
}


x86 assembly (clang, -O0):
<oclCreateProgramFromBinary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <oclGetProgramBinFromFile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
lea    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%r8
lea    -0x28(%rbp),%r9
lea    -0x24(%rbp),%rax
mov    %rax,(%rsp)
call   1060 <clCreateProgramWithBinary@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe6b(%rip),%rsi
call   1050 <oclCheckStatus@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
lea    -0x8(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe5f(%rip),%rsi
call   1050 <oclCheckStatus@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<oclCreateProgramFromBinary>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    %esi,0x14(%rsp)
lea    0x28(%rsp),%r14
mov    %rdx,%rdi
mov    %r14,%rsi
call   1040 <oclGetProgramBinFromFile@plt>
mov    %rax,0x18(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,(%rsp)
lea    0x14(%rsp),%r15
lea    0x18(%rsp),%r8
lea    0x24(%rsp),%r9
mov    %ebx,%edi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <clCreateProgramWithBinary@plt>
mov    %eax,%ebx
mov    0x10(%rsp),%edi
lea    0xe65(%rip),%rsi
call   1050 <oclCheckStatus@plt>
mov    %ebx,%edi
mov    $0x1,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %eax,0x10(%rsp)
lea    0xe60(%rip),%rsi
mov    %eax,%edi
call   1050 <oclCheckStatus@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"clCreateProgramWithBinary failed.\00"
@.str.1 = [23 x i8] c"clBuildProgram failed.\00"
define i32 @oclCreateProgramFromBinary(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%12 = load i8*, i8** %6
%13 = call i8* @oclGetProgramBinFromFile(i8* %12, i64* %7)
store i8* %13, i8** %8
%14 = load i32, i32* %4
%15 = call i32 @clCreateProgramWithBinary(i32 %14, i32 1, i32* %5, i64* %7, i8** %8, i32* %10, i32* %9)
store i32 %15, i32* %11
%16 = load i32, i32* %9
%17 = call i32 @oclCheckStatus(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %11
%19 = call i32 @clBuildProgram(i32 %18, i32 1, i32* %5, i32* null, i32* null, i32* null)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = call i32 @oclCheckStatus(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %11
ret i32 %22
}

LLVM IR (-O2):
@.str = [34 x i8] c"clCreateProgramWithBinary failed.\00"
@.str.1 = [23 x i8] c"clBuildProgram failed.\00"
define i32 @oclCreateProgramFromBinary(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %1, i32* %4
%9 = bitcast i64* %5 to i8*
%10 = bitcast i8** %6 to i8*
%11 = call i8* @oclGetProgramBinFromFile(i8* %2, i64* %5)
store i8* %11, i8** %6
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = call i32 @clCreateProgramWithBinary(i32 %0, i32 1, i32* %4, i64* %5, i8** %6, i32* %8, i32* %7)
%15 = load i32, i32* %7
%16 = call i32 @oclCheckStatus(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @clBuildProgram(i32 %14, i32 1, i32* %4, i32* null, i32* null, i32* null)
store i32 %17, i32* %7
%18 = call i32 @oclCheckStatus(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
ret i32 %14
}