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

/* Variables and functions */
scalar_t__ CL_DEVICE_NOT_FOUND;
int /*<<< orphan*/ CL_DEVICE_TYPE_CPU;
int /*<<< orphan*/ CL_DEVICE_TYPE_GPU;
scalar_t__ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ clGetPlatformIDs(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
cl_device_id create_device() {
  cl_platform_id platform;
  cl_device_id dev;
  cl_int err;
  err = clGetPlatformIDs(1, &platform, NULL);
  if (err < 0) {
    perror("Couldn't identify a platform");
    exit(1);
  }
  err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL);
  if (err == CL_DEVICE_NOT_FOUND) {
    printf("GPU not found\n");
    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL);
  }
  if (err < 0) {
    perror("Couldn't access any devices");
    exit(1);
  }
  return dev;
}


x86 assembly (clang, -O0):
<create_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x1,%edi
lea    -0x4(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <clGetPlatformIDs@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118f <create_device+0x3f>
lea    0xe80(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
lea    -0x8(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetDeviceIDs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <create_device+0xa5>
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
lea    -0x8(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetDeviceIDs@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1216 <create_device+0xc6>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_device>:
push   %rax
lea    0x4(%rsp),%rsi
mov    $0x1,%edi
xor    %edx,%edx
call   1050 <clGetPlatformIDs@plt>
test   %rax,%rax
js     11c3 <create_device+0x73>
mov    0x4(%rsp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
mov    $0x1,%edx
xor    %r8d,%r8d
call   1040 <clGetDeviceIDs@plt>
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <create_device+0x69>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
mov    $0x1,%edx
xor    %r8d,%r8d
call   1040 <clGetDeviceIDs@plt>
test   %rax,%rax
js     11cc <create_device+0x7c>
mov    (%rsp),%eax
pop    %rcx
ret
lea    0xe36(%rip),%rdi
jmp    11d3 <create_device+0x83>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Couldn't identify a platform\00"
@CL_DEVICE_TYPE_GPU = global i32 0
@CL_DEVICE_NOT_FOUND = global i64 0
@.str.1 = [15 x i8] c"GPU not found\0A\00"
@CL_DEVICE_TYPE_CPU = global i32 0
@.str.2 = [28 x i8] c"Couldn't access any devices\00"
define i32 @create_device() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = call i64 @clGetPlatformIDs(i32 1, i32* %1, i32* null)
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %1
%12 = load i32, i32* @CL_DEVICE_TYPE_GPU
%13 = call i64 @clGetDeviceIDs(i32 %11, i32 %12, i32 1, i32* %2, i32* null)
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = load i64, i64* @CL_DEVICE_NOT_FOUND
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %1
%20 = load i32, i32* @CL_DEVICE_TYPE_CPU
%21 = call i64 @clGetDeviceIDs(i32 %19, i32 %20, i32 1, i32* %2, i32* null)
store i64 %21, i64* %3
br label %22
22: ; preds = %17, %10
%23 = load i64, i64* %3
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [29 x i8] c"Couldn't identify a platform\00"
@CL_DEVICE_TYPE_GPU = global i32 0
@CL_DEVICE_NOT_FOUND = global i64 0
@CL_DEVICE_TYPE_CPU = global i32 0
@.str.2 = [28 x i8] c"Couldn't access any devices\00"
@str = [14 x i8] c"GPU not found\00"
define i32 @create_device() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i64 @clGetPlatformIDs(i32 1, i32* %1, i32* null)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %1
%12 = load i32, i32* @CL_DEVICE_TYPE_GPU
%13 = call i64 @clGetDeviceIDs(i32 %11, i32 %12, i32 1, i32* %2, i32* null)
%14 = load i64, i64* @CL_DEVICE_NOT_FOUND
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* %1
%19 = load i32, i32* @CL_DEVICE_TYPE_CPU
%20 = call i64 @clGetDeviceIDs(i32 %18, i32 %19, i32 1, i32* %2, i32* null)
br label %21
21: ; preds = %16, %10
%22 = phi i64 [ %20, %16 ], [ %13, %10 ]
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = load i32, i32* %2
ret i32 %28
}