#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*/ uint64_t;
typedef int /*<<< orphan*/ cl_uint;
typedef int /*<<< orphan*/ *cl_program;
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 CL_MEM_READ_WRITE;
int CL_MEM_SVM_ATOMICS;
int CL_MEM_SVM_FINE_GRAIN_BUFFER;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ assert(int);
scalar_t__ clCreateKernels(int /*<<< orphan*/ *, int /*<<< orphan*/ **);
scalar_t__ clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, size_t *, size_t *,
                                  int /*<<< orphan*/, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ clFinish(int /*<<< orphan*/);
int /*<<< orphan*/ *clLoadProgram(int /*<<< orphan*/, int /*<<< orphan*/,
                                  char *);
int /*<<< orphan*/ clReleaseKernels(int /*<<< orphan*/ *);
int /*<<< orphan*/ clReleaseProgram(int /*<<< orphan*/ *);
void *clSVMAlloc(int /*<<< orphan*/, int, int /*<<< orphan*/,
                 int /*<<< orphan*/);
scalar_t__ clSetKernelArg(int /*<<< orphan*/, int, int, void *);
scalar_t__ clSetKernelArgSVMPointer(int /*<<< orphan*/, int /*<<< orphan*/,
                                    void *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int kma_create_svm(cl_device_id device, cl_context context,
                   cl_command_queue queue, uint64_t size, void **clheap) {
  cl_program program;
  cl_kernel *kernels;
  size_t cb;
  cl_int err;
  cl_uint num;
  size_t global_work_size[1] = {1};
  size_t local_work_size[1] = {1};
  program = clLoadProgram(context, device, "kma.cl");
  if (program == NULL) {
    perror("Error, can't load or build program\n");
    exit(1);
  }
  err = clCreateKernels(program, &kernels);
  if (err) {
    return 1;
  }
  void *heap;
  {
    heap = clSVMAlloc(context,
                      CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER |
                          CL_MEM_SVM_ATOMICS,
                      size, 0);
    assert(heap != NULL);
    err = clSetKernelArgSVMPointer(kernels[0], 0, heap);
    assert(err == CL_SUCCESS);
    err = clSetKernelArg(kernels[0], 1, sizeof(uint64_t), (void *)&size);
    assert(err == CL_SUCCESS);
    err = clEnqueueNDRangeKernel(queue, kernels[0], 1, NULL, global_work_size,
                                 local_work_size, 0, NULL, NULL);
    assert(err == CL_SUCCESS);
    clFinish(queue);
  }
  *clheap = heap;
  clReleaseKernels(kernels);
  clReleaseProgram(program);
  return 0;
}


x86 assembly (clang, -O0):
<kma_create_svm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    0xe1e(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe13(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    0xe0a(%rip),%rdx
call   10b0 <clLoadProgram@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1228 <kma_create_svm+0x68>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <clCreateKernels@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1250 <kma_create_svm+0x90>
movl   $0x1,-0x4(%rbp)
jmp    137b <kma_create_svm+0x1bb>
mov    -0xc(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d5c(%rip),%rax
or     (%rax),%esi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   10e0 <clSVMAlloc@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rdx
xor    %esi,%esi
call   10d0 <clSetKernelArgSVMPointer@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2d22(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   10c0 <clSetKernelArg@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2cea(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%r8
lea    -0x58(%rbp),%r9
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %eax,%eax
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
call   1060 <clEnqueueNDRangeKernel@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2c91(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%edi
call   1030 <clFinish@plt>
mov    -0x60(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <clReleaseKernels@plt>
mov    -0x28(%rbp),%rdi
call   1040 <clReleaseProgram@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kma_create_svm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r12
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%esi
mov    %ecx,0x24(%rsp)
movq   $0x1,0x30(%rsp)
movq   $0x1,0x28(%rsp)
lea    0xe09(%rip),%rdx
mov    %ebx,%edi
call   10b0 <clLoadProgram@plt>
test   %rax,%rax
je     131f <kma_create_svm+0x15f>
mov    %rax,%r14
lea    0x18(%rsp),%rsi
mov    %rax,%rdi
call   1090 <clCreateKernels@plt>
mov    $0x1,%r13d
test   %rax,%rax
jne    130d <kma_create_svm+0x14d>
mov    0x2dbb(%rip),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d89(%rip),%rax
or     (%rax),%esi
xor    %r13d,%r13d
mov    %ebx,%edi
mov    %ebp,%edx
xor    %ecx,%ecx
call   10e0 <clSVMAlloc@plt>
mov    %r12,%rbp
mov    %rax,%r12
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    0x18(%rsp),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    %r12,%rdx
call   10d0 <clSetKernelArgSVMPointer@plt>
mov    0x2d55(%rip),%rbx
xor    %edi,%edi
cmp    (%rbx),%rax
sete   %dil
call   1070 <assert@plt>
mov    0x18(%rsp),%rax
mov    (%rax),%edi
lea    0x24(%rsp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   10c0 <clSetKernelArg@plt>
xor    %edi,%edi
cmp    (%rbx),%rax
sete   %dil
call   1070 <assert@plt>
mov    0x18(%rsp),%rax
mov    (%rax),%esi
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movl   $0x0,(%rsp)
lea    0x30(%rsp),%r8
lea    0x28(%rsp),%r9
mov    %r15d,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <clEnqueueNDRangeKernel@plt>
xor    %edi,%edi
cmp    (%rbx),%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15d,%edi
call   1030 <clFinish@plt>
mov    %r12,0x0(%rbp)
mov    0x18(%rsp),%rdi
call   1050 <clReleaseKernels@plt>
mov    %r14,%rdi
call   1040 <clReleaseProgram@plt>
mov    %r13d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.kma_create_svm.global_work_size = [1 x i64] [i64 1]
@__const.kma_create_svm.local_work_size = [1 x i64] [i64 1]
@.str = [7 x i8] c"kma.cl\00"
@.str.1 = [36 x i8] c"Error, can't load or build program\0A\00"
@CL_MEM_READ_WRITE = global i32 0
@CL_MEM_SVM_FINE_GRAIN_BUFFER = global i32 0
@CL_MEM_SVM_ATOMICS = global i32 0
@CL_SUCCESS = global i64 0
define i32 @kma_create_svm(i32 %0, i32 %1, i32 %2, i32 %3, i8** %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8**
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
%17 = alloca [1 x i64]
%18 = alloca [1 x i64]
%19 = alloca i8*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8** %4, i8*** %11
%20 = bitcast [1 x i64]* %17 to i8*
%21 = bitcast [1 x i64]* %18 to i8*
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = call i32* @clLoadProgram(i32 %22, i32 %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %12
%25 = load i32*, i32** %12
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %5
%28 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %5
%31 = load i32*, i32** %12
%32 = call i64 @clCreateKernels(i32* %31, i32** %13)
store i64 %32, i64* %15
%33 = load i64, i64* %15
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 1, i32* %6
br label %89
36: ; preds = %30
%37 = load i32, i32* %8
%38 = load i32, i32* @CL_MEM_READ_WRITE
%39 = load i32, i32* @CL_MEM_SVM_FINE_GRAIN_BUFFER
%40 = or i32 %38, %39
%41 = load i32, i32* @CL_MEM_SVM_ATOMICS
%42 = or i32 %40, %41
%43 = load i32, i32* %10
%44 = call i8* @clSVMAlloc(i32 %37, i32 %42, i32 %43, i32 0)
store i8* %44, i8** %19
%45 = load i8*, i8** %19
%46 = icmp ne i8* %45, null
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i32*, i32** %13
%50 = getelementptr inbounds i32, i32* %49, i64 0
%51 = load i32, i32* %50
%52 = load i8*, i8** %19
%53 = call i64 @clSetKernelArgSVMPointer(i32 %51, i32 0, i8* %52)
store i64 %53, i64* %15
%54 = load i64, i64* %15
%55 = load i64, i64* @CL_SUCCESS
%56 = icmp eq i64 %54, %55
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = load i32*, i32** %13
%60 = getelementptr inbounds i32, i32* %59, i64 0
%61 = load i32, i32* %60
%62 = bitcast i32* %10 to i8*
%63 = call i64 @clSetKernelArg(i32 %61, i32 1, i32 4, i8* %62)
store i64 %63, i64* %15
%64 = load i64, i64* %15
%65 = load i64, i64* @CL_SUCCESS
%66 = icmp eq i64 %64, %65
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
%69 = load i32, i32* %9
%70 = load i32*, i32** %13
%71 = getelementptr inbounds i32, i32* %70, i64 0
%72 = load i32, i32* %71
%73 = getelementptr inbounds [1 x i64], [1 x i64]* %17, i64 0, i64 0
%74 = getelementptr inbounds [1 x i64], [1 x i64]* %18, i64 0, i64 0
%75 = call i64 @clEnqueueNDRangeKernel(i32 %69, i32 %72, i32 1, i32* null, i64* %73, i64* %74, i32 0, i32* null, i32* null)
store i64 %75, i64* %15
%76 = load i64, i64* %15
%77 = load i64, i64* @CL_SUCCESS
%78 = icmp eq i64 %76, %77
%79 = zext i1 %78 to i32
%80 = call i32 @assert(i32 %79)
%81 = load i32, i32* %9
%82 = call i32 @clFinish(i32 %81)
%83 = load i8*, i8** %19
%84 = load i8**, i8*** %11
store i8* %83, i8** %84
%85 = load i32*, i32** %13
%86 = call i32 @clReleaseKernels(i32* %85)
%87 = load i32*, i32** %12
%88 = call i32 @clReleaseProgram(i32* %87)
store i32 0, i32* %6
br label %89
89: ; preds = %36, %35
%90 = load i32, i32* %6
ret i32 %90
}

LLVM IR (-O2):
@.str = [7 x i8] c"kma.cl\00"
@.str.1 = [36 x i8] c"Error, can't load or build program\0A\00"
@CL_MEM_READ_WRITE = global i32 0
@CL_MEM_SVM_FINE_GRAIN_BUFFER = global i32 0
@CL_MEM_SVM_ATOMICS = global i32 0
@CL_SUCCESS = global i64 0
define i32 @kma_create_svm(i32 %0, i32 %1, i32 %2, i32 %3, i8** nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [1 x i64]
%9 = alloca [1 x i64]
store i32 %3, i32* %6
%10 = bitcast i32** %7 to i8*
%11 = bitcast [1 x i64]* %8 to i8*
%12 = getelementptr inbounds [1 x i64], [1 x i64]* %8, i64 0, i64 0
store i64 1, i64* %12
%13 = bitcast [1 x i64]* %9 to i8*
%14 = getelementptr inbounds [1 x i64], [1 x i64]* %9, i64 0, i64 0
store i64 1, i64* %14
%15 = tail call i32* @clLoadProgram(i32 %1, i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %5
%21 = call i64 @clCreateKernels(i32* %15, i32** %7)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %59
23: ; preds = %20
%24 = load i32, i32* @CL_MEM_READ_WRITE
%25 = load i32, i32* @CL_MEM_SVM_FINE_GRAIN_BUFFER
%26 = or i32 %25, %24
%27 = load i32, i32* @CL_MEM_SVM_ATOMICS
%28 = or i32 %26, %27
%29 = call i8* @clSVMAlloc(i32 %1, i32 %28, i32 %3, i32 0)
%30 = icmp ne i8* %29, null
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32*, i32** %7
%34 = load i32, i32* %33
%35 = call i64 @clSetKernelArgSVMPointer(i32 %34, i32 0, i8* %29)
%36 = load i64, i64* @CL_SUCCESS
%37 = icmp eq i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32*, i32** %7
%41 = load i32, i32* %40
%42 = bitcast i32* %6 to i8*
%43 = call i64 @clSetKernelArg(i32 %41, i32 1, i32 4, i8* %42)
%44 = load i64, i64* @CL_SUCCESS
%45 = icmp eq i64 %43, %44
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = load i32*, i32** %7
%49 = load i32, i32* %48
%50 = call i64 @clEnqueueNDRangeKernel(i32 %2, i32 %49, i32 1, i32* null, i64* %12, i64* %14, i32 0, i32* null, i32* null)
%51 = load i64, i64* @CL_SUCCESS
%52 = icmp eq i64 %50, %51
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = call i32 @clFinish(i32 %2)
store i8* %29, i8** %4
%56 = load i32*, i32** %7
%57 = call i32 @clReleaseKernels(i32* %56)
%58 = call i32 @clReleaseProgram(i32* %15)
br label %59
59: ; preds = %20, %23
%60 = phi i32 [ 0, %23 ], [ 1, %20 ]
ret i32 %60
}