#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 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;
int _kma_create_32_svm(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int,
                       void **);
int _kma_create_64_svm(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int,
                       void **);
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 clSetKernelArgSVMPointer(int /*<<< orphan*/, int /*<<< orphan*/, void *);
int /*<<< orphan*/ printf(char *, ...);
int kma_create_svm(cl_device_id dev, cl_context ctx, cl_command_queue cq,
                   cl_program prg, unsigned int sblocks, void **host_ptr) {
  cl_int error;
  cl_uint bits;
  cl_kernel kernel;
  const size_t threads = 1;
  if (sblocks == 0)
    return 1;
  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 1;
  }
  if (bits == 32) {
    error = _kma_create_32_svm(ctx, cq, sblocks, host_ptr);
  } else {
    error = _kma_create_64_svm(ctx, cq, sblocks, host_ptr);
  }
  if (error) {
    printf("KMA: No heap!\n");
    return 1;
  }
  kernel = clCreateKernel(prg, "clheap_init", &error);
  if (error != CL_SUCCESS) {
    printf("KMA: Could not create heap init kernel: %i\n", error);
    return 1;
  }
  error = clSetKernelArgSVMPointer(kernel, 0, *host_ptr);
  if (error != CL_SUCCESS) {
    printf("KMA: Wrong argument: %i\n", error);
    return 1;
  }
  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 1;
  }
  clReleaseKernel(kernel);
  return CL_SUCCESS;
}


x86 assembly (clang, -O0):
<kma_create_svm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
movq   $0x1,-0x38(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ca <kma_create_svm+0x3a>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
mov    -0x8(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetDeviceInfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1210 <kma_create_svm+0x80>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
cmpl   $0x20,-0x28(%rbp)
jne    1234 <kma_create_svm+0xa4>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1090 <_kma_create_32_svm@plt>
mov    %eax,-0x24(%rbp)
jmp    1249 <kma_create_svm+0xb9>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1080 <_kma_create_64_svm@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     126d <kma_create_svm+0xdd>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
mov    -0x14(%rbp),%edi
lea    0xdc6(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   10a0 <clCreateKernel@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%eax
je     12b2 <kma_create_svm+0x122>
mov    -0x24(%rbp),%esi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
xor    %esi,%esi
call   10b0 <clSetKernelArgSVMPointer@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%eax
je     12f5 <kma_create_svm+0x165>
mov    -0x24(%rbp),%esi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
mov    -0x10(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x38(%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,-0x24(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <clFinish@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2c91(%rip),%rcx
cmp    (%rcx),%eax
je     136c <kma_create_svm+0x1dc>
mov    -0x24(%rbp),%esi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1380 <kma_create_svm+0x1f0>
mov    -0x2c(%rbp),%edi
call   1040 <clReleaseKernel@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kma_create_svm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x1,0x20(%rsp)
mov    $0x1,%r15d
test   %r8d,%r8d
je     12f6 <kma_create_svm+0x156>
mov    %r9,%r13
mov    %r8d,%ebx
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %esi,%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0x1c(%rsp),%rcx
mov    $0x4,%edx
xor    %r8d,%r8d
call   1060 <clGetDeviceInfo@plt>
test   %eax,%eax
je     1204 <kma_create_svm+0x64>
lea    0xe92(%rip),%rdi
call   1040 <puts@plt>
jmp    12f6 <kma_create_svm+0x156>
cmpl   $0x20,0x1c(%rsp)
jne    121c <kma_create_svm+0x7c>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   10a0 <_kma_create_32_svm@plt>
jmp    122b <kma_create_svm+0x8b>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   1090 <_kma_create_64_svm@plt>
mov    %eax,0x18(%rsp)
test   %eax,%eax
je     1244 <kma_create_svm+0xa4>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
jmp    12f6 <kma_create_svm+0x156>
lea    0xdb5(%rip),%rsi
lea    0x18(%rsp),%rdx
mov    %r12d,%edi
call   10b0 <clCreateKernel@plt>
mov    0x18(%rsp),%esi
mov    0x2d75(%rip),%rbp
cmp    0x0(%rbp),%esi
jne    12d4 <kma_create_svm+0x134>
mov    %eax,%ebx
mov    0x0(%r13),%rdx
mov    %eax,%edi
xor    %esi,%esi
call   10c0 <clSetKernelArgSVMPointer@plt>
mov    %eax,0x18(%rsp)
cmp    0x0(%rbp),%eax
jne    12dd <kma_create_svm+0x13d>
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movl   $0x0,(%rsp)
mov    $0x1,%r15d
lea    0x20(%rsp),%r8
mov    %r14d,%edi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1070 <clEnqueueNDRangeKernel@plt>
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1030 <clFinish@plt>
or     0x18(%rsp),%eax
mov    %eax,0x18(%rsp)
cmp    0x0(%rbp),%eax
jne    12e6 <kma_create_svm+0x146>
mov    %ebx,%edi
call   1050 <clReleaseKernel@plt>
mov    0x0(%rbp),%r15d
jmp    12f6 <kma_create_svm+0x156>
lea    0xd31(%rip),%rdi
jmp    12ef <kma_create_svm+0x14f>
lea    0xd54(%rip),%rdi
jmp    12ed <kma_create_svm+0x14d>
lea    0xd64(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [25 x i8] c"KMA: Wrong argument: %i\0A\00"
@.str.5 = [45 x i8] c"KMA: Could not execute heap init kernel: %i\0A\00"
define i32 @kma_create_svm(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i8** %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8**
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i64
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i8** %5, i8*** %13
store i64 1, i64* %17
%18 = load i32, i32* %12
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %6
store i32 1, i32* %7
br label %87
21: ; preds = %6
%22 = load i32, i32* %8
%23 = load i32, i32* @CL_DEVICE_ADDRESS_BITS
%24 = call i32 @clGetDeviceInfo(i32 %22, i32 %23, i32 4, i32* %15, i64 0)
store i32 %24, i32* %14
%25 = load i32, i32* %14
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %87
29: ; preds = %21
%30 = load i32, i32* %15
%31 = icmp eq i32 %30, 32
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* %9
%34 = load i32, i32* %10
%35 = load i32, i32* %12
%36 = load i8**, i8*** %13
%37 = call i32 @_kma_create_32_svm(i32 %33, i32 %34, i32 %35, i8** %36)
store i32 %37, i32* %14
br label %44
38: ; preds = %29
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = load i32, i32* %12
%42 = load i8**, i8*** %13
%43 = call i32 @_kma_create_64_svm(i32 %39, i32 %40, i32 %41, i8** %42)
store i32 %43, i32* %14
br label %44
44: ; preds = %38, %32
%45 = load i32, i32* %14
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %87
49: ; preds = %44
%50 = load i32, i32* %11
%51 = call i32 @clCreateKernel(i32 %50, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %14)
store i32 %51, i32* %16
%52 = load i32, i32* %14
%53 = load i32, i32* @CL_SUCCESS
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i32, i32* %14
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %56)
store i32 1, i32* %7
br label %87
58: ; preds = %49
%59 = load i32, i32* %16
%60 = load i8**, i8*** %13
%61 = load i8*, i8** %60
%62 = call i32 @clSetKernelArgSVMPointer(i32 %59, i32 0, i8* %61)
store i32 %62, i32* %14
%63 = load i32, i32* %14
%64 = load i32, i32* @CL_SUCCESS
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = load i32, i32* %14
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %67)
store i32 1, i32* %7
br label %87
69: ; preds = %58
%70 = load i32, i32* %10
%71 = load i32, i32* %16
%72 = call i32 @clEnqueueNDRangeKernel(i32 %70, i32 %71, i32 1, i64 0, i64* %17, i64 0, i32 0, i64 0, i64 0)
store i32 %72, i32* %14
%73 = load i32, i32* %10
%74 = call i32 @clFinish(i32 %73)
%75 = load i32, i32* %14
%76 = or i32 %75, %74
store i32 %76, i32* %14
%77 = load i32, i32* %14
%78 = load i32, i32* @CL_SUCCESS
%79 = icmp ne i32 %77, %78
br i1 %79, label %80, label %83
80: ; preds = %69
%81 = load i32, i32* %14
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %81)
store i32 1, i32* %7
br label %87
83: ; preds = %69
%84 = load i32, i32* %16
%85 = call i32 @clReleaseKernel(i32 %84)
%86 = load i32, i32* @CL_SUCCESS
store i32 %86, i32* %7
br label %87
87: ; preds = %83, %80, %66, %55, %47, %27, %20
%88 = load i32, i32* %7
ret i32 %88
}

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