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

/* Variables and functions */
int /*<<< orphan*/ CL_CONTEXT_DEVICES;
int /*<<< orphan*/ CL_DEVICE_TYPE;
scalar_t__ CL_DEVICE_TYPE_CPU;
scalar_t__ CL_DEVICE_TYPE_GPU;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ assert(int);
scalar_t__ calloc(size_t, int);
scalar_t__ clGetContextInfo(int /*<<< orphan*/, int /*<<< orphan*/, size_t,
                            int /*<<< orphan*/ *, size_t *);
scalar_t__ clGetDeviceInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                           scalar_t__ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int GetDevice(cl_device_id *dev_gpu, cl_device_id *dev_cpu, cl_context ctx) {
  size_t num_dev = 0;
  cl_int status = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, NULL, &num_dev);
  assert(status == CL_SUCCESS);
  assert(num_dev >= 2);
  cl_device_id *dev = (cl_device_id *)calloc(num_dev, sizeof(cl_device_id));
  assert(dev);
  status = clGetContextInfo(ctx, CL_CONTEXT_DEVICES,
                            num_dev * sizeof(cl_device_id), dev, NULL);
  assert(status == CL_SUCCESS);
  for (size_t i = 0; i < num_dev; ++i) {
    cl_device_type t = 0;
    status = clGetDeviceInfo(dev[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &t,
                             NULL);
    assert(status == CL_SUCCESS);
    if (t == CL_DEVICE_TYPE_GPU)
      *dev_gpu = dev[i];
    else if (t == CL_DEVICE_TYPE_CPU)
      *dev_cpu = dev[i];
    if (*dev_gpu && *dev_cpu)
      break;
  }
  free(dev);
  dev = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<GetDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1030 <clGetContextInfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x2,-0x20(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
shl    $0x2,%rdx
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetContextInfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1303 <GetDevice+0x1b3>
movq   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetDeviceInfo@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x40(%rbp),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a4 <GetDevice+0x154>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ce <GetDevice+0x17e>
mov    -0x40(%rbp),%rax
mov    0x2d21(%rip),%rcx
cmp    (%rcx),%rax
jne    12c9 <GetDevice+0x179>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ce <GetDevice+0x17e>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
je     12ed <GetDevice+0x19d>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     12ed <GetDevice+0x19d>
jmp    1303 <GetDevice+0x1b3>
jmp    12f2 <GetDevice+0x1a2>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    121e <GetDevice+0xce>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetDevice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
movq   $0x0,(%rsp)
mov    0x2e70(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%r8
mov    %edx,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <clGetContextInfo@plt>
mov    0x2e38(%rip),%rbp
xor    %edi,%edi
cmp    0x0(%rbp),%rax
sete   %dil
call   1070 <assert@plt>
xor    %edi,%edi
cmpq   $0x2,(%rsp)
setae  %dil
call   1070 <assert@plt>
mov    (%rsp),%rdi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   1070 <assert@plt>
mov    (%r14),%esi
mov    (%rsp),%rdx
shl    $0x2,%rdx
mov    %r12d,%edi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1030 <clGetContextInfo@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%rax
sete   %dil
call   1070 <assert@plt>
cmpq   $0x0,(%rsp)
je     12ab <GetDevice+0x15b>
xor    %ebp,%ebp
mov    0x2db5(%rip),%r13
lea    0x10(%rsp),%r12
mov    0x2dd1(%rip),%r14
jmp    122e <GetDevice+0xde>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    (%rsp),%rbp
jae    12ab <GetDevice+0x15b>
movq   $0x0,0x10(%rsp)
mov    (%rbx,%rbp,4),%edi
mov    0x0(%r13),%esi
mov    $0x8,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1050 <clGetDeviceInfo@plt>
xor    %edi,%edi
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
sete   %dil
call   1070 <assert@plt>
mov    0x10(%rsp),%rax
cmp    (%r14),%rax
jne    1280 <GetDevice+0x130>
mov    (%rbx,%rbp,4),%eax
mov    %eax,(%r15)
test   %eax,%eax
jne    129d <GetDevice+0x14d>
jmp    1220 <GetDevice+0xd0>
nopl   0x0(%rax)
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    1296 <GetDevice+0x146>
mov    (%rbx,%rbp,4),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
mov    (%r15),%eax
test   %eax,%eax
je     1220 <GetDevice+0xd0>
mov    0x8(%rsp),%rax
cmpl   $0x0,(%rax)
je     1220 <GetDevice+0xd0>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CL_CONTEXT_DEVICES = global i32 0
@CL_SUCCESS = global i64 0
@CL_DEVICE_TYPE = global i32 0
@CL_DEVICE_TYPE_GPU = global i64 0
@CL_DEVICE_TYPE_CPU = global i64 0
define i32 @GetDevice(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i64 0, i64* %7
%12 = load i32, i32* %6
%13 = load i32, i32* @CL_CONTEXT_DEVICES
%14 = call i64 @clGetContextInfo(i32 %12, i32 %13, i64 0, i32* null, i64* %7)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = load i64, i64* @CL_SUCCESS
%17 = icmp eq i64 %15, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i64, i64* %7
%21 = icmp uge i64 %20, 2
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i64, i64* %7
%25 = call i64 @calloc(i64 %24, i32 4)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %9
%27 = load i32*, i32** %9
%28 = ptrtoint i32* %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %6
%31 = load i32, i32* @CL_CONTEXT_DEVICES
%32 = load i64, i64* %7
%33 = mul i64 %32, 4
%34 = load i32*, i32** %9
%35 = call i64 @clGetContextInfo(i32 %30, i32 %31, i64 %33, i32* %34, i64* null)
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = load i64, i64* @CL_SUCCESS
%38 = icmp eq i64 %36, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
store i64 0, i64* %10
br label %41
41: ; preds = %87, %3
%42 = load i64, i64* %10
%43 = load i64, i64* %7
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %90
45: ; preds = %41
store i64 0, i64* %11
%46 = load i32*, i32** %9
%47 = load i64, i64* %10
%48 = getelementptr inbounds i32, i32* %46, i64 %47
%49 = load i32, i32* %48
%50 = load i32, i32* @CL_DEVICE_TYPE
%51 = call i64 @clGetDeviceInfo(i32 %49, i32 %50, i32 8, i64* %11, i32* null)
store i64 %51, i64* %8
%52 = load i64, i64* %8
%53 = load i64, i64* @CL_SUCCESS
%54 = icmp eq i64 %52, %53
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i64, i64* %11
%58 = load i64, i64* @CL_DEVICE_TYPE_GPU
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %66
60: ; preds = %45
%61 = load i32*, i32** %9
%62 = load i64, i64* %10
%63 = getelementptr inbounds i32, i32* %61, i64 %62
%64 = load i32, i32* %63
%65 = load i32*, i32** %4
store i32 %64, i32* %65
br label %77
66: ; preds = %45
%67 = load i64, i64* %11
%68 = load i64, i64* @CL_DEVICE_TYPE_CPU
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %76
70: ; preds = %66
%71 = load i32*, i32** %9
%72 = load i64, i64* %10
%73 = getelementptr inbounds i32, i32* %71, i64 %72
%74 = load i32, i32* %73
%75 = load i32*, i32** %5
store i32 %74, i32* %75
br label %76
76: ; preds = %70, %66
br label %77
77: ; preds = %76, %60
%78 = load i32*, i32** %4
%79 = load i32, i32* %78
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %86
81: ; preds = %77
%82 = load i32*, i32** %5
%83 = load i32, i32* %82
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %81
br label %90
86: ; preds = %81, %77
br label %87
87: ; preds = %86
%88 = load i64, i64* %10
%89 = add i64 %88, 1
store i64 %89, i64* %10
br label %41
90: ; preds = %85, %41
%91 = load i32*, i32** %9
%92 = call i32 @free(i32* %91)
store i32* null, i32** %9
ret i32 0
}

LLVM IR (-O2):
@CL_CONTEXT_DEVICES = global i32 0
@CL_SUCCESS = global i64 0
@CL_DEVICE_TYPE = global i32 0
@CL_DEVICE_TYPE_GPU = global i64 0
@CL_DEVICE_TYPE_CPU = global i64 0
define i32 @GetDevice(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = load i32, i32* @CL_CONTEXT_DEVICES
%8 = call i64 @clGetContextInfo(i32 %2, i32 %7, i64 0, i32* null, i64* %4)
%9 = load i64, i64* @CL_SUCCESS
%10 = icmp eq i64 %8, %9
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i64, i64* %4
%14 = icmp ugt i64 %13, 1
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i64, i64* %4
%18 = call i64 @calloc(i64 %17, i32 4)
%19 = inttoptr i64 %18 to i32*
%20 = trunc i64 %18 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @CL_CONTEXT_DEVICES
%23 = load i64, i64* %4
%24 = shl i64 %23, 2
%25 = call i64 @clGetContextInfo(i32 %2, i32 %22, i64 %24, i32* %19, i64* null)
%26 = load i64, i64* @CL_SUCCESS
%27 = icmp eq i64 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i64, i64* %4
%31 = icmp eq i64 %30, 0
br i1 %31, label %67, label %32
32: ; preds = %3
%33 = bitcast i64* %5 to i8*
br label %34
34: ; preds = %32, %63
%35 = phi i64 [ 0, %32 ], [ %64, %63 ]
store i64 0, i64* %5
%36 = getelementptr inbounds i32, i32* %19, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* @CL_DEVICE_TYPE
%39 = call i64 @clGetDeviceInfo(i32 %37, i32 %38, i32 8, i64* %5, i32* null)
%40 = load i64, i64* @CL_SUCCESS
%41 = icmp eq i64 %39, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i64, i64* %5
%45 = load i64, i64* @CL_DEVICE_TYPE_GPU
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %34
%48 = load i32, i32* %36
store i32 %48, i32* %0
br label %56
49: ; preds = %34
%50 = load i64, i64* @CL_DEVICE_TYPE_CPU
%51 = icmp eq i64 %44, %50
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i32, i32* %36
store i32 %53, i32* %1
br label %54
54: ; preds = %52, %49
%55 = load i32, i32* %0
br label %56
56: ; preds = %54, %47
%57 = phi i32 [ %55, %54 ], [ %48, %47 ]
%58 = icmp eq i32 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %56
%60 = load i32, i32* %1
%61 = icmp eq i32 %60, 0
br i1 %61, label %63, label %62
62: ; preds = %59
br label %67
63: ; preds = %56, %59
%64 = add nuw i64 %35, 1
%65 = load i64, i64* %4
%66 = icmp ult i64 %64, %65
br i1 %66, label %34, label %67
67: ; preds = %63, %3, %62
%68 = call i32 @free(i32* %19)
ret i32 0
}