#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 scalar_t__ cl_int;
typedef int /*<<< orphan*/ *cl_device_id;
typedef int /*<<< orphan*/ cl_context;

/* Variables and functions */
int /*<<< orphan*/ CL_CONTEXT_DEVICES;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ assert(int);
scalar_t__ clGetContextInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                            int /*<<< orphan*/ **, size_t *);
int GetDevice(cl_device_id *dev, cl_context ctx) {
  size_t num_device = 0;
  cl_int status =
      clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, NULL, &num_device);
  assert(status == CL_SUCCESS);
  assert(num_device > 0);
  status = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(cl_device_id), dev,
                            NULL);
  assert(status == CL_SUCCESS);
  assert(*dev != NULL);
  return 0;
}


x86 assembly (clang, -O0):
<GetDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
call   1030 <clGetContextInfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    $0x8,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetContextInfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetDevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    0x2ead(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%r8
mov    %ebp,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <clGetContextInfo@plt>
mov    0x2e85(%rip),%r15
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1040 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%rsp)
setne  %dil
call   1040 <assert@plt>
mov    (%r14),%esi
mov    %ebp,%edi
mov    $0x8,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1030 <clGetContextInfo@plt>
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1040 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%rbx)
setne  %dil
call   1040 <assert@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CL_CONTEXT_DEVICES = global i32 0
@CL_SUCCESS = global i64 0
define i32 @GetDevice(i32** %0, i32 %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32** %0, i32*** %3
store i32 %1, i32* %4
store i64 0, i64* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @CL_CONTEXT_DEVICES
%9 = call i64 @clGetContextInfo(i32 %7, i32 %8, i32 0, i32** null, i64* %5)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = load i64, i64* @CL_SUCCESS
%12 = icmp eq i64 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %5
%16 = icmp ugt i64 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %4
%20 = load i32, i32* @CL_CONTEXT_DEVICES
%21 = load i32**, i32*** %3
%22 = call i64 @clGetContextInfo(i32 %19, i32 %20, i32 8, i32** %21, i64* null)
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = load i64, i64* @CL_SUCCESS
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32**, i32*** %3
%29 = load i32*, i32** %28
%30 = icmp ne i32* %29, null
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
ret i32 0
}

LLVM IR (-O2):
@CL_CONTEXT_DEVICES = global i32 0
@CL_SUCCESS = global i64 0
define i32 @GetDevice(i32** %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%5 = load i32, i32* @CL_CONTEXT_DEVICES
%6 = call i64 @clGetContextInfo(i32 %1, i32 %5, i32 0, i32** null, i64* %3)
%7 = load i64, i64* @CL_SUCCESS
%8 = icmp eq i64 %6, %7
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i64, i64* %3
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @CL_CONTEXT_DEVICES
%16 = call i64 @clGetContextInfo(i32 %1, i32 %15, i32 8, i32** %0, i64* null)
%17 = load i64, i64* @CL_SUCCESS
%18 = icmp eq i64 %16, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32*, i32** %0
%22 = icmp ne i32* %21, null
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
ret i32 0
}