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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_NAME;
int /*<<< orphan*/ CL_DEVICE_TYPE_ALL;
int /*<<< orphan*/ CL_PLATFORM_NAME;
scalar_t__ CL_SUCCESS;
scalar_t__ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ **, int *);
scalar_t__ clGetDeviceInfo(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                           char *, int /*<<< orphan*/ *);
scalar_t__ clGetPlatformIDs(int, int /*<<< orphan*/ *, int *);
scalar_t__ clGetPlatformInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                             char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
cl_device_id retrieve_device(char *platform_vender) {
  cl_int err;
  cl_device_id device_id = NULL;
  cl_platform_id platform_ids[4];
  cl_uint ret_num_devices = 0;
  cl_uint ret_num_platforms = 0;
  err = clGetPlatformIDs(4, platform_ids, &ret_num_platforms);
  if (err != CL_SUCCESS) {
    printf("%s %d\n", "Error: Failed to create a platform!", err);
    return NULL;
  }
  printf("There are %d platforms totally.\n", ret_num_platforms);
  int i;
  for (i = 0; i < ret_num_platforms; ++i) {
    cl_platform_id platform_id = platform_ids[i];
    char platform_name[1024];
    err = clGetPlatformInfo(platform_id, CL_PLATFORM_NAME, 1024, platform_name,
                            NULL);
    if (strcmp(platform_vender, platform_name) < 0) {
      err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 4, &device_id,
                           &ret_num_devices);
      if (err != CL_SUCCESS) {
        printf("%s %d\n", "Error: Failed to create a device group!", err);
        return NULL;
      }
      char device_name[1024];
      err = clGetDeviceInfo(device_id, CL_DEVICE_NAME, 1024, device_name, NULL);
      printf("Get device \"%s\" in platform \"%s\".\n", device_name,
             platform_name);
      return device_id;
    }
  }
  return device_id;
}


x86 assembly (clang, -O0):
<retrieve_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rsi
mov    $0x4,%edi
lea    -0x38(%rbp),%rdx
call   1050 <clGetPlatformIDs@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
je     11d5 <retrieve_device+0x75>
mov    -0x18(%rbp),%rdx
lea    0xe46(%rip),%rdi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130b <retrieve_device+0x1ab>
mov    -0x38(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    1303 <retrieve_device+0x1a3>
movslq -0x3c(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    -0x440(%rbp),%rcx
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <clGetPlatformInfo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jge    12f0 <retrieve_device+0x190>
mov    -0x40(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
lea    -0x20(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1030 <clGetDeviceIDs@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
je     12a0 <retrieve_device+0x140>
mov    -0x18(%rbp),%rdx
lea    0xd7b(%rip),%rdi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    130b <retrieve_device+0x1ab>
mov    -0x20(%rbp),%rdi
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    -0x840(%rbp),%rcx
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <clGetDeviceInfo@plt>
mov    %rax,-0x18(%rbp)
lea    -0x840(%rbp),%rsi
lea    -0x440(%rbp),%rdx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    130b <retrieve_device+0x1ab>
jmp    12f5 <retrieve_device+0x195>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ed <retrieve_device+0x8d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrieve_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %rdi,%r15
movq   $0x0,0x10(%rsp)
movl   $0x0,0x1c(%rsp)
movl   $0x0,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    $0x4,%edi
call   1050 <clGetPlatformIDs@plt>
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
jne    121b <retrieve_device+0xbb>
mov    0xc(%rsp),%esi
lea    0xe73(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1236 <retrieve_device+0xd6>
xor    %r14d,%r14d
mov    0x2e05(%rip),%r13
lea    0x30(%rsp),%rbx
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    0x20(%rsp,%r12,4),%ebp
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    $0x400,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1070 <clGetPlatformInfo@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
js     124b <retrieve_device+0xeb>
add    $0x1,%r12
movslq 0xc(%rsp),%rax
cmp    %rax,%r12
jl     11e0 <retrieve_device+0x80>
jmp    1236 <retrieve_device+0xd6>
lea    0xdde(%rip),%rdi
lea    0xdde(%rip),%rsi
xor    %r14d,%r14d
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rax
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rcx
lea    0x1c(%rsp),%r8
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <clGetDeviceIDs@plt>
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
jne    12bc <retrieve_device+0x15c>
mov    0x10(%rsp),%rdi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    0x430(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1040 <clGetDeviceInfo@plt>
lea    0xdd1(%rip),%rdi
lea    0x30(%rsp),%rdx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%r14
jmp    1236 <retrieve_device+0xd6>
lea    0xd3d(%rip),%rdi
lea    0xd82(%rip),%rsi
jmp    1229 <retrieve_device+0xc9>

LLVM IR (-O0):
@CL_SUCCESS = global i64 0
@.str = [7 x i8] c"%s %d\0A\00"
@.str.1 = [36 x i8] c"Error: Failed to create a platform!\00"
@.str.2 = [33 x i8] c"There are %d platforms totally.\0A\00"
@CL_PLATFORM_NAME = global i32 0
@CL_DEVICE_TYPE_ALL = global i32 0
@.str.3 = [40 x i8] c"Error: Failed to create a device group!\00"
@CL_DEVICE_NAME = global i32 0
@.str.4 = [35 x i8] c"Get device \22%s\22 in platform \22%s\22.\0A\00"
define i32* @retrieve_device(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca [4 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32* null, i32** %5
store i32 0, i32* %7
store i32 0, i32* %8
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%14 = call i64 @clGetPlatformIDs(i32 4, i32* %13, i32* %8)
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = load i64, i64* @CL_SUCCESS
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i64, i64* %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %19)
store i32* null, i32** %2
br label %66
21: ; preds = %1
%22 = load i32, i32* %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 0, i32* %9
br label %24
24: ; preds = %61, %21
%25 = load i32, i32* %9
%26 = load i32, i32* %8
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %64
28: ; preds = %24
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 %30
%32 = load i32, i32* %31
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = load i32, i32* @CL_PLATFORM_NAME
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%36 = call i64 @clGetPlatformInfo(i32 %33, i32 %34, i32 1024, i8* %35, i32* null)
store i64 %36, i64* %4
%37 = load i8*, i8** %3
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%39 = call i64 @strcmp(i8* %37, i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %60
41: ; preds = %28
%42 = load i32, i32* %10
%43 = load i32, i32* @CL_DEVICE_TYPE_ALL
%44 = call i64 @clGetDeviceIDs(i32 %42, i32 %43, i32 4, i32** %5, i32* %7)
store i64 %44, i64* %4
%45 = load i64, i64* %4
%46 = load i64, i64* @CL_SUCCESS
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %41
%49 = load i64, i64* %4
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i64 %49)
store i32* null, i32** %2
br label %66
51: ; preds = %41
%52 = load i32*, i32** %5
%53 = load i32, i32* @CL_DEVICE_NAME
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%55 = call i64 @clGetDeviceInfo(i32* %52, i32 %53, i32 1024, i8* %54, i32* null)
store i64 %55, i64* %4
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %56, i8* %57)
%59 = load i32*, i32** %5
store i32* %59, i32** %2
br label %66
60: ; preds = %28
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %24
64: ; preds = %24
%65 = load i32*, i32** %5
store i32* %65, i32** %2
br label %66
66: ; preds = %64, %51, %48, %18
%67 = load i32*, i32** %2
ret i32* %67
}

LLVM IR (-O2):
@CL_SUCCESS = global i64 0
@.str = [7 x i8] c"%s %d\0A\00"
@.str.1 = [36 x i8] c"Error: Failed to create a platform!\00"
@.str.2 = [33 x i8] c"There are %d platforms totally.\0A\00"
@CL_PLATFORM_NAME = global i32 0
@CL_DEVICE_TYPE_ALL = global i32 0
@.str.3 = [40 x i8] c"Error: Failed to create a device group!\00"
@CL_DEVICE_NAME = global i32 0
@.str.4 = [35 x i8] c"Get device \22%s\22 in platform \22%s\22.\0A\00"
define i32* @retrieve_device(i8* %0) {
%2 = alloca i32*
%3 = alloca [4 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = bitcast i32** %2 to i8*
store i32* null, i32** %2
%9 = bitcast [4 x i32]* %3 to i8*
%10 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%11 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%13 = call i64 @clGetPlatformIDs(i32 4, i32* %12, i32* %5)
%14 = load i64, i64* @CL_SUCCESS
%15 = icmp eq i64 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %13)
br label %54
18: ; preds = %1
%19 = load i32, i32* %5
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %54
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
br label %25
25: ; preds = %23, %49
%26 = phi i64 [ 0, %23 ], [ %50, %49 ]
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = load i32, i32* @CL_PLATFORM_NAME
%30 = call i64 @clGetPlatformInfo(i32 %28, i32 %29, i32 1024, i8* %24, i32* null)
%31 = call i64 @strcmp(i8* %0, i8* %24)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %49
33: ; preds = %25
%34 = load i32, i32* @CL_DEVICE_TYPE_ALL
%35 = call i64 @clGetDeviceIDs(i32 %28, i32 %34, i32 4, i32** %2, i32* %4)
%36 = load i64, i64* @CL_SUCCESS
%37 = icmp eq i64 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i64 %35)
br label %47
40: ; preds = %33
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%42 = load i32*, i32** %2
%43 = load i32, i32* @CL_DEVICE_NAME
%44 = call i64 @clGetDeviceInfo(i32* %42, i32 %43, i32 1024, i8* %41, i32* null)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %41, i8* %24)
%46 = load i32*, i32** %2
br label %47
47: ; preds = %40, %38
%48 = phi i32* [ null, %38 ], [ %46, %40 ]
br label %54
49: ; preds = %25
%50 = add nuw nsw i64 %26, 1
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = icmp slt i64 %50, %52
br i1 %53, label %25, label %54
54: ; preds = %49, %18, %47, %16
%55 = phi i32* [ null, %16 ], [ %48, %47 ], [ null, %18 ], [ null, %49 ]
ret i32* %55
}