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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_ADDRESS_BITS;
scalar_t__ CL_SUCCESS;
uintptr_t _clQueue_create_32(int /*<<< orphan*/, int /*<<< orphan*/);
uintptr_t _clQueue_create_64(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ clCreateKernel(int /*<<< orphan*/, char *, scalar_t__ *);
scalar_t__ clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  uintptr_t, size_t const *, uintptr_t,
                                  int /*<<< orphan*/, uintptr_t, uintptr_t);
int /*<<< orphan*/ clFinish(int /*<<< orphan*/);
scalar_t__ clGetDeviceInfo(int /*<<< orphan*/, int /*<<< orphan*/, int, int *,
                           uintptr_t);
int /*<<< orphan*/ clReleaseKernel(int /*<<< orphan*/);
int /*<<< orphan*/ clSetKernelArg(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  uintptr_t *);
int /*<<< orphan*/ printf(char *, ...);
cl_mem clQueue_create(cl_device_id dev, cl_context ctx, cl_command_queue cq,
                      cl_program prg) {
  cl_int error;
  cl_uint bits;
  cl_mem q;
  cl_kernel kernel;
  const size_t threads = 1;
  error = clGetDeviceInfo(dev, CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &bits,
                          (uintptr_t)0);
  if (error) {
    printf("clQueue: could not discover device address space\n");
    return (uintptr_t)0;
  }
  if (bits == 32) {
    q = _clQueue_create_32(ctx, cq);
  } else {
    q = _clQueue_create_64(ctx, cq);
  }
  if (!q)
    return (uintptr_t)0;
  kernel = clCreateKernel(prg, "clQueue_init", &error);
  if (error != CL_SUCCESS) {
    printf("clQueue: Could not create queue init kernel: %i\n", error);
    return (uintptr_t)0;
  }
  clSetKernelArg(kernel, 0, sizeof(cl_mem), &q);
  error = clEnqueueNDRangeKernel(cq, kernel, 1, (uintptr_t)0, &threads,
                                 (uintptr_t)0, 0, (uintptr_t)0, (uintptr_t)0);
  if (error != CL_SUCCESS) {
    printf("clQueue: Could not execute queue init kernel: %i\n", error);
    return (uintptr_t)0;
  }
  clFinish(cq);
  clReleaseKernel(kernel);
  return q;
}


x86 assembly (clang, -O0):
<clQueue_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x1,-0x40(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x24(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetDeviceInfo@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f5 <clQueue_create+0x65>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    131c <clQueue_create+0x18c>
cmpl   $0x20,-0x24(%rbp)
jne    1213 <clQueue_create+0x83>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <_clQueue_create_32@plt>
mov    %rax,-0x30(%rbp)
jmp    1222 <clQueue_create+0x92>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1090 <_clQueue_create_64@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123a <clQueue_create+0xaa>
movq   $0x0,-0x8(%rbp)
jmp    131c <clQueue_create+0x18c>
mov    -0x18(%rbp),%edi
lea    0xdee(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <clCreateKernel@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
je     1283 <clQueue_create+0xf3>
mov    -0x20(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    131c <clQueue_create+0x18c>
mov    -0x34(%rbp),%edi
xor    %esi,%esi
mov    $0x8,%edx
lea    -0x30(%rbp),%rcx
call   10b0 <clSetKernelArg@plt>
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x40(%rbp),%r8
xor    %eax,%eax
mov    %r9,%rcx
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
call   1060 <clEnqueueNDRangeKernel@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2cfc(%rip),%rcx
cmp    (%rcx),%rax
je     1304 <clQueue_create+0x174>
mov    -0x20(%rbp),%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    131c <clQueue_create+0x18c>
mov    -0x14(%rbp),%edi
call   1030 <clFinish@plt>
mov    -0x34(%rbp),%edi
call   1040 <clReleaseKernel@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clQueue_create>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %esi,%ebx
movq   $0x1,0x38(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    0x2c(%rsp),%rcx
mov    $0x4,%edx
xor    %r8d,%r8d
call   1060 <clGetDeviceInfo@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     11f0 <clQueue_create+0x50>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    12be <clQueue_create+0x11e>
cmpl   $0x20,0x2c(%rsp)
jne    1203 <clQueue_create+0x63>
mov    %ebx,%edi
mov    %r14d,%esi
call   1090 <_clQueue_create_32@plt>
jmp    120d <clQueue_create+0x6d>
mov    %ebx,%edi
mov    %r14d,%esi
call   10a0 <_clQueue_create_64@plt>
mov    %rax,0x30(%rsp)
test   %rax,%rax
je     129c <clQueue_create+0xfc>
lea    0xdde(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %ebp,%edi
call   10b0 <clCreateKernel@plt>
mov    0x20(%rsp),%rsi
mov    0x2d9e(%rip),%rbx
cmp    (%rbx),%rsi
jne    12a0 <clQueue_create+0x100>
mov    %eax,%ebp
lea    0x30(%rsp),%rcx
mov    %eax,%edi
xor    %esi,%esi
mov    $0x8,%edx
call   10c0 <clSetKernelArg@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movl   $0x0,(%rsp)
lea    0x38(%rsp),%r8
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1070 <clEnqueueNDRangeKernel@plt>
mov    %rax,0x20(%rsp)
cmp    (%rbx),%rax
jne    12ab <clQueue_create+0x10b>
mov    %r14d,%edi
call   1030 <clFinish@plt>
mov    %ebp,%edi
call   1050 <clReleaseKernel@plt>
mov    0x30(%rsp),%rbx
jmp    12be <clQueue_create+0x11e>
xor    %ebx,%ebx
jmp    12be <clQueue_create+0x11e>
lea    0xd66(%rip),%rdi
xor    %ebx,%ebx
jmp    12b7 <clQueue_create+0x117>
lea    0xd8c(%rip),%rdi
xor    %ebx,%ebx
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@CL_DEVICE_ADDRESS_BITS = global i32 0
@.str = [50 x i8] c"clQueue: could not discover device address space\0A\00"
@.str.1 = [13 x i8] c"clQueue_init\00"
@CL_SUCCESS = global i64 0
@.str.2 = [49 x i8] c"clQueue: Could not create queue init kernel: %i\0A\00"
@.str.3 = [50 x i8] c"clQueue: Could not execute queue init kernel: %i\0A\00"
define i64 @clQueue_create(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 1, i64* %14
%15 = load i32, i32* %6
%16 = load i32, i32* @CL_DEVICE_ADDRESS_BITS
%17 = call i64 @clGetDeviceInfo(i32 %15, i32 %16, i32 4, i32* %11, i64 0)
store i64 %17, i64* %10
%18 = load i64, i64* %10
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %5
br label %64
22: ; preds = %4
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, 32
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = call i64 @_clQueue_create_32(i32 %26, i32 %27)
store i64 %28, i64* %12
br label %33
29: ; preds = %22
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = call i64 @_clQueue_create_64(i32 %30, i32 %31)
store i64 %32, i64* %12
br label %33
33: ; preds = %29, %25
%34 = load i64, i64* %12
%35 = icmp ne i64 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %33
store i64 0, i64* %5
br label %64
37: ; preds = %33
%38 = load i32, i32* %9
%39 = call i32 @clCreateKernel(i32 %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64* %10)
store i32 %39, i32* %13
%40 = load i64, i64* %10
%41 = load i64, i64* @CL_SUCCESS
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = load i64, i64* %10
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %44)
store i64 0, i64* %5
br label %64
46: ; preds = %37
%47 = load i32, i32* %13
%48 = call i32 @clSetKernelArg(i32 %47, i32 0, i32 8, i64* %12)
%49 = load i32, i32* %8
%50 = load i32, i32* %13
%51 = call i64 @clEnqueueNDRangeKernel(i32 %49, i32 %50, i32 1, i64 0, i64* %14, i64 0, i32 0, i64 0, i64 0)
store i64 %51, i64* %10
%52 = load i64, i64* %10
%53 = load i64, i64* @CL_SUCCESS
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %46
%56 = load i64, i64* %10
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i64 %56)
store i64 0, i64* %5
br label %64
58: ; preds = %46
%59 = load i32, i32* %8
%60 = call i32 @clFinish(i32 %59)
%61 = load i32, i32* %13
%62 = call i32 @clReleaseKernel(i32 %61)
%63 = load i64, i64* %12
store i64 %63, i64* %5
br label %64
64: ; preds = %58, %55, %43, %36, %20
%65 = load i64, i64* %5
ret i64 %65
}

LLVM IR (-O2):
@CL_DEVICE_ADDRESS_BITS = global i32 0
@.str.1 = [13 x i8] c"clQueue_init\00"
@CL_SUCCESS = global i64 0
@.str.2 = [49 x i8] c"clQueue: Could not create queue init kernel: %i\0A\00"
@.str.3 = [50 x i8] c"clQueue: Could not execute queue init kernel: %i\0A\00"
@str = [49 x i8] c"clQueue: could not discover device address space\00"
define i64 @clQueue_create(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = bitcast i64* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i64* %7 to i8*
%12 = bitcast i64* %8 to i8*
store i64 1, i64* %8
%13 = load i32, i32* @CL_DEVICE_ADDRESS_BITS
%14 = call i64 @clGetDeviceInfo(i32 %0, i32 %13, i32 4, i32* %6, i64 0)
store i64 %14, i64* %5
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %4
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
br label %46
18: ; preds = %4
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 32
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i64 @_clQueue_create_32(i32 %1, i32 %2)
br label %25
23: ; preds = %18
%24 = call i64 @_clQueue_create_64(i32 %1, i32 %2)
br label %25
25: ; preds = %23, %21
%26 = phi i64 [ %24, %23 ], [ %22, %21 ]
store i64 %26, i64* %7
%27 = icmp eq i64 %26, 0
br i1 %27, label %46, label %28
28: ; preds = %25
%29 = call i32 @clCreateKernel(i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i64* %5)
%30 = load i64, i64* %5
%31 = load i64, i64* @CL_SUCCESS
%32 = icmp eq i64 %30, %31
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %30)
br label %46
35: ; preds = %28
%36 = call i32 @clSetKernelArg(i32 %29, i32 0, i32 8, i64* %7)
%37 = call i64 @clEnqueueNDRangeKernel(i32 %2, i32 %29, i32 1, i64 0, i64* %8, i64 0, i32 0, i64 0, i64 0)
store i64 %37, i64* %5
%38 = load i64, i64* @CL_SUCCESS
%39 = icmp eq i64 %37, %38
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i64 %37)
br label %46
42: ; preds = %35
%43 = call i32 @clFinish(i32 %2)
%44 = call i32 @clReleaseKernel(i32 %29)
%45 = load i64, i64* %7
br label %46
46: ; preds = %25, %42, %40, %33, %16
%47 = phi i64 [ 0, %16 ], [ 0, %33 ], [ 0, %40 ], [ %45, %42 ], [ 0, %25 ]
ret i64 %47
}