#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 int 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;
int CL_SUCCESS;
uintptr_t _kma_create_32(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int);
uintptr_t _kma_create_64(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ clCreateKernel(int /*<<< orphan*/, char *, int *);
int clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                           uintptr_t, size_t const *, uintptr_t,
                           int /*<<< orphan*/, uintptr_t, uintptr_t);
int clFinish(int /*<<< orphan*/);
int 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 kma_create(cl_device_id dev, cl_context ctx, cl_command_queue cq,
                  cl_program prg, unsigned int sblocks) {
  cl_int error;
  cl_uint bits;
  cl_mem q;
  cl_kernel kernel;
  const size_t threads = 1;
  if (sblocks == 0)
    return (uintptr_t)0;
  error = clGetDeviceInfo(dev, CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &bits,
                          (uintptr_t)0);
  if (error) {
    printf("KMA: could not discover device address space\n");
    return (uintptr_t)0;
  }
  if (bits == 32) {
    q = _kma_create_32(ctx, cq, sblocks);
  } else {
    q = _kma_create_64(ctx, cq, sblocks);
  }
  if (!q) {
    printf("KMA: No heap!\n");
    return (uintptr_t)0;
  }
  kernel = clCreateKernel(prg, "clheap_init", &error);
  if (error != CL_SUCCESS) {
    printf("KMA: Could not create heap 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);
  error |= clFinish(cq);
  if (error != CL_SUCCESS) {
    printf("KMA: Could not execute heap init kernel: %i\n", error);
    return (uintptr_t)0;
  }
  clReleaseKernel(kernel);
  return q;
}


x86 assembly (clang, -O0):
<kma_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)
mov    %r8d,-0x1c(%rbp)
movq   $0x1,-0x40(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11c7 <kma_create+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1348 <kma_create+0x1b8>
mov    -0xc(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x24(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetDeviceInfo@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     120e <kma_create+0x7e>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <kma_create+0x1b8>
cmpl   $0x20,-0x24(%rbp)
jne    122f <kma_create+0x9f>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1080 <_kma_create_32@plt>
mov    %rax,-0x30(%rbp)
jmp    1241 <kma_create+0xb1>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1090 <_kma_create_64@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1267 <kma_create+0xd7>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <kma_create+0x1b8>
mov    -0x18(%rbp),%edi
lea    0xdcc(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <clCreateKernel@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d51(%rip),%rcx
cmp    (%rcx),%eax
je     12ad <kma_create+0x11d>
mov    -0x20(%rbp),%esi
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <kma_create+0x1b8>
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    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <clFinish@plt>
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2cc6(%rip),%rcx
cmp    (%rcx),%eax
je     1338 <kma_create+0x1a8>
mov    -0x20(%rbp),%esi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <kma_create+0x1b8>
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):
<kma_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
movq   $0x1,0x30(%rsp)
test   %r8d,%r8d
je     11f2 <kma_create+0x52>
mov    %r8d,%ebx
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebp
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0x24(%rsp),%rcx
mov    $0x4,%edx
xor    %r8d,%r8d
call   1060 <clGetDeviceInfo@plt>
mov    %eax,0x20(%rsp)
test   %eax,%eax
je     1202 <kma_create+0x62>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmpl   $0x20,0x24(%rsp)
jne    1217 <kma_create+0x77>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
call   1090 <_kma_create_32@plt>
jmp    1223 <kma_create+0x83>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
call   10a0 <_kma_create_64@plt>
mov    %rax,0x28(%rsp)
test   %rax,%rax
je     12ba <kma_create+0x11a>
lea    0xdc8(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %r15d,%edi
call   10b0 <clCreateKernel@plt>
mov    0x20(%rsp),%esi
mov    0x2d88(%rip),%rbx
cmp    (%rbx),%esi
jne    12c6 <kma_create+0x126>
mov    %eax,%ebp
lea    0x28(%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    0x30(%rsp),%r8
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1070 <clEnqueueNDRangeKernel@plt>
mov    %eax,0x20(%rsp)
mov    %r14d,%edi
call   1030 <clFinish@plt>
or     0x20(%rsp),%eax
mov    %eax,0x20(%rsp)
cmp    (%rbx),%eax
jne    12d1 <kma_create+0x131>
mov    %ebp,%edi
call   1050 <clReleaseKernel@plt>
mov    0x28(%rsp),%rbx
jmp    11f4 <kma_create+0x54>
lea    0xda4(%rip),%rdi
jmp    11ed <kma_create+0x4d>
lea    0xd3f(%rip),%rdi
xor    %ebx,%ebx
jmp    12dc <kma_create+0x13c>
lea    0xd60(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    11f4 <kma_create+0x54>

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

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