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

/* Variables and functions */
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ CL_TRUE;
scalar_t__ clEnqueueReadBuffer(int /*<<< orphan*/, int /*<<< orphan*/,
                               int /*<<< orphan*/, int /*<<< orphan*/, int,
                               double *, int /*<<< orphan*/,
                               int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ clFinish(int /*<<< orphan*/);
int /*<<< orphan*/ free(double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void printCL(cl_mem *cl_u, cl_command_queue *command_queue, int Nx, int Ny) {
  double *u;
  int i = 0;
  int j = 0;
  cl_int ret = 0;
  u = (double *)malloc(Nx * Ny * sizeof(double));
  ret = clEnqueueReadBuffer(*command_queue, *cl_u, CL_TRUE, 0,
                            Nx * Ny * sizeof(double), u, 0, NULL, NULL);
  ret = clFinish(*command_queue);
  if (ret != CL_SUCCESS) {
    printf("failed");
  }
  for (i = 0; i < Nx; i++) {
    for (j = 0; j < Ny; j++) {
      printf("%f ", u[i + Nx * j]);
    }
    printf("\n");
  }
  printf("\n");
  free(u);
}


x86 assembly (clang, -O0):
<printCL>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x3,%rax
mov    %eax,%r8d
mov    -0x20(%rbp),%r9
xor    %ecx,%ecx
xor    %eax,%eax
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
call   1070 <clEnqueueReadBuffer@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <clFinish@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
je     1214 <printCL+0xc4>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    128a <printCL+0x13a>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    126e <printCL+0x11e>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%edx
imul   -0x28(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xdae(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    122e <printCL+0xde>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    121b <printCL+0xcb>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printCL>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%r12
mov    %edx,%ebp
mov    %ecx,0x1c(%rsp)
imul   %ecx,%ebp
shl    $0x3,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r9
mov    0x0(%r13),%edi
mov    (%r12),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movl   $0x0,(%rsp)
xor    %ecx,%ecx
mov    %ebp,%r8d
mov    %r9,0x20(%rsp)
call   1080 <clEnqueueReadBuffer@plt>
mov    0x0(%r13),%edi
call   1050 <clFinish@plt>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
je     11df <printCL+0x7f>
lea    0xe28(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r15d,%r15d
jle    125a <printCL+0xfa>
mov    %r15d,%eax
mov    0x1c(%rsp),%ecx
mov    %rcx,0x28(%rsp)
mov    %rax,0x30(%rsp)
lea    0x0(,%rax,8),%rbx
xor    %r13d,%r13d
lea    0xe00(%rip),%rbp
mov    0x20(%rsp),%r12
jmp    1229 <printCL+0xc9>
xchg   %ax,%ax
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%r13
add    $0x8,%r12
cmp    0x30(%rsp),%r13
je     125a <printCL+0xfa>
cmpl   $0x0,0x1c(%rsp)
jle    1210 <printCL+0xb0>
mov    0x28(%rsp),%r15
mov    %r12,%r14
nopl   0x0(%rax,%rax,1)
movsd  (%r14),%xmm0
mov    %rbp,%rdi
mov    $0x1,%al
call   1060 <printf@plt>
add    %rbx,%r14
add    $0xffffffffffffffff,%r15
jne    1240 <printCL+0xe0>
jmp    1210 <printCL+0xb0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x20(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@CL_TRUE = global i32 0
@CL_SUCCESS = global i64 0
@.str = [7 x i8] c"failed\00"
@.str.1 = [4 x i8] c"%f \00"
@.str.2 = [2 x i8] c"\0A\00"
define void @printCL(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
store i64 0, i64* %12
%13 = load i32, i32* %7
%14 = load i32, i32* %8
%15 = mul nsw i32 %13, %14
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 8
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to double*
store double* %20, double** %9
%21 = load i32*, i32** %6
%22 = load i32, i32* %21
%23 = load i32*, i32** %5
%24 = load i32, i32* %23
%25 = load i32, i32* @CL_TRUE
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = mul nsw i32 %26, %27
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 8
%31 = trunc i64 %30 to i32
%32 = load double*, double** %9
%33 = call i64 @clEnqueueReadBuffer(i32 %22, i32 %24, i32 %25, i32 0, i32 %31, double* %32, i32 0, i32* null, i32* null)
store i64 %33, i64* %12
%34 = load i32*, i32** %6
%35 = load i32, i32* %34
%36 = call i64 @clFinish(i32 %35)
store i64 %36, i64* %12
%37 = load i64, i64* %12
%38 = load i64, i64* @CL_SUCCESS
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %4
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %4
store i32 0, i32* %10
br label %43
43: ; preds = %68, %42
%44 = load i32, i32* %10
%45 = load i32, i32* %7
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %71
47: ; preds = %43
store i32 0, i32* %11
br label %48
48: ; preds = %63, %47
%49 = load i32, i32* %11
%50 = load i32, i32* %8
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %66
52: ; preds = %48
%53 = load double*, double** %9
%54 = load i32, i32* %10
%55 = load i32, i32* %7
%56 = load i32, i32* %11
%57 = mul nsw i32 %55, %56
%58 = add nsw i32 %54, %57
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double, double* %53, i64 %59
%61 = load double, double* %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %61)
br label %63
63: ; preds = %52
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %48
66: ; preds = %48
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %68
68: ; preds = %66
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %43
71: ; preds = %43
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%73 = load double*, double** %9
%74 = call i32 @free(double* %73)
ret void
}

LLVM IR (-O2):
@CL_TRUE = global i32 0
@CL_SUCCESS = global i64 0
@.str = [7 x i8] c"failed\00"
@.str.1 = [4 x i8] c"%f \00"
define void @printCL(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = shl i32 %2, 3
%6 = mul i32 %5, %3
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to double*
%9 = load i32, i32* %1
%10 = load i32, i32* %0
%11 = load i32, i32* @CL_TRUE
%12 = tail call i64 @clEnqueueReadBuffer(i32 %9, i32 %10, i32 %11, i32 0, i32 %6, double* %8, i32 0, i32* null, i32* null)
%13 = load i32, i32* %1
%14 = tail call i64 @clFinish(i32 %13)
%15 = load i64, i64* @CL_SUCCESS
%16 = icmp eq i64 %14, %15
br i1 %16, label %19, label %17
17: ; preds = %4
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %4
%20 = icmp sgt i32 %2, 0
br i1 %20, label %21, label %41
21: ; preds = %19
%22 = icmp sgt i32 %3, 0
%23 = zext i32 %2 to i64
%24 = zext i32 %2 to i64
%25 = zext i32 %3 to i64
br label %26
26: ; preds = %21, %37
%27 = phi i64 [ 0, %21 ], [ %39, %37 ]
br i1 %22, label %28, label %37
28: ; preds = %26, %28
%29 = phi i64 [ %35, %28 ], [ 0, %26 ]
%30 = mul nsw i64 %29, %23
%31 = add nuw nsw i64 %30, %27
%32 = getelementptr inbounds double, double* %8, i64 %31
%33 = load double, double* %32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), double %33)
%35 = add nuw nsw i64 %29, 1
%36 = icmp eq i64 %35, %25
br i1 %36, label %37, label %28
37: ; preds = %28, %26
%38 = tail call i32 @putchar(i32 10)
%39 = add nuw nsw i64 %27, 1
%40 = icmp eq i64 %39, %24
br i1 %40, label %41, label %26
41: ; preds = %37, %19
%42 = tail call i32 @putchar(i32 10)
%43 = tail call i32 @free(double* %8)
ret void
}