#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_uint;
typedef int /*<<< orphan*/ cl_mem;
typedef int /*<<< orphan*/ cl_kernel;
typedef int /*<<< orphan*/ cl_event;
typedef int /*<<< orphan*/ cl_context;
typedef int /*<<< orphan*/ cl_command_queue;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ CL_MEM_WRITE_ONLY;
int /*<<< orphan*/ check_error(int, char *);
int /*<<< orphan*/ clCreateBuffer(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, int *);
int clEnqueueNDRangeKernel(int /*<<< orphan*/, int /*<<< orphan*/, int,
                           int /*<<< orphan*/ *, size_t *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *);
int clSetKernelArg(int /*<<< orphan*/, int /*<<< orphan*/, int, void *);
void enqueue_job(cl_context *context, cl_kernel *kernel,
                 cl_command_queue *queue, cl_event *event) {
  int ocl_status;
  int num_items = 50000;
  cl_mem buffer =
      clCreateBuffer(*context, CL_MEM_WRITE_ONLY, num_items * sizeof(cl_uint),
                     NULL, &ocl_status);
  check_error(ocl_status, "Create buffer");
  size_t global_work_size = num_items;
  ocl_status = clSetKernelArg(*kernel, 0, sizeof(buffer), (void *)&buffer);
  check_error(ocl_status, "Set Kernel Arg");
  ocl_status = clEnqueueNDRangeKernel(*queue, *kernel, 1, NULL,
                                      &global_work_size, NULL, 0, NULL, event);
  check_error(ocl_status, "Enqueue NDRange Kernel");
}


x86 assembly (clang, -O0):
<enqueue_job>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xc350,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
movslq -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%r8
call   1030 <clCreateBuffer@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe6e(%rip),%rsi
call   1050 <check_error@plt>
movslq -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rcx
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <clSetKernelArg@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe4c(%rip),%rsi
call   1050 <check_error@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %ecx,%r9d
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    %r9,%rcx
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1040 <clEnqueueNDRangeKernel@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe0c(%rip),%rsi
call   1050 <check_error@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enqueue_job>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    (%rdi),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r8
mov    $0x30d40,%edx
xor    %ecx,%ecx
call   1030 <clCreateBuffer@plt>
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe86(%rip),%rsi
call   1050 <check_error@plt>
movq   $0xc350,0x8(%rsp)
mov    (%rbx),%edi
lea    0x4(%rsp),%rcx
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <clSetKernelArg@plt>
mov    %eax,(%rsp)
lea    0xe69(%rip),%rsi
mov    %eax,%edi
call   1050 <check_error@plt>
mov    (%r15),%edi
mov    (%rbx),%esi
sub    $0x8,%rsp
lea    0x10(%rsp),%r8
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
push   %r14
push   $0x0
push   $0x0
call   1040 <clEnqueueNDRangeKernel@plt>
add    $0x20,%rsp
mov    %eax,(%rsp)
lea    0xe40(%rip),%rsi
mov    %eax,%edi
call   1050 <check_error@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@CL_MEM_WRITE_ONLY = global i32 0
@.str = [14 x i8] c"Create buffer\00"
@.str.1 = [15 x i8] c"Set Kernel Arg\00"
@.str.2 = [23 x i8] c"Enqueue NDRange Kernel\00"
define void @enqueue_job(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%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 50000, i32* %10
%13 = load i32*, i32** %5
%14 = load i32, i32* %13
%15 = load i32, i32* @CL_MEM_WRITE_ONLY
%16 = load i32, i32* %10
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 4
%19 = trunc i64 %18 to i32
%20 = call i32 @clCreateBuffer(i32 %14, i32 %15, i32 %19, i32* null, i32* %9)
store i32 %20, i32* %11
%21 = load i32, i32* %9
%22 = call i32 @check_error(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
store i64 %24, i64* %12
%25 = load i32*, i32** %6
%26 = load i32, i32* %25
%27 = bitcast i32* %11 to i8*
%28 = call i32 @clSetKernelArg(i32 %26, i32 0, i32 4, i8* %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = call i32 @check_error(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = load i32*, i32** %6
%34 = load i32, i32* %33
%35 = load i32*, i32** %8
%36 = call i32 @clEnqueueNDRangeKernel(i32 %32, i32 %34, i32 1, i32* null, i64* %12, i32* null, i32 0, i32* null, i32* %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = call i32 @check_error(i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@CL_MEM_WRITE_ONLY = global i32 0
@.str = [14 x i8] c"Create buffer\00"
@.str.1 = [15 x i8] c"Set Kernel Arg\00"
@.str.2 = [23 x i8] c"Enqueue NDRange Kernel\00"
define void @enqueue_job(i32* nocapture %0, i32* nocapture %1, i32* nocapture %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = load i32, i32* %0
%11 = load i32, i32* @CL_MEM_WRITE_ONLY
%12 = call i32 @clCreateBuffer(i32 %10, i32 %11, i32 200000, i32* null, i32* %5)
store i32 %12, i32* %6
%13 = load i32, i32* %5
%14 = call i32 @check_error(i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = bitcast i64* %7 to i8*
store i64 50000, i64* %7
%16 = load i32, i32* %1
%17 = call i32 @clSetKernelArg(i32 %16, i32 0, i32 4, i8* %9)
store i32 %17, i32* %5
%18 = call i32 @check_error(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = load i32, i32* %1
%21 = call i32 @clEnqueueNDRangeKernel(i32 %19, i32 %20, i32 1, i32* null, i64* %7, i32* null, i32 0, i32* null, i32* %3)
store i32 %21, i32* %5
%22 = call i32 @check_error(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
ret void
}