#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 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*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ *clCreateProgramWithSource(int /*<<< orphan*/, int,
                                              char const **,
                                              int /*<<< orphan*/ *,
                                              scalar_t__ *);
int /*<<< orphan*/ clGetProgramBuildInfo(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/, int /*<<< orphan*/,
                                         size_t, char *, size_t *);
int /*<<< orphan*/ clReleaseProgram(int /*<<< orphan*/ *);
char *cl_utils_read_file(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ stderr;
int cl_utils_create_program(cl_program *program, char *filename,
                            cl_context context, cl_device_id device) {
  cl_int err;
  char *source_code;
  size_t build_log_size;
  char *build_log;
  source_code = cl_utils_read_file(filename);
  if (source_code == NULL) {
    fprintf(stderr, "cl_utils_create_program: could not read %s\n", filename);
    fflush(stderr);
    goto out_no_source;
  }
  *program = clCreateProgramWithSource(context, 1, (const char **)&source_code,
                                       NULL, &err);
  if (err != CL_SUCCESS) {
    fprintf(stderr,
            "cl_utils_create_program: clCreateProgramWithSource failed\n");
    fflush(stderr);
    goto out_no_program;
  }
  err = clBuildProgram(*program, 1, &device, NULL, NULL, NULL);
  if (err != CL_SUCCESS) {
    fprintf(stderr, "cl_utils_create_program: clBuildProgram failed\n");
    clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG, 0, NULL,
                          &build_log_size);
    build_log = malloc(build_log_size);
    if (build_log == NULL) {
      fprintf(stderr, "cl_utils_create_program: No memory for build log\n");
      fflush(stderr);
      goto out_build_fail;
    }
    clGetProgramBuildInfo(*program, device, CL_PROGRAM_BUILD_LOG,
                          build_log_size, build_log, NULL);
    fprintf(stderr, "%s\n", build_log);
    fflush(stderr);
    free(build_log);
    goto out_build_fail;
  }
  free(source_code);
  return 0;
out_build_fail:
  clReleaseProgram(*program);
  *program = NULL;
out_no_program:
  free(source_code);
out_no_source:
  return -1;
}


x86 assembly (clang, -O0):
<cl_utils_create_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <cl_utils_read_file@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ec <cl_utils_create_program+0x5c>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    1393 <cl_utils_create_program+0x203>
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    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
je     124d <cl_utils_create_program+0xbd>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    138a <cl_utils_create_program+0x1fa>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x1,%esi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
je     135e <cl_utils_create_program+0x1ce>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
mov    0x2d3b(%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),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1302 <cl_utils_create_program+0x172>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
jmp    1373 <cl_utils_create_program+0x1e3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1090 <clGetProgramBuildInfo@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xced(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
jmp    1373 <cl_utils_create_program+0x1e3>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    139a <cl_utils_create_program+0x20a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <clReleaseProgram@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_utils_create_program>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %ecx,0x4(%rsp)
mov    %rsi,%rdi
call   1050 <cl_utils_read_file@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     121d <cl_utils_create_program+0x8d>
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
call   1060 <clCreateProgramWithSource@plt>
mov    %rax,(%r15)
mov    0x10(%rsp),%rcx
mov    0x2def(%rip),%rbp
cmp    0x0(%rbp),%rcx
jne    1245 <cl_utils_create_program+0xb5>
xor    %ebx,%ebx
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %rax,0x10(%rsp)
cmp    0x0(%rbp),%rax
jne    1268 <cl_utils_create_program+0xd8>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
jmp    132f <cl_utils_create_program+0x19f>
mov    0x2db4(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdd2(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
call   10b0 <fflush@plt>
jmp    132a <cl_utils_create_program+0x19a>
mov    0x2d8c(%rip),%rbx
mov    (%rbx),%edi
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
call   10b0 <fflush@plt>
jmp    1320 <cl_utils_create_program+0x190>
mov    0x2d69(%rip),%r12
mov    (%r12),%edi
lea    0xded(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r15),%rdi
mov    0x4(%rsp),%esi
mov    0x2d59(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x18(%rsp),%r9
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <clGetProgramBuildInfo@plt>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12f6 <cl_utils_create_program+0x166>
mov    %rax,%r14
mov    (%r15),%rdi
mov    0x4(%rsp),%esi
mov    0x0(%rbp),%edx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %r9d,%r9d
call   1090 <clGetProgramBuildInfo@plt>
mov    (%r12),%edi
lea    0xd4f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r12),%edi
call   10b0 <fflush@plt>
mov    %r14,%rdi
call   1040 <free@plt>
jmp    1311 <cl_utils_create_program+0x181>
mov    (%r12),%edi
lea    0xd96(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r12),%edi
call   10b0 <fflush@plt>
mov    (%r15),%rdi
call   1070 <clReleaseProgram@plt>
movq   $0x0,(%r15)
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"cl_utils_create_program: could not read %s\0A\00"
@CL_SUCCESS = global i64 0
@.str.1 = [59 x i8] c"cl_utils_create_program: clCreateProgramWithSource failed\0A\00"
@.str.2 = [48 x i8] c"cl_utils_create_program: clBuildProgram failed\0A\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.3 = [50 x i8] c"cl_utils_create_program: No memory for build log\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @cl_utils_create_program(i32** %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
store i32** %0, i32*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i8*, i8** %7
%15 = call i8* @cl_utils_read_file(i8* %14)
store i8* %15, i8** %11
%16 = load i8*, i8** %11
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %7
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @stderr
%23 = call i32 @fflush(i32 %22)
br label %86
24: ; preds = %4
%25 = load i32, i32* %8
%26 = call i32* @clCreateProgramWithSource(i32 %25, i32 1, i8** %11, i32* null, i64* %10)
%27 = load i32**, i32*** %6
store i32* %26, i32** %27
%28 = load i64, i64* %10
%29 = load i64, i64* @CL_SUCCESS
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 @fflush(i32 %34)
br label %83
36: ; preds = %24
%37 = load i32**, i32*** %6
%38 = load i32*, i32** %37
%39 = call i64 @clBuildProgram(i32* %38, i32 1, i32* %9, i32* null, i32* null, i32* null)
store i64 %39, i64* %10
%40 = load i64, i64* %10
%41 = load i64, i64* @CL_SUCCESS
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %75
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32**, i32*** %6
%47 = load i32*, i32** %46
%48 = load i32, i32* %9
%49 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%50 = call i32 @clGetProgramBuildInfo(i32* %47, i32 %48, i32 %49, i64 0, i8* null, i64* %12)
%51 = load i64, i64* %12
%52 = call i8* @malloc(i64 %51)
store i8* %52, i8** %13
%53 = load i8*, i8** %13
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %60
55: ; preds = %43
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @stderr
%59 = call i32 @fflush(i32 %58)
br label %78
60: ; preds = %43
%61 = load i32**, i32*** %6
%62 = load i32*, i32** %61
%63 = load i32, i32* %9
%64 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%65 = load i64, i64* %12
%66 = load i8*, i8** %13
%67 = call i32 @clGetProgramBuildInfo(i32* %62, i32 %63, i32 %64, i64 %65, i8* %66, i64* null)
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %13
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %69)
%71 = load i32, i32* @stderr
%72 = call i32 @fflush(i32 %71)
%73 = load i8*, i8** %13
%74 = call i32 @free(i8* %73)
br label %78
75: ; preds = %36
%76 = load i8*, i8** %11
%77 = call i32 @free(i8* %76)
store i32 0, i32* %5
br label %87
78: ; preds = %60, %55
%79 = load i32**, i32*** %6
%80 = load i32*, i32** %79
%81 = call i32 @clReleaseProgram(i32* %80)
%82 = load i32**, i32*** %6
store i32* null, i32** %82
br label %83
83: ; preds = %78, %31
%84 = load i8*, i8** %11
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %83, %18
store i32 -1, i32* %5
br label %87
87: ; preds = %86, %75
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"cl_utils_create_program: could not read %s\0A\00"
@CL_SUCCESS = global i64 0
@.str.1 = [59 x i8] c"cl_utils_create_program: clCreateProgramWithSource failed\0A\00"
@.str.2 = [48 x i8] c"cl_utils_create_program: clBuildProgram failed\0A\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.3 = [50 x i8] c"cl_utils_create_program: No memory for build log\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @cl_utils_create_program(i32** nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 %3, i32* %5
%9 = bitcast i64* %6 to i8*
%10 = bitcast i8** %7 to i8*
%11 = bitcast i64* %8 to i8*
%12 = tail call i8* @cl_utils_read_file(i8* %1)
store i8* %12, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %1)
%17 = load i32, i32* @stderr
%18 = tail call i32 @fflush(i32 %17)
br label %67
19: ; preds = %4
%20 = call i32* @clCreateProgramWithSource(i32 %2, i32 1, i8** %7, i32* null, i64* %6)
store i32* %20, i32** %0
%21 = load i64, i64* %6
%22 = load i64, i64* @CL_SUCCESS
%23 = icmp eq i64 %21, %22
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 @fflush(i32 %27)
br label %64
29: ; preds = %19
%30 = call i64 @clBuildProgram(i32* %20, i32 1, i32* %5, i32* null, i32* null, i32* null)
store i64 %30, i64* %6
%31 = load i64, i64* @CL_SUCCESS
%32 = icmp eq i64 %30, %31
br i1 %32, label %58, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32*, i32** %0
%37 = load i32, i32* %5
%38 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%39 = call i32 @clGetProgramBuildInfo(i32* %36, i32 %37, i32 %38, i64 0, i8* null, i64* %8)
%40 = load i64, i64* %8
%41 = call i8* @malloc(i64 %40)
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = call i32 @fflush(i32 %46)
br label %61
48: ; preds = %33
%49 = load i32*, i32** %0
%50 = load i32, i32* %5
%51 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%52 = call i32 @clGetProgramBuildInfo(i32* %49, i32 %50, i32 %51, i64 %40, i8* %41, i64* null)
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%55 = load i32, i32* @stderr
%56 = call i32 @fflush(i32 %55)
%57 = call i32 @free(i8* %41)
br label %61
58: ; preds = %29
%59 = load i8*, i8** %7
%60 = call i32 @free(i8* %59)
br label %67
61: ; preds = %48, %43
%62 = load i32*, i32** %0
%63 = call i32 @clReleaseProgram(i32* %62)
store i32* null, i32** %0
br label %64
64: ; preds = %61, %24
%65 = load i8*, i8** %7
%66 = call i32 @free(i8* %65)
br label %67
67: ; preds = %14, %64, %58
%68 = phi i32 [ 0, %58 ], [ -1, %64 ], [ -1, %14 ]
ret i32 %68
}