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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_TYPE_ALL;
int /*<<< orphan*/ clError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ clGetPlatformIDs(int, int /*<<< orphan*/ *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
cl_device_id get_device_by_id(int platform_index, int device_index) {
  cl_int error;
  cl_uint n_platforms;
  error = clGetPlatformIDs(0, NULL, &n_platforms);
  if (n_platforms == 0) {
    printf("No OpenCL platforms found\n");
  }
  cl_platform_id *all_platforms =
      (cl_platform_id *)malloc(sizeof(cl_platform_id) * n_platforms);
  error = clGetPlatformIDs(n_platforms, all_platforms, NULL);
  cl_uint n_devices;
  error = clGetDeviceIDs(all_platforms[platform_index], CL_DEVICE_TYPE_ALL, 0,
                         NULL, &n_devices);
  cl_device_id *all_devices =
      (cl_device_id *)malloc(sizeof(cl_device_id) * n_devices);
  error = clGetDeviceIDs(all_platforms[platform_index], CL_DEVICE_TYPE_ALL,
                         n_devices, all_devices, NULL);
  clError("get device by id", error);
  return all_devices[device_index];
}


x86 assembly (clang, -O0):
<get_device_by_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x10(%rbp),%rdx
call   1050 <clGetPlatformIDs@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1186 <get_device_by_id+0x36>
lea    0xe7f(%rip),%rdi
call   1060 <printf@plt>
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <clGetPlatformIDs@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1c(%rbp),%r8
call   1040 <clGetDeviceIDs@plt>
mov    %eax,-0xc(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <clGetDeviceIDs@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe04(%rip),%rdi
call   1030 <clError@plt>
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device_by_id>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x8(%rsp),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1060 <clGetPlatformIDs@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
jne    1187 <get_device_by_id+0x37>
lea    0xe93(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
mov    %rax,%rsi
xor    %edx,%edx
call   1060 <clGetPlatformIDs@plt>
movslq %ebp,%r15
mov    (%rbx,%r15,4),%edi
mov    0x2e3a(%rip),%r12
mov    (%r12),%esi
lea    0xc(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <clGetDeviceIDs@plt>
mov    0xc(%rsp),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    (%rbx,%r15,4),%edi
mov    (%r12),%esi
mov    0xc(%rsp),%edx
mov    %rax,%rcx
xor    %r8d,%r8d
call   1050 <clGetDeviceIDs@plt>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
call   1030 <clError@plt>
movslq %r14d,%rax
mov    0x0(%rbp,%rax,4),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"No OpenCL platforms found\0A\00"
@CL_DEVICE_TYPE_ALL = global i32 0
@.str.1 = [17 x i8] c"get device by id\00"
define i32 @get_device_by_id(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 @clGetPlatformIDs(i32 0, i32* null, i32* %6)
store i32 %10, i32* %5
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = mul i64 4, %17
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %7
%22 = load i32, i32* %6
%23 = load i32*, i32** %7
%24 = call i32 @clGetPlatformIDs(i32 %22, i32* %23, i32* null)
store i32 %24, i32* %5
%25 = load i32*, i32** %7
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
%30 = load i32, i32* @CL_DEVICE_TYPE_ALL
%31 = call i32 @clGetDeviceIDs(i32 %29, i32 %30, i32 0, i32* null, i32* %8)
store i32 %31, i32* %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = mul i64 4, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %9
%38 = load i32*, i32** %7
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = load i32, i32* @CL_DEVICE_TYPE_ALL
%44 = load i32, i32* %8
%45 = load i32*, i32** %9
%46 = call i32 @clGetDeviceIDs(i32 %42, i32 %43, i32 %44, i32* %45, i32* null)
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = call i32 @clError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = load i32*, i32** %9
%50 = load i32, i32* %4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
ret i32 %53
}

LLVM IR (-O2):
@CL_DEVICE_TYPE_ALL = global i32 0
@.str.1 = [17 x i8] c"get device by id\00"
@str = [26 x i8] c"No OpenCL platforms found\00"
define i32 @get_device_by_id(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = call i32 @clGetPlatformIDs(i32 0, i32* null, i32* %3)
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%11 = load i32, i32* %3
br label %12
12: ; preds = %9, %2
%13 = phi i32 [ %11, %9 ], [ %7, %2 ]
%14 = shl i32 %13, 2
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i32*
%17 = load i32, i32* %3
%18 = call i32 @clGetPlatformIDs(i32 %17, i32* %16, i32* null)
%19 = bitcast i32* %4 to i8*
%20 = sext i32 %0 to i64
%21 = getelementptr inbounds i32, i32* %16, i64 %20
%22 = load i32, i32* %21
%23 = load i32, i32* @CL_DEVICE_TYPE_ALL
%24 = call i32 @clGetDeviceIDs(i32 %22, i32 %23, i32 0, i32* null, i32* %4)
%25 = load i32, i32* %4
%26 = shl i32 %25, 2
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i32*
%29 = load i32, i32* %21
%30 = load i32, i32* @CL_DEVICE_TYPE_ALL
%31 = load i32, i32* %4
%32 = call i32 @clGetDeviceIDs(i32 %29, i32 %30, i32 %31, i32* %28, i32* null)
%33 = call i32 @clError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = sext i32 %1 to i64
%35 = getelementptr inbounds i32, i32* %28, i64 %34
%36 = load i32, i32* %35
ret i32 %36
}