#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_platform_id;
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_TYPE_GPU;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ clCreateCommandQueue(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/, int /*<<< orphan*/,
                                        scalar_t__ *);
int /*<<< orphan*/ *clCreateContext(int /*<<< orphan*/, int,
                                    int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ *, scalar_t__ *);
scalar_t__ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ clGetPlatformIDs(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ clReleaseContext(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int cl_utils_setup_gpu(cl_context *context, cl_command_queue *command_queue,
                       cl_device_id *device) {
  cl_int err;
  cl_platform_id platform;
  err = clGetPlatformIDs(1, &platform, NULL);
  if (err != CL_SUCCESS)
    goto out_err;
  err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, device, NULL);
  if (err != CL_SUCCESS)
    goto out_err;
  *context = clCreateContext(0, 1, device, NULL, NULL, &err);
  if (err != CL_SUCCESS)
    goto out_err;
  *command_queue = clCreateCommandQueue(*context, *device, 0, &err);
  if (err != CL_SUCCESS)
    goto out_no_queue;
  return 0;
out_no_queue:
  clReleaseContext(*context);
  *context = NULL;
out_err:
  fprintf(stderr, "cl_utils_setup_gpu: failed\n");
  fflush(stderr);
  return -1;
}


x86 assembly (clang, -O0):
<cl_utils_setup_gpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <clGetPlatformIDs@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11b3 <cl_utils_setup_gpu+0x43>
jmp    128a <cl_utils_setup_gpu+0x11a>
mov    -0x2c(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetDeviceIDs@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
je     11ef <cl_utils_setup_gpu+0x7f>
jmp    128a <cl_utils_setup_gpu+0x11a>
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
mov    $0x1,%esi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x28(%rbp),%r9
mov    %r8,%rcx
call   1060 <clCreateContext@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
je     122e <cl_utils_setup_gpu+0xbe>
jmp    128a <cl_utils_setup_gpu+0x11a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
xor    %edx,%edx
lea    -0x28(%rbp),%rcx
call   1040 <clCreateCommandQueue@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
je     1267 <cl_utils_setup_gpu+0xf7>
jmp    1273 <cl_utils_setup_gpu+0x103>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <cl_utils_setup_gpu+0x144>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <clReleaseContext@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd66(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_utils_setup_gpu>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
xor    %edx,%edx
call   1050 <clGetPlatformIDs@plt>
mov    %rax,(%rsp)
mov    0x2e2e(%rip),%r13
cmp    0x0(%r13),%rax
jne    1222 <cl_utils_setup_gpu+0xb2>
mov    0xc(%rsp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1030 <clGetDeviceIDs@plt>
mov    %rax,(%rsp)
cmp    0x0(%r13),%rax
jne    1222 <cl_utils_setup_gpu+0xb2>
xor    %ebx,%ebx
mov    %rsp,%r9
xor    %edi,%edi
mov    $0x1,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <clCreateContext@plt>
mov    %rax,(%r15)
mov    (%rsp),%rcx
cmp    0x0(%r13),%rcx
jne    1222 <cl_utils_setup_gpu+0xb2>
mov    (%r12),%esi
mov    %rsp,%rcx
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <clCreateCommandQueue@plt>
mov    %eax,(%r14)
mov    (%rsp),%rax
cmp    0x0(%r13),%rax
je     1243 <cl_utils_setup_gpu+0xd3>
mov    (%r15),%rdi
call   1090 <clReleaseContext@plt>
movq   $0x0,(%r15)
mov    0x2daf(%rip),%rbx
mov    (%rbx),%edi
lea    0xdce(%rip),%rsi
call   1070 <fprintf@plt>
mov    (%rbx),%edi
call   1080 <fflush@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@CL_SUCCESS = global i64 0
@CL_DEVICE_TYPE_GPU = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"cl_utils_setup_gpu: failed\0A\00"
define i32 @cl_utils_setup_gpu(i32** %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i32** %0, i32*** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%10 = call i64 @clGetPlatformIDs(i32 1, i32* %9, i32* null)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = load i64, i64* @CL_SUCCESS
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
br label %49
15: ; preds = %3
%16 = load i32, i32* %9
%17 = load i32, i32* @CL_DEVICE_TYPE_GPU
%18 = load i32*, i32** %7
%19 = call i64 @clGetDeviceIDs(i32 %16, i32 %17, i32 1, i32* %18, i32* null)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = load i64, i64* @CL_SUCCESS
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %15
br label %49
24: ; preds = %15
%25 = load i32*, i32** %7
%26 = call i32* @clCreateContext(i32 0, i32 1, i32* %25, i32* null, i32* null, i64* %8)
%27 = load i32**, i32*** %5
store i32* %26, i32** %27
%28 = load i64, i64* %8
%29 = load i64, i64* @CL_SUCCESS
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %24
br label %49
32: ; preds = %24
%33 = load i32**, i32*** %5
%34 = load i32*, i32** %33
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = call i32 @clCreateCommandQueue(i32* %34, i32 %36, i32 0, i64* %8)
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load i64, i64* %8
%40 = load i64, i64* @CL_SUCCESS
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %32
br label %44
43: ; preds = %32
store i32 0, i32* %4
br label %54
44: ; preds = %42
%45 = load i32**, i32*** %5
%46 = load i32*, i32** %45
%47 = call i32 @clReleaseContext(i32* %46)
%48 = load i32**, i32*** %5
store i32* null, i32** %48
br label %49
49: ; preds = %44, %31, %23, %14
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%52 = load i32, i32* @stderr
%53 = call i32 @fflush(i32 %52)
store i32 -1, i32* %4
br label %54
54: ; preds = %49, %43
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@CL_SUCCESS = global i64 0
@CL_DEVICE_TYPE_GPU = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"cl_utils_setup_gpu: failed\0A\00"
define i32 @cl_utils_setup_gpu(i32** nocapture %0, i32* nocapture writeonly %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = bitcast i64* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i64 @clGetPlatformIDs(i32 1, i32* %5, i32* null)
store i64 %8, i64* %4
%9 = load i64, i64* @CL_SUCCESS
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %31
11: ; preds = %3
%12 = load i32, i32* %5
%13 = load i32, i32* @CL_DEVICE_TYPE_GPU
%14 = call i64 @clGetDeviceIDs(i32 %12, i32 %13, i32 1, i32* %2, i32* null)
store i64 %14, i64* %4
%15 = load i64, i64* @CL_SUCCESS
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %31
17: ; preds = %11
%18 = call i32* @clCreateContext(i32 0, i32 1, i32* %2, i32* null, i32* null, i64* %4)
store i32* %18, i32** %0
%19 = load i64, i64* %4
%20 = load i64, i64* @CL_SUCCESS
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i32, i32* %2
%24 = call i32 @clCreateCommandQueue(i32* %18, i32 %23, i32 0, i64* %4)
store i32 %24, i32* %1
%25 = load i64, i64* %4
%26 = load i64, i64* @CL_SUCCESS
%27 = icmp eq i64 %25, %26
br i1 %27, label %36, label %28
28: ; preds = %22
%29 = load i32*, i32** %0
%30 = call i32 @clReleaseContext(i32* %29)
store i32* null, i32** %0
br label %31
31: ; preds = %17, %11, %3, %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 @fflush(i32 %34)
br label %36
36: ; preds = %22, %31
%37 = phi i32 [ -1, %31 ], [ 0, %22 ]
ret i32 %37
}