#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_ulong;
typedef int cl_uint;
typedef int /*<<< orphan*/ *cl_program;
typedef int /*<<< orphan*/ *cl_platform_id;
typedef int /*<<< orphan*/ cl_mem;
typedef int /*<<< orphan*/ *cl_kernel;
typedef int cl_int;
typedef int /*<<< orphan*/ cl_event;
typedef int /*<<< orphan*/ *cl_device_id;
typedef int /*<<< orphan*/ *cl_context;
typedef int /*<<< orphan*/ *cl_command_queue;

/* Variables and functions */
int /*<<< orphan*/ clReleaseCommandQueue(int /*<<< orphan*/ *);
int /*<<< orphan*/ clReleaseContext(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int init_cl(int /*<<< orphan*/ **, int /*<<< orphan*/ **, int /*<<< orphan*/ **,
            int /*<<< orphan*/ **);
int /*<<< orphan*/ printf(char *, ...);
void calc_opencl(int datasize, double *data1, double *data2, double *result,
                 const char *cl_filename) {
  cl_int num_rand = 4096 * 256;
  int count_all, i;
  int num_generator = 32;
  double pi;
  cl_platform_id platform_id = NULL;
  cl_device_id device_id = NULL;
  cl_context context = NULL;
  cl_command_queue command_queue = NULL;
  cl_uint num_compute_unit;
  cl_uint num_work_item;
  cl_program program = NULL;
  cl_kernel kernel_mt = NULL, kernel_pi = NULL;
  cl_mem rand, count;
  size_t global_item_size[3], local_item_size[3];
  cl_mem dev_mts;
  cl_event ev_mt_end, ev_pi_end, ev_copy_end;
  cl_ulong prof_start, prof_mt_end, prof_pi_end, prof_copy_end;
  cl_int ret;
  printf(" [Initialize Device]\n");
  num_compute_unit =
      init_cl(&platform_id, &device_id, &context, &command_queue);
  if (num_compute_unit < 1) {
    exit(-1);
  }
  num_work_item = (num_generator + (num_compute_unit - 1)) / num_compute_unit;
  printf("   * Wrok Items: %d\n", num_work_item);
  global_item_size[0] = num_work_item * num_compute_unit;
  global_item_size[1] = 1;
  global_item_size[2] = 1;
  local_item_size[0] = num_work_item;
  local_item_size[1] = 1;
  local_item_size[2] = 1;
  printf(" [Calculation]\n");
  printf(" [Profile]\n");
  clReleaseCommandQueue(command_queue);
  clReleaseContext(context);
}


x86 assembly (clang, -O0):
<calc_opencl>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x100000,-0x2c(%rbp)
movl   $0x20,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x70(%rbp)
movq   $0x0,-0x78(%rbp)
movq   $0x0,-0x80(%rbp)
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   1030 <init_cl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x1,-0x64(%rbp)
jge    11ee <calc_opencl+0x9e>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%eax
mov    -0x64(%rbp),%ecx
sub    $0x1,%ecx
add    %ecx,%eax
cltd
idivl  -0x64(%rbp)
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x68(%rbp),%eax
imul   -0x64(%rbp),%eax
cltq
mov    %rax,-0xa0(%rbp)
movq   $0x1,-0x98(%rbp)
movq   $0x1,-0x90(%rbp)
movslq -0x68(%rbp),%rax
mov    %rax,-0xc0(%rbp)
movq   $0x1,-0xb8(%rbp)
movq   $0x1,-0xb0(%rbp)
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x60(%rbp),%rdi
call   1050 <clReleaseCommandQueue@plt>
mov    -0x58(%rbp),%rdi
call   1060 <clReleaseContext@plt>
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_opencl>:
sub    $0x28,%rsp
movq   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1030 <init_cl@plt>
test   %eax,%eax
jle    11fb <calc_opencl+0x9b>
mov    %eax,%ecx
add    $0x1f,%eax
xor    %edx,%edx
div    %ecx
lea    0xe3f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
lea    0xe5c(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%rdi
call   1060 <clReleaseCommandQueue@plt>
mov    0x10(%rsp),%rdi
call   1070 <clReleaseContext@plt>
add    $0x28,%rsp
ret
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c" [Initialize Device]\0A\00"
@.str.1 = [21 x i8] c" * Wrok Items: %d\0A\00"
@.str.2 = [16 x i8] c" [Calculation]\0A\00"
@.str.3 = [12 x i8] c" [Profile]\0A\00"
define void @calc_opencl(i32 %0, double* %1, double* %2, double* %3, i8* %4) {
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca double*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca double
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i32*
%19 = alloca i32*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32*
%23 = alloca i32*
%24 = alloca i32*
%25 = alloca i32
%26 = alloca i32
%27 = alloca [3 x i64]
%28 = alloca [3 x i64]
%29 = alloca i32
%30 = alloca i32
%31 = alloca i32
%32 = alloca i32
%33 = alloca i32
%34 = alloca i32
%35 = alloca i32
%36 = alloca i32
%37 = alloca i32
store i32 %0, i32* %6
store double* %1, double** %7
store double* %2, double** %8
store double* %3, double** %9
store i8* %4, i8** %10
store i32 1048576, i32* %11
store i32 32, i32* %14
store i32* null, i32** %16
store i32* null, i32** %17
store i32* null, i32** %18
store i32* null, i32** %19
store i32* null, i32** %22
store i32* null, i32** %23
store i32* null, i32** %24
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @init_cl(i32** %16, i32** %17, i32** %18, i32** %19)
store i32 %39, i32* %20
%40 = load i32, i32* %20
%41 = icmp slt i32 %40, 1
br i1 %41, label %42, label %44
42: ; preds = %5
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %5
%45 = load i32, i32* %14
%46 = load i32, i32* %20
%47 = sub nsw i32 %46, 1
%48 = add nsw i32 %45, %47
%49 = load i32, i32* %20
%50 = sdiv i32 %48, %49
store i32 %50, i32* %21
%51 = load i32, i32* %21
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %21
%54 = load i32, i32* %20
%55 = mul nsw i32 %53, %54
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [3 x i64], [3 x i64]* %27, i64 0, i64 0
store i64 %56, i64* %57
%58 = getelementptr inbounds [3 x i64], [3 x i64]* %27, i64 0, i64 1
store i64 1, i64* %58
%59 = getelementptr inbounds [3 x i64], [3 x i64]* %27, i64 0, i64 2
store i64 1, i64* %59
%60 = load i32, i32* %21
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [3 x i64], [3 x i64]* %28, i64 0, i64 0
store i64 %61, i64* %62
%63 = getelementptr inbounds [3 x i64], [3 x i64]* %28, i64 0, i64 1
store i64 1, i64* %63
%64 = getelementptr inbounds [3 x i64], [3 x i64]* %28, i64 0, i64 2
store i64 1, i64* %64
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%67 = load i32*, i32** %19
%68 = call i32 @clReleaseCommandQueue(i32* %67)
%69 = load i32*, i32** %18
%70 = call i32 @clReleaseContext(i32* %69)
ret void
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c" * Wrok Items: %d\0A\00"
@str = [21 x i8] c" [Initialize Device]\00"
@str.4 = [15 x i8] c" [Calculation]\00"
@str.5 = [11 x i8] c" [Profile]\00"
define void @calc_opencl(i32 %0, double* nocapture readnone %1, double* nocapture readnone %2, double* nocapture readnone %3, i8* nocapture readnone %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = bitcast i32** %6 to i8*
store i32* null, i32** %6
%11 = bitcast i32** %7 to i8*
store i32* null, i32** %7
%12 = bitcast i32** %8 to i8*
store i32* null, i32** %8
%13 = bitcast i32** %9 to i8*
store i32* null, i32** %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%15 = call i32 @init_cl(i32** %6, i32** %7, i32** %8, i32** %9)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %5
%20 = add nuw nsw i32 %15, 31
%21 = udiv i32 %20, %15
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0))
%25 = load i32*, i32** %9
%26 = call i32 @clReleaseCommandQueue(i32* %25)
%27 = load i32*, i32** %8
%28 = call i32 @clReleaseContext(i32* %27)
ret void
}