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

/* Variables and functions */
int /*<<< orphan*/ clError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ clGetDeviceIDs(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ clGetPlatformIDs(int, int /*<<< orphan*/ **, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ **);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
cl_device_id get_device(cl_device_type device_type) {
  cl_int error;
  cl_platform_id platform;
  cl_device_id device;
  cl_uint n_platforms;
  error = clGetPlatformIDs(0, NULL, &n_platforms);
  if (n_platforms == 0) {
    fprintf(stderr, "No OpenCL platforms found\n");
    return NULL;
  }
  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;
  int platform_to_use = -1;
  for (int i = 0; i < n_platforms; i++) {
    error = clGetDeviceIDs(all_platforms[i], device_type, 0, NULL, &n_devices);
    if (n_devices > 0) {
      platform_to_use = i;
      break;
    }
  }
  if (platform_to_use == -1) {
    fprintf(stderr, "No platform with the requested device\n");
    return NULL;
  }
  platform = all_platforms[platform_to_use];
  cl_device_id *all_devices =
      (cl_device_id *)malloc(sizeof(cl_device_id) * n_devices);
  error = clGetDeviceIDs(platform, device_type, n_devices, all_devices, NULL);
  device = all_devices[0];
  free(all_devices);
  free(all_platforms);
  clError("Couldn't get device", error);
  return device;
}


x86 assembly (clang, -O0):
<get_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x24(%rbp),%rdx
call   1060 <clGetPlatformIDs@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11a9 <get_device+0x49>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_device+0x170>
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <clGetPlatformIDs@plt>
mov    %eax,-0x10(%rbp)
movl   $0xffffffff,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1232 <get_device+0xd2>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x34(%rbp),%r8
call   1050 <clGetDeviceIDs@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    121f <get_device+0xbf>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1232 <get_device+0xd2>
jmp    1224 <get_device+0xc4>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11dd <get_device+0x7d>
cmpl   $0xffffffff,-0x38(%rbp)
jne    125e <get_device+0xfe>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d0 <get_device+0x170>
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x48(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <clGetDeviceIDs@plt>
mov    %eax,-0x10(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%esi
lea    0xd7f(%rip),%rdi
call   1040 <clError@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r12d
lea    0x8(%rsp),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1060 <clGetPlatformIDs@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
je     11eb <get_device+0x8b>
shl    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
mov    %rax,%rsi
xor    %edx,%edx
call   1060 <clGetPlatformIDs@plt>
cmpl   $0x0,0x8(%rsp)
jle    11d9 <get_device+0x79>
xor    %ebp,%ebp
lea    0xc(%rsp),%r14
nopl   0x0(%rax)
mov    (%rbx,%rbp,8),%rdi
mov    %r12d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <clGetDeviceIDs@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
jg     1204 <get_device+0xa4>
add    $0x1,%rbp
movslq 0x8(%rsp),%rax
cmp    %rax,%rbp
jl     11b0 <get_device+0x50>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
jmp    11fb <get_device+0x9b>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1070 <fprintf@plt>
xor    %ebp,%ebp
jmp    124d <get_device+0xed>
mov    (%rbx,%rbp,8),%r14
shl    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    0xc(%rsp),%edx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %rax,%rcx
xor    %r8d,%r8d
call   1050 <clGetDeviceIDs@plt>
mov    %eax,%r14d
mov    (%r15),%rbp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdfd(%rip),%rdi
mov    %r14d,%esi
call   1040 <clError@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"No OpenCL platforms found\0A\00"
@.str.1 = [39 x i8] c"No platform with the requested device\0A\00"
@.str.2 = [20 x i8] c"Couldn't get device\00"
define i32* @get_device(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32**
store i32 %0, i32* %3
%13 = call i32 @clGetPlatformIDs(i32 0, i32** null, i32* %7)
store i32 %13, i32* %4
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %2
br label %82
19: ; preds = %1
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = mul i64 8, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i32**
store i32** %25, i32*** %8
%26 = load i32, i32* %7
%27 = load i32**, i32*** %8
%28 = call i32 @clGetPlatformIDs(i32 %26, i32** %27, i32* null)
store i32 %28, i32* %4
store i32 -1, i32* %10
store i32 0, i32* %11
br label %29
29: ; preds = %46, %19
%30 = load i32, i32* %11
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %49
33: ; preds = %29
%34 = load i32**, i32*** %8
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32*, i32** %34, i64 %36
%38 = load i32*, i32** %37
%39 = load i32, i32* %3
%40 = call i32 @clGetDeviceIDs(i32* %38, i32 %39, i32 0, i32** null, i32* %9)
store i32 %40, i32* %4
%41 = load i32, i32* %9
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %33
%44 = load i32, i32* %11
store i32 %44, i32* %10
br label %49
45: ; preds = %33
br label %46
46: ; preds = %45
%47 = load i32, i32* %11
%48 = add nsw i32 %47, 1
store i32 %48, i32* %11
br label %29
49: ; preds = %43, %29
%50 = load i32, i32* %10
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %2
br label %82
55: ; preds = %49
%56 = load i32**, i32*** %8
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32*, i32** %56, i64 %58
%60 = load i32*, i32** %59
store i32* %60, i32** %5
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = mul i64 8, %62
%64 = trunc i64 %63 to i32
%65 = call i64 @malloc(i32 %64)
%66 = inttoptr i64 %65 to i32**
store i32** %66, i32*** %12
%67 = load i32*, i32** %5
%68 = load i32, i32* %3
%69 = load i32, i32* %9
%70 = load i32**, i32*** %12
%71 = call i32 @clGetDeviceIDs(i32* %67, i32 %68, i32 %69, i32** %70, i32* null)
store i32 %71, i32* %4
%72 = load i32**, i32*** %12
%73 = getelementptr inbounds i32*, i32** %72, i64 0
%74 = load i32*, i32** %73
store i32* %74, i32** %6
%75 = load i32**, i32*** %12
%76 = call i32 @free(i32** %75)
%77 = load i32**, i32*** %8
%78 = call i32 @free(i32** %77)
%79 = load i32, i32* %4
%80 = call i32 @clError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %79)
%81 = load i32*, i32** %6
store i32* %81, i32** %2
br label %82
82: ; preds = %55, %52, %16
%83 = load i32*, i32** %2
ret i32* %83
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"No OpenCL platforms found\0A\00"
@.str.1 = [39 x i8] c"No platform with the requested device\0A\00"
@.str.2 = [20 x i8] c"Couldn't get device\00"
define i32* @get_device(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = call i32 @clGetPlatformIDs(i32 0, i32** null, i32* %2)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %50
11: ; preds = %1
%12 = shl i32 %6, 3
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32**
%15 = load i32, i32* %2
%16 = call i32 @clGetPlatformIDs(i32 %15, i32** %14, i32* null)
%17 = bitcast i32* %3 to i8*
%18 = load i32, i32* %2
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %11, %27
%21 = phi i64 [ %28, %27 ], [ 0, %11 ]
%22 = getelementptr inbounds i32*, i32** %14, i64 %21
%23 = load i32*, i32** %22
%24 = call i32 @clGetDeviceIDs(i32* %23, i32 %0, i32 0, i32** null, i32* %3)
%25 = load i32, i32* %3
%26 = icmp sgt i32 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %20
%28 = add nuw nsw i64 %21, 1
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = icmp slt i64 %28, %30
br i1 %31, label %20, label %32
32: ; preds = %27, %11
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %48
35: ; preds = %20
%36 = and i64 %21, 4294967295
%37 = getelementptr inbounds i32*, i32** %14, i64 %36
%38 = load i32*, i32** %37
%39 = shl i32 %25, 3
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i32**
%42 = load i32, i32* %3
%43 = call i32 @clGetDeviceIDs(i32* %38, i32 %0, i32 %42, i32** %41, i32* null)
%44 = load i32*, i32** %41
%45 = call i32 @free(i32** %41)
%46 = call i32 @free(i32** %14)
%47 = call i32 @clError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %43)
br label %48
48: ; preds = %35, %32
%49 = phi i32* [ null, %32 ], [ %44, %35 ]
br label %50
50: ; preds = %48, %8
%51 = phi i32* [ null, %8 ], [ %49, %48 ]
ret i32* %51
}