#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 float cl_ulong;
typedef int /*<<< orphan*/ cl_kernel;
typedef scalar_t__ cl_int;
typedef int /*<<< orphan*/ cl_event;
typedef int /*<<< orphan*/ cl_command_queue;

/* Variables and functions */
int /*<<< orphan*/ CL_PROFILING_COMMAND_END;
int /*<<< orphan*/ CL_PROFILING_COMMAND_START;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ assert(int);
scalar_t__ clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, size_t const *,
                                  size_t const *, int /*<<< orphan*/,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ clFinish(int /*<<< orphan*/);
scalar_t__ clGetEventProfilingInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                   float *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, float);
int /*<<< orphan*/ stderr;
int RunKernel(cl_command_queue cmd_q, cl_kernel kernel, int num_elem) {
  const size_t global_size[1] = {(num_elem + 255) / 256 * 256};
  const size_t local_size[1] = {256};
  cl_event e;
  cl_int status = clEnqueueNDRangeKernel(cmd_q, kernel, 1, NULL, global_size,
                                         local_size, 0, NULL, &e);
  assert(status == CL_SUCCESS);
  status = clFinish(cmd_q);
  assert(status == CL_SUCCESS);
  cl_ulong t_start = 0;
  status = clGetEventProfilingInfo(e, CL_PROFILING_COMMAND_START,
                                   sizeof(cl_ulong), &t_start, NULL);
  assert(status == CL_SUCCESS);
  cl_ulong t_end = 0;
  status = clGetEventProfilingInfo(e, CL_PROFILING_COMMAND_END,
                                   sizeof(cl_ulong), &t_end, NULL);
  assert(status == CL_SUCCESS);
  fprintf(stderr, "\tKernel exec time: %8.2f us\n",
          1.f * (t_end - t_start) / 1e3);
  return 0;
}


x86 assembly (clang, -O0):
<RunKernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xff,%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
shl    $0x8,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x18(%rbp),%r8
lea    -0x20(%rbp),%r9
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %eax,%eax
lea    -0x24(%rbp),%rax
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <clEnqueueNDRangeKernel@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x4(%rbp),%edi
call   1030 <clFinish@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x34(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetEventProfilingInfo@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x38(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetEventProfilingInfo@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
movss  -0x38(%rbp),%xmm1
subss  -0x34(%rbp),%xmm1
movss  0xd67(%rip),%xmm0
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd47(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
lea    0xd4c(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RunKernel>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0xff(%rdx),%eax
add    $0x1fe,%edx
test   %eax,%eax
cmovns %eax,%edx
and    $0xffffff00,%edx
movslq %edx,%rax
mov    %rax,0x20(%rsp)
movq   $0x100,0x18(%rsp)
sub    $0x8,%rsp
lea    0x1c(%rsp),%rax
lea    0x28(%rsp),%r8
lea    0x20(%rsp),%r9
mov    $0x1,%edx
xor    %ecx,%ecx
push   %rax
push   $0x0
push   $0x0
call   1040 <clEnqueueNDRangeKernel@plt>
add    $0x20,%rsp
mov    0x2e18(%rip),%r14
xor    %edi,%edi
cmp    (%r14),%rax
sete   %dil
call   1060 <assert@plt>
mov    %ebx,%edi
call   1030 <clFinish@plt>
xor    %edi,%edi
cmp    (%r14),%rax
sete   %dil
call   1060 <assert@plt>
movl   $0x0,0x10(%rsp)
mov    0x14(%rsp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rcx
mov    $0x4,%edx
xor    %r8d,%r8d
call   1050 <clGetEventProfilingInfo@plt>
xor    %edi,%edi
cmp    (%r14),%rax
sete   %dil
call   1060 <assert@plt>
movl   $0x0,0xc(%rsp)
mov    0x14(%rsp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rcx
mov    $0x4,%edx
xor    %r8d,%r8d
call   1050 <clGetEventProfilingInfo@plt>
xor    %edi,%edi
cmp    (%r14),%rax
sete   %dil
call   1060 <assert@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
movss  0xc(%rsp),%xmm0
subss  0x10(%rsp),%xmm0
divss  0xda6(%rip),%xmm0
lea    0xda3(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.RunKernel.local_size = [1 x i64] [i64 256]
@CL_SUCCESS = global i64 0
@CL_PROFILING_COMMAND_START = global i32 0
@CL_PROFILING_COMMAND_END = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"\09Kernel exec time: %8.2f us\0A\00"
define i32 @RunKernel(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1 x i64]
%8 = alloca [1 x i64]
%9 = alloca i32
%10 = alloca i64
%11 = alloca float
%12 = alloca float
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = getelementptr inbounds [1 x i64], [1 x i64]* %7, i64 0, i64 0
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 255
%16 = sdiv i32 %15, 256
%17 = mul nsw i32 %16, 256
%18 = sext i32 %17 to i64
store i64 %18, i64* %13
%19 = bitcast [1 x i64]* %8 to i8*
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = getelementptr inbounds [1 x i64], [1 x i64]* %7, i64 0, i64 0
%23 = getelementptr inbounds [1 x i64], [1 x i64]* %8, i64 0, i64 0
%24 = call i64 @clEnqueueNDRangeKernel(i32 %20, i32 %21, i32 1, i32* null, i64* %22, i64* %23, i32 0, i32* null, i32* %9)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = load i64, i64* @CL_SUCCESS
%27 = icmp eq i64 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %4
%31 = call i64 @clFinish(i32 %30)
store i64 %31, i64* %10
%32 = load i64, i64* %10
%33 = load i64, i64* @CL_SUCCESS
%34 = icmp eq i64 %32, %33
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
store float 0.000000e+00, float* %11
%37 = load i32, i32* %9
%38 = load i32, i32* @CL_PROFILING_COMMAND_START
%39 = call i64 @clGetEventProfilingInfo(i32 %37, i32 %38, i32 4, float* %11, i32* null)
store i64 %39, i64* %10
%40 = load i64, i64* %10
%41 = load i64, i64* @CL_SUCCESS
%42 = icmp eq i64 %40, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
store float 0.000000e+00, float* %12
%45 = load i32, i32* %9
%46 = load i32, i32* @CL_PROFILING_COMMAND_END
%47 = call i64 @clGetEventProfilingInfo(i32 %45, i32 %46, i32 4, float* %12, i32* null)
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = load i64, i64* @CL_SUCCESS
%50 = icmp eq i64 %48, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i32, i32* @stderr
%54 = load float, float* %12
%55 = load float, float* %11
%56 = fsub float %54, %55
%57 = fmul float 1.000000e+00, %56
%58 = fpext float %57 to double
%59 = fdiv double %58, 1.000000e+03
%60 = fptrunc double %59 to float
%61 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), float %60)
ret i32 0
}

LLVM IR (-O2):
@CL_SUCCESS = global i64 0
@CL_PROFILING_COMMAND_START = global i32 0
@CL_PROFILING_COMMAND_END = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"\09Kernel exec time: %8.2f us\0A\00"
define i32 @RunKernel(i32 %0, i32 %1, i32 %2) {
%4 = alloca [1 x i64]
%5 = alloca [1 x i64]
%6 = alloca i32
%7 = alloca float
%8 = alloca float
%9 = bitcast [1 x i64]* %4 to i8*
%10 = getelementptr inbounds [1 x i64], [1 x i64]* %4, i64 0, i64 0
%11 = add nsw i32 %2, 255
%12 = sdiv i32 %11, 256
%13 = shl nsw i32 %12, 8
%14 = sext i32 %13 to i64
store i64 %14, i64* %10
%15 = bitcast [1 x i64]* %5 to i8*
%16 = getelementptr inbounds [1 x i64], [1 x i64]* %5, i64 0, i64 0
store i64 256, i64* %16
%17 = bitcast i32* %6 to i8*
%18 = call i64 @clEnqueueNDRangeKernel(i32 %0, i32 %1, i32 1, i32* null, i64* %10, i64* %16, i32 0, i32* null, i32* %6)
%19 = load i64, i64* @CL_SUCCESS
%20 = icmp eq i64 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = call i64 @clFinish(i32 %0)
%24 = load i64, i64* @CL_SUCCESS
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = bitcast float* %7 to i8*
store float 0.000000e+00, float* %7
%29 = load i32, i32* %6
%30 = load i32, i32* @CL_PROFILING_COMMAND_START
%31 = call i64 @clGetEventProfilingInfo(i32 %29, i32 %30, i32 4, float* %7, i32* null)
%32 = load i64, i64* @CL_SUCCESS
%33 = icmp eq i64 %31, %32
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = bitcast float* %8 to i8*
store float 0.000000e+00, float* %8
%37 = load i32, i32* %6
%38 = load i32, i32* @CL_PROFILING_COMMAND_END
%39 = call i64 @clGetEventProfilingInfo(i32 %37, i32 %38, i32 4, float* %8, i32* null)
%40 = load i64, i64* @CL_SUCCESS
%41 = icmp eq i64 %39, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32, i32* @stderr
%45 = load float, float* %8
%46 = load float, float* %7
%47 = fsub float %45, %46
%48 = fdiv float %47, 1.000000e+03
%49 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), float %48)
ret i32 0
}