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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_TYPE_GPU;
int CL_SUCCESS;
int clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                   int /*<<< orphan*/ *, int *);
int clGetPlatformIDs(int, int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ stderr;
cl_int find_first_gpu(cl_device_id *dev) {
  if (dev == NULL)
    return -1;
  cl_platform_id platforms[10];
  cl_uint num_platforms;
  ({
    cl_int ret = clGetPlatformIDs((sizeof(platforms) / sizeof(*platforms)),
                                  platforms, &num_platforms);
    if (ret != CL_SUCCESS) {
      if ("getting platform ids"[0] != 0)
        fprintf(stderr, "Error %s: %d\n", "getting platform ids", ret);
      return -1;
    }
  });
  for (int i = 0; i < num_platforms; i++) {
    cl_device_id devices[10];
    cl_uint num_devices;
    cl_int ret = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU,
                                (sizeof(devices) / sizeof(*devices)), devices,
                                &num_devices);
    if (ret != CL_SUCCESS)
      continue;
    if (num_devices >= 1) {
      *dev = devices[0];
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_first_gpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1156 <find_first_gpu+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <find_first_gpu+0x10d>
lea    -0x40(%rbp),%rsi
mov    $0xa,%edi
lea    -0x44(%rbp),%rdx
call   1040 <clGetPlatformIDs@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
je     11a8 <find_first_gpu+0x78>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%ecx
lea    0xe70(%rip),%rsi
lea    0xe77(%rip),%rdx
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <find_first_gpu+0x10d>
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1236 <find_first_gpu+0x106>
movslq -0x4c(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rcx
mov    $0xa,%edx
lea    -0x84(%rbp),%r8
call   1030 <clGetDeviceIDs@plt>
mov    %eax,-0x88(%rbp)
mov    -0x88(%rbp),%eax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%eax
je     1201 <find_first_gpu+0xd1>
jmp    1228 <find_first_gpu+0xf8>
cmpl   $0x1,-0x84(%rbp)
jl     1223 <find_first_gpu+0xf3>
mov    -0x80(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    123d <find_first_gpu+0x10d>
jmp    1228 <find_first_gpu+0xf8>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11af <find_first_gpu+0x7f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_gpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
test   %rdi,%rdi
je     11fd <find_first_gpu+0xcd>
mov    %rdi,%rbp
lea    0x40(%rsp),%rsi
mov    %rsp,%rdx
mov    $0xa,%edi
call   1040 <clGetPlatformIDs@plt>
mov    0x2e6d(%rip),%rbx
cmp    (%rbx),%eax
jne    11df <find_first_gpu+0xaf>
mov    $0xffffffff,%r15d
cmpl   $0x0,(%rsp)
jle    1203 <find_first_gpu+0xd3>
mov    %rbp,0x8(%rsp)
xor    %ebp,%ebp
mov    0x2e63(%rip),%r14
lea    0x10(%rsp),%r12
lea    0x4(%rsp),%r13
jmp    11ad <find_first_gpu+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
movslq (%rsp),%rax
cmp    %rax,%rbp
jge    1203 <find_first_gpu+0xd3>
mov    0x40(%rsp,%rbp,4),%edi
mov    (%r14),%esi
mov    $0xa,%edx
mov    %r12,%rcx
mov    %r13,%r8
call   1030 <clGetDeviceIDs@plt>
cmp    (%rbx),%eax
jne    11a0 <find_first_gpu+0x70>
cmpl   $0x0,0x4(%rsp)
jle    11a0 <find_first_gpu+0x70>
mov    0x10(%rsp),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
xor    %r15d,%r15d
jmp    1203 <find_first_gpu+0xd3>
mov    0x2df2(%rip),%rcx
mov    (%rcx),%edi
lea    0xe11(%rip),%rsi
lea    0xe18(%rip),%rdx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@CL_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"Error %s: %d\0A\00"
@.str.1 = [21 x i8] c"getting platform ids\00"
@CL_DEVICE_TYPE_GPU = global i32 0
define i32 @find_first_gpu(i32* writeonly %0) {
%2 = alloca [10 x i32]
%3 = alloca i32
%4 = alloca [10 x i32]
%5 = alloca i32
%6 = icmp eq i32* %0, null
br i1 %6, label %44, label %7
7: ; preds = %1
%8 = bitcast [10 x i32]* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%11 = call i32 @clGetPlatformIDs(i32 10, i32* %10, i32* %3)
%12 = load i32, i32* @CL_SUCCESS
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %42
17: ; preds = %14
%18 = bitcast [10 x i32]* %4 to i8*
%19 = bitcast i32* %5 to i8*
%20 = getelementptr inbounds [10 x i32], [10 x i32]* %4, i64 0, i64 0
br label %24
21: ; preds = %7
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %42
24: ; preds = %17, %35
%25 = phi i64 [ 0, %17 ], [ %36, %35 ]
%26 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %25
%27 = load i32, i32* %26
%28 = load i32, i32* @CL_DEVICE_TYPE_GPU
%29 = call i32 @clGetDeviceIDs(i32 %27, i32 %28, i32 10, i32* %20, i32* %5)
%30 = load i32, i32* @CL_SUCCESS
%31 = icmp eq i32 %29, %30
%32 = load i32, i32* %5
%33 = icmp sgt i32 %32, 0
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %40, label %35
35: ; preds = %24
%36 = add nuw nsw i64 %25, 1
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %36, %38
br i1 %39, label %24, label %42
40: ; preds = %24
%41 = load i32, i32* %20
store i32 %41, i32* %0
br label %42
42: ; preds = %35, %14, %40, %21
%43 = phi i32 [ -1, %21 ], [ 0, %40 ], [ -1, %14 ], [ -1, %35 ]
br label %44
44: ; preds = %1, %42
%45 = phi i32 [ %43, %42 ], [ -1, %1 ]
ret i32 %45
}