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

/* Variables and functions */
int /*<<< orphan*/ CL_PROGRAM_BUILD_LOG;
int /*<<< orphan*/ SEEK_END;
int clBuildProgram(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ clCreateProgramWithSource(int /*<<< orphan*/, int,
                                             char const **, size_t *, int *);
int /*<<< orphan*/ clGetProgramBuildInfo(int /*<<< orphan*/, int /*<<< orphan*/,
                                         int /*<<< orphan*/, size_t, char *,
                                         size_t *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
cl_program build_program(cl_context ctx, cl_device_id dev,
                         const char *filename) {
  cl_program program;
  FILE *program_handle;
  char *program_buffer, *program_log;
  size_t program_size, log_size;
  int err;
  program_handle = fopen(filename, "r");
  if (program_handle == NULL) {
    perror("Couldn't find the program file");
    exit(1);
  }
  fseek(program_handle, 0, SEEK_END);
  program_size = ftell(program_handle);
  rewind(program_handle);
  program_buffer = (char *)malloc(program_size + 1);
  program_buffer[program_size] = '\0';
  fread(program_buffer, sizeof(char), program_size, program_handle);
  fclose(program_handle);
  program = clCreateProgramWithSource(ctx, 1, (const char **)&program_buffer,
                                      &program_size, &err);
  if (err < 0) {
    perror("Couldn't create the program");
    exit(1);
  }
  free(program_buffer);
  err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  if (err < 0) {
    clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG, 0, NULL,
                          &log_size);
    program_log = (char *)malloc(log_size + 1);
    program_log[log_size] = '\0';
    clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG, log_size + 1,
                          program_log, NULL);
    printf("%s\n", program_log);
    free(program_log);
    exit(1);
  }
  return program;
}


x86 assembly (clang, -O0):
<build_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1227 <build_program+0x47>
lea    0xdea(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <rewind@plt>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
lea    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    -0x44(%rbp),%r8
call   1050 <clCreateProgramWithSource@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    12c9 <build_program+0xe9>
lea    0xd67(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rdx
mov    %r9,%rcx
mov    %r9,%r8
call   1030 <clBuildProgram@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    137c <build_program+0x19c>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x40(%rbp),%r9
mov    %r8,%rcx
call   10a0 <clGetProgramBuildInfo@plt>
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2c9f(%rip),%rax
mov    (%rax),%edx
mov    -0x40(%rbp),%rcx
add    $0x1,%rcx
mov    -0x30(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   10a0 <clGetProgramBuildInfo@plt>
mov    -0x30(%rbp),%rsi
lea    0xcd9(%rip),%rdi
call   1080 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_program>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe09(%rip),%rsi
mov    %rdx,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12be <build_program+0xde>
mov    %rax,%rbx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbx,%rdi
call   10b0 <ftell@plt>
mov    %rax,%rbp
mov    %rax,0x20(%rsp)
mov    %rbx,%rdi
call   1090 <rewind@plt>
lea    0x1(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,0x18(%rsp)
movb   $0x0,(%rax,%rbp,1)
mov    0x18(%rsp),%rdi
mov    0x20(%rsp),%rdx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1060 <fread@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0x18(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r15d,%edi
mov    $0x1,%esi
call   1050 <clCreateProgramWithSource@plt>
cmpl   $0x0,0xc(%rsp)
js     12c7 <build_program+0xe7>
mov    %eax,%ebp
mov    0x18(%rsp),%rdi
call   1040 <free@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <clBuildProgram@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     12dd <build_program+0xfd>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3d(%rip),%rdi
jmp    12ce <build_program+0xee>
lea    0xd53(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2cfc(%rip),%r15
mov    (%r15),%edx
lea    0x10(%rsp),%r9
mov    %ebp,%edi
mov    %r14d,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <clGetProgramBuildInfo@plt>
mov    0x10(%rsp),%rdi
add    $0x1,%rdi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
mov    (%r15),%edx
mov    0x10(%rsp),%rcx
add    $0x1,%rcx
mov    %ebp,%edi
mov    %r14d,%esi
mov    %rbx,%r8
xor    %r9d,%r9d
call   10a0 <clGetProgramBuildInfo@plt>
lea    0xd05(%rip),%rdi
mov    %rbx,%rsi
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [31 x i8] c"Couldn't find the program file\00"
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c"Couldn't create the program\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @build_program(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = load i32*, i32** %8
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %8
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %11
%27 = load i32*, i32** %8
%28 = call i32 @rewind(i32* %27)
%29 = load i64, i64* %11
%30 = add i64 %29, 1
%31 = call i64 @malloc(i64 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = load i64, i64* %11
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %9
%37 = load i64, i64* %11
%38 = load i32*, i32** %8
%39 = call i32 @fread(i8* %36, i32 1, i64 %37, i32* %38)
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %4
%43 = call i32 @clCreateProgramWithSource(i32 %42, i32 1, i8** %9, i64* %11, i32* %13)
store i32 %43, i32* %7
%44 = load i32, i32* %13
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %21
%47 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %21
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %7
%53 = call i32 @clBuildProgram(i32 %52, i32 0, i32* null, i32* null, i32* null, i32* null)
store i32 %53, i32* %13
%54 = load i32, i32* %13
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %80
56: ; preds = %49
%57 = load i32, i32* %7
%58 = load i32, i32* %5
%59 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%60 = call i32 @clGetProgramBuildInfo(i32 %57, i32 %58, i32 %59, i64 0, i8* null, i64* %12)
%61 = load i64, i64* %12
%62 = add i64 %61, 1
%63 = call i64 @malloc(i64 %62)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %10
%65 = load i8*, i8** %10
%66 = load i64, i64* %12
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8 0, i8* %67
%68 = load i32, i32* %7
%69 = load i32, i32* %5
%70 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%71 = load i64, i64* %12
%72 = add i64 %71, 1
%73 = load i8*, i8** %10
%74 = call i32 @clGetProgramBuildInfo(i32 %68, i32 %69, i32 %70, i64 %72, i8* %73, i64* null)
%75 = load i8*, i8** %10
%76 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %75)
%77 = load i8*, i8** %10
%78 = call i32 @free(i8* %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %49
%81 = load i32, i32* %7
ret i32 %81
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [31 x i8] c"Couldn't find the program file\00"
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c"Couldn't create the program\00"
@CL_PROGRAM_BUILD_LOG = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @build_program(i32 %0, i32 %1, i8* nocapture %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = bitcast i8** %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = bitcast i64* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32, i32* @SEEK_END
%19 = tail call i32 @fseek(i32* %12, i32 0, i32 %18)
%20 = tail call i64 @ftell(i32* %12)
store i64 %20, i64* %5
%21 = tail call i32 @rewind(i32* %12)
%22 = add i64 %20, 1
%23 = tail call i64 @malloc(i64 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 %20
store i8 0, i8* %25
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = tail call i32 @fread(i8* %26, i32 1, i64 %27, i32* %12)
%29 = tail call i32 @fclose(i32* %12)
%30 = call i32 @clCreateProgramWithSource(i32 %0, i32 1, i8** %4, i64* %5, i32* %7)
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %17
%34 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %17
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
%39 = call i32 @clBuildProgram(i32 %30, i32 0, i32* null, i32* null, i32* null, i32* null)
store i32 %39, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %57
41: ; preds = %36
%42 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%43 = call i32 @clGetProgramBuildInfo(i32 %30, i32 %1, i32 %42, i64 0, i8* null, i64* %6)
%44 = load i64, i64* %6
%45 = add i64 %44, 1
%46 = call i64 @malloc(i64 %45)
%47 = inttoptr i64 %46 to i8*
%48 = load i64, i64* %6
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i32, i32* @CL_PROGRAM_BUILD_LOG
%51 = load i64, i64* %6
%52 = add i64 %51, 1
%53 = call i32 @clGetProgramBuildInfo(i32 %30, i32 %1, i32 %50, i64 %52, i8* %47, i64* null)
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%55 = call i32 @free(i8* %47)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %36
ret i32 %30
}