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

/* Variables and functions */
int CL_MEM_COPY_HOST_PTR;
int CL_MEM_READ_WRITE;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ CL_TRUE;
int /*<<< orphan*/ clCreateBuffer(int /*<<< orphan*/, int, int, float *,
                                  scalar_t__ *);
scalar_t__ clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, size_t *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ clEnqueueReadBuffer(int /*<<< orphan*/, int /*<<< orphan*/,
                               int /*<<< orphan*/, int /*<<< orphan*/, int,
                               void *, int, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ clReleaseEvent(int /*<<< orphan*/);
scalar_t__ clSetKernelArg(int /*<<< orphan*/, int /*<<< orphan*/, int, void *);
cl_int square_array(cl_context ctx, cl_command_queue queue, cl_kernel kern,
                    float *a_h, int N) {
  cl_int errcode;
  cl_mem a_d = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
                              sizeof(*a_h) * N, a_h, &errcode);
  if (errcode != CL_SUCCESS)
    return errcode;
  errcode = clSetKernelArg(kern, 0, sizeof(cl_mem), (void *)&a_d);
  if (errcode != CL_SUCCESS)
    return errcode;
  size_t global_work_size = N;
  cl_event event;
  errcode = clEnqueueNDRangeKernel(queue, kern, 1, NULL, &global_work_size,
                                   NULL, 0, NULL, &event);
  if (errcode != CL_SUCCESS)
    return errcode;
  errcode = clEnqueueReadBuffer(queue, a_d, CL_TRUE, 0, sizeof(*a_h) * N,
                                (void *)a_h, 1, &event, NULL);
  clReleaseEvent(event);
  return errcode;
}


x86 assembly (clang, -O0):
<square_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1030 <clCreateBuffer@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11b9 <square_array+0x69>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <square_array+0x15a>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rcx
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <clSetKernelArg@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
je     11f1 <square_array+0xa1>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <square_array+0x15a>
movslq -0x24(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x40(%rbp),%r8
xor    %eax,%eax
lea    -0x44(%rbp),%rax
mov    %r9,%rcx
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <clEnqueueNDRangeKernel@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
je     1255 <square_array+0x105>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <square_array+0x15a>
mov    -0x10(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%r8d
mov    -0x20(%rbp),%r9
xor    %ecx,%ecx
lea    -0x44(%rbp),%rax
xor    %r10d,%r10d
movl   $0x1,(%rsp)
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
call   1050 <clEnqueueReadBuffer@plt>
mov    %rax,-0x30(%rbp)
mov    -0x44(%rbp),%edi
call   1060 <clReleaseEvent@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<square_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %rcx,%r15
mov    %edx,%ebx
mov    %esi,%r14d
mov    0x2e70(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x0(,%rbp,4),%r12d
mov    %rsp,%r8
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <clCreateBuffer@plt>
mov    %eax,0x8(%rsp)
mov    (%rsp),%rax
mov    0x2e28(%rip),%r13
cmp    0x0(%r13),%rax
jne    123f <square_array+0xef>
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <clSetKernelArg@plt>
mov    %rax,(%rsp)
cmp    0x0(%r13),%rax
jne    123f <square_array+0xef>
movslq %ebp,%rax
mov    %rax,0x10(%rsp)
sub    $0x8,%rsp
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r8
mov    %r14d,%edi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
push   %rbp
push   $0x0
push   $0x0
call   1040 <clEnqueueNDRangeKernel@plt>
add    $0x20,%rsp
mov    %rax,(%rsp)
cmp    0x0(%r13),%rax
jne    123f <square_array+0xef>
mov    0x8(%rsp),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
sub    $0x8,%rsp
mov    %r14d,%edi
xor    %ecx,%ecx
mov    %r12d,%r8d
mov    %r15,%r9
push   $0x0
push   %rbp
push   $0x1
call   1050 <clEnqueueReadBuffer@plt>
add    $0x20,%rsp
mov    %rax,(%rsp)
mov    0xc(%rsp),%edi
call   1060 <clReleaseEvent@plt>
mov    (%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CL_MEM_READ_WRITE = global i32 0
@CL_MEM_COPY_HOST_PTR = global i32 0
@CL_SUCCESS = global i64 0
@CL_TRUE = global i32 0
define i64 @square_array(i32 %0, i32 %1, i32 %2, float* %3, i32 %4) {
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca float*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store float* %3, float** %10
store i32 %4, i32* %11
%16 = load i32, i32* %7
%17 = load i32, i32* @CL_MEM_READ_WRITE
%18 = load i32, i32* @CL_MEM_COPY_HOST_PTR
%19 = or i32 %17, %18
%20 = load i32, i32* %11
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = trunc i64 %22 to i32
%24 = load float*, float** %10
%25 = call i32 @clCreateBuffer(i32 %16, i32 %19, i32 %23, float* %24, i64* %12)
store i32 %25, i32* %13
%26 = load i64, i64* %12
%27 = load i64, i64* @CL_SUCCESS
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %5
%30 = load i64, i64* %12
store i64 %30, i64* %6
br label %65
31: ; preds = %5
%32 = load i32, i32* %9
%33 = bitcast i32* %13 to i8*
%34 = call i64 @clSetKernelArg(i32 %32, i32 0, i32 4, i8* %33)
store i64 %34, i64* %12
%35 = load i64, i64* %12
%36 = load i64, i64* @CL_SUCCESS
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = load i64, i64* %12
store i64 %39, i64* %6
br label %65
40: ; preds = %31
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
store i64 %42, i64* %14
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = call i64 @clEnqueueNDRangeKernel(i32 %43, i32 %44, i32 1, i32* null, i64* %14, i32* null, i32 0, i32* null, i32* %15)
store i64 %45, i64* %12
%46 = load i64, i64* %12
%47 = load i64, i64* @CL_SUCCESS
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = load i64, i64* %12
store i64 %50, i64* %6
br label %65
51: ; preds = %40
%52 = load i32, i32* %8
%53 = load i32, i32* %13
%54 = load i32, i32* @CL_TRUE
%55 = load i32, i32* %11
%56 = sext i32 %55 to i64
%57 = mul i64 4, %56
%58 = trunc i64 %57 to i32
%59 = load float*, float** %10
%60 = bitcast float* %59 to i8*
%61 = call i64 @clEnqueueReadBuffer(i32 %52, i32 %53, i32 %54, i32 0, i32 %58, i8* %60, i32 1, i32* %15, i32* null)
store i64 %61, i64* %12
%62 = load i32, i32* %15
%63 = call i32 @clReleaseEvent(i32 %62)
%64 = load i64, i64* %12
store i64 %64, i64* %6
br label %65
65: ; preds = %51, %49, %38, %29
%66 = load i64, i64* %6
ret i64 %66
}

LLVM IR (-O2):
@CL_MEM_READ_WRITE = global i32 0
@CL_MEM_COPY_HOST_PTR = global i32 0
@CL_SUCCESS = global i64 0
@CL_TRUE = global i32 0
define i64 @square_array(i32 %0, i32 %1, i32 %2, float* %3, i32 %4) {
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = bitcast i64* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = load i32, i32* @CL_MEM_READ_WRITE
%13 = load i32, i32* @CL_MEM_COPY_HOST_PTR
%14 = or i32 %13, %12
%15 = sext i32 %4 to i64
%16 = shl i32 %4, 2
%17 = call i32 @clCreateBuffer(i32 %0, i32 %14, i32 %16, float* %3, i64* %6)
store i32 %17, i32* %7
%18 = load i64, i64* %6
%19 = load i64, i64* @CL_SUCCESS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %41
21: ; preds = %5
%22 = call i64 @clSetKernelArg(i32 %2, i32 0, i32 4, i8* %11)
store i64 %22, i64* %6
%23 = load i64, i64* @CL_SUCCESS
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = bitcast i64* %8 to i8*
store i64 %15, i64* %8
%27 = bitcast i32* %9 to i8*
%28 = call i64 @clEnqueueNDRangeKernel(i32 %1, i32 %2, i32 1, i32* null, i64* %8, i32* null, i32 0, i32* null, i32* %9)
store i64 %28, i64* %6
%29 = load i64, i64* @CL_SUCCESS
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %25
%32 = load i32, i32* %7
%33 = load i32, i32* @CL_TRUE
%34 = bitcast float* %3 to i8*
%35 = call i64 @clEnqueueReadBuffer(i32 %1, i32 %32, i32 %33, i32 0, i32 %16, i8* %34, i32 1, i32* %9, i32* null)
store i64 %35, i64* %6
%36 = load i32, i32* %9
%37 = call i32 @clReleaseEvent(i32 %36)
%38 = load i64, i64* %6
br label %39
39: ; preds = %25, %31
%40 = phi i64 [ %38, %31 ], [ %28, %25 ]
br label %41
41: ; preds = %21, %5, %39
%42 = phi i64 [ %40, %39 ], [ %18, %5 ], [ %22, %21 ]
ret i64 %42
}