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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_NAME;
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, int device_id,
                             cl_device_type type) {
  cl_int err;
  cl_device_id device_ids[4];
  cl_platform_id platform_ids[4];
  cl_uint ret_num_devices = 0;
  cl_uint ret_num_platforms = 0;
  int default_device_id = 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, type, 4, device_ids, &ret_num_devices);
      if (err != CL_SUCCESS) {
        printf("%s %d\n", "Error: Failed to create a device group!", err);
        return NULL;
      }
      int j = 0;
      for (j = 0; j < ret_num_devices; ++j) {
        char device_name[1024];
        err = clGetDeviceInfo(device_ids[j], CL_DEVICE_NAME, 1024, device_name,
                              NULL);
        if (j == device_id) {
          printf("Get device \"%s\" in platform \"%s\".\n", device_name,
                 platform_name);
          return device_ids[device_id];
        }
      }
    }
  }
  return device_ids[default_device_id];
  ;
}


x86 assembly (clang, -O0):
<retrieve_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x880,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
lea    -0x50(%rbp),%rsi
mov    $0x4,%edi
lea    -0x58(%rbp),%rdx
call   1050 <clGetPlatformIDs@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11da <retrieve_device+0x7a>
mov    -0x20(%rbp),%rdx
lea    0xe41(%rip),%rdi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    136c <retrieve_device+0x20c>
mov    -0x58(%rbp),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x58(%rbp),%eax
jge    135f <retrieve_device+0x1ff>
movslq -0x60(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    -0x470(%rbp),%rcx
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <clGetPlatformInfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x470(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jge    134c <retrieve_device+0x1ec>
mov    -0x64(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x40(%rbp),%rcx
mov    $0x4,%edx
lea    -0x54(%rbp),%r8
call   1030 <clGetDeviceIDs@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%rax
je     129f <retrieve_device+0x13f>
mov    -0x20(%rbp),%rdx
lea    0xd7c(%rip),%rdi
lea    0xdc1(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    136c <retrieve_device+0x20c>
movl   $0x0,-0x474(%rbp)
movl   $0x0,-0x474(%rbp)
mov    -0x474(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1347 <retrieve_device+0x1e7>
movslq -0x474(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
lea    -0x880(%rbp),%rcx
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <clGetDeviceInfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x474(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    132e <retrieve_device+0x1ce>
lea    -0x880(%rbp),%rsi
lea    -0x470(%rbp),%rdx
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x14(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rax
mov    %rax,-0x8(%rbp)
jmp    136c <retrieve_device+0x20c>
jmp    1333 <retrieve_device+0x1d3>
mov    -0x474(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x474(%rbp)
jmp    12b3 <retrieve_device+0x153>
jmp    134c <retrieve_device+0x1ec>
jmp    1351 <retrieve_device+0x1f1>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    11f2 <retrieve_device+0x92>
movslq -0x5c(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrieve_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x858,%rsp
mov    %edx,0x14(%rsp)
mov    %esi,%ebx
mov    %rdi,0x18(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x4,%edi
call   1050 <clGetPlatformIDs@plt>
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <retrieve_device+0x150>
mov    0x8(%rsp),%esi
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    12f3 <retrieve_device+0x193>
mov    %ebx,0x10(%rsp)
mov    %ebx,%r14d
xor    %r15d,%r15d
mov    0x2dfa(%rip),%rbp
lea    0x50(%rsp),%r12
jmp    1202 <retrieve_device+0xa2>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r15
movslq 0x8(%rsp),%rax
cmp    %rax,%r15
jge    1308 <retrieve_device+0x1a8>
mov    0x20(%rsp,%r15,4),%r13d
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    $0x400,%edx
lea    0x450(%rsp),%rbx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1070 <clGetPlatformInfo@plt>
mov    0x18(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jns    11f0 <retrieve_device+0x90>
mov    %r13d,%edi
mov    0x14(%rsp),%esi
mov    $0x4,%edx
lea    0x30(%rsp),%rcx
lea    0xc(%rsp),%r8
call   1030 <clGetDeviceIDs@plt>
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    130f <retrieve_device+0x1af>
cmpl   $0x0,0xc(%rsp)
jle    11f0 <retrieve_device+0x90>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x30(%rsp,%rbx,8),%rdi
mov    0x0(%rbp),%esi
mov    $0x400,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1040 <clGetDeviceInfo@plt>
cmp    %rbx,%r14
je     12cc <retrieve_device+0x16c>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1280 <retrieve_device+0x120>
jmp    11f0 <retrieve_device+0x90>
lea    0xd49(%rip),%rdi
lea    0xd49(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12f3 <retrieve_device+0x193>
lea    0xda1(%rip),%rdi
lea    0x50(%rsp),%rsi
lea    0x450(%rsp),%rdx
xor    %eax,%eax
call   1060 <printf@plt>
movslq 0x10(%rsp),%rax
mov    0x30(%rsp,%rax,8),%rbx
jmp    12f3 <retrieve_device+0x193>
mov    %rbx,%rax
add    $0x858,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x30(%rsp),%rbx
jmp    12f3 <retrieve_device+0x193>
lea    0xcea(%rip),%rdi
lea    0xd2f(%rip),%rsi
jmp    12be <retrieve_device+0x15e>

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
@.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, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca [4 x i32*]
%10 = alloca [4 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [1024 x i8]
%17 = alloca i32
%18 = alloca [1024 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 0
%20 = call i64 @clGetPlatformIDs(i32 4, i32* %19, i32* %12)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = load i64, i64* @CL_SUCCESS
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %3
%25 = load i64, i64* %8
%26 = 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 %25)
store i32* null, i32** %4
br label %96
27: ; preds = %3
%28 = load i32, i32* %12
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 0, i32* %14
br label %30
30: ; preds = %88, %27
%31 = load i32, i32* %14
%32 = load i32, i32* %12
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %91
34: ; preds = %30
%35 = load i32, i32* %14
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %10, i64 0, i64 %36
%38 = load i32, i32* %37
store i32 %38, i32* %15
%39 = load i32, i32* %15
%40 = load i32, i32* @CL_PLATFORM_NAME
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%42 = call i64 @clGetPlatformInfo(i32 %39, i32 %40, i32 1024, i8* %41, i32* null)
store i64 %42, i64* %8
%43 = load i8*, i8** %5
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%45 = call i64 @strcmp(i8* %43, i8* %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %87
47: ; preds = %34
%48 = load i32, i32* %15
%49 = load i32, i32* %7
%50 = getelementptr inbounds [4 x i32*], [4 x i32*]* %9, i64 0, i64 0
%51 = call i64 @clGetDeviceIDs(i32 %48, i32 %49, i32 4, i32** %50, i32* %11)
store i64 %51, i64* %8
%52 = load i64, i64* %8
%53 = load i64, i64* @CL_SUCCESS
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %47
%56 = load i64, i64* %8
%57 = 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 %56)
store i32* null, i32** %4
br label %96
58: ; preds = %47
store i32 0, i32* %17
store i32 0, i32* %17
br label %59
59: ; preds = %83, %58
%60 = load i32, i32* %17
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %86
63: ; preds = %59
%64 = load i32, i32* %17
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [4 x i32*], [4 x i32*]* %9, i64 0, i64 %65
%67 = load i32*, i32** %66
%68 = load i32, i32* @CL_DEVICE_NAME
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%70 = call i64 @clGetDeviceInfo(i32* %67, i32 %68, i32 1024, i8* %69, i32* null)
store i64 %70, i64* %8
%71 = load i32, i32* %17
%72 = load i32, i32* %6
%73 = icmp eq i32 %71, %72
br i1 %73, label %74, label %82
74: ; preds = %63
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %75, i8* %76)
%78 = load i32, i32* %6
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [4 x i32*], [4 x i32*]* %9, i64 0, i64 %79
%81 = load i32*, i32** %80
store i32* %81, i32** %4
br label %96
82: ; preds = %63
br label %83
83: ; preds = %82
%84 = load i32, i32* %17
%85 = add nsw i32 %84, 1
store i32 %85, i32* %17
br label %59
86: ; preds = %59
br label %87
87: ; preds = %86, %34
br label %88
88: ; preds = %87
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %30
91: ; preds = %30
%92 = load i32, i32* %13
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [4 x i32*], [4 x i32*]* %9, i64 0, i64 %93
%95 = load i32*, i32** %94
store i32* %95, i32** %4
br label %96
96: ; preds = %91, %74, %55, %24
%97 = load i32*, i32** %4
ret i32* %97
}

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
@.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, i32 %1, i32 %2) {
%4 = alloca [4 x i32*]
%5 = alloca [4 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = bitcast [4 x i32*]* %4 to i8*
%11 = bitcast [4 x i32]* %5 to i8*
%12 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%13 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
%15 = call i64 @clGetPlatformIDs(i32 4, i32* %14, i32* %7)
%16 = load i64, i64* @CL_SUCCESS
%17 = icmp eq i64 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %3
%19 = 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 %15)
br label %73
20: ; preds = %3
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %73
25: ; preds = %20
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%27 = getelementptr inbounds [4 x i32*], [4 x i32*]* %4, i64 0, i64 0
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = zext i32 %1 to i64
br label %30
30: ; preds = %25, %66
%31 = phi i64 [ 0, %25 ], [ %67, %66 ]
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = load i32, i32* @CL_PLATFORM_NAME
%35 = call i64 @clGetPlatformInfo(i32 %33, i32 %34, i32 1024, i8* %26, i32* null)
%36 = call i64 @strcmp(i8* %0, i8* %26)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %66
38: ; preds = %30
%39 = call i64 @clGetDeviceIDs(i32 %33, i32 %2, i32 4, i32** %27, i32* %6)
%40 = load i64, i64* @CL_SUCCESS
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %6
%44 = icmp sgt i32 %43, 0
br i1 %44, label %47, label %66
45: ; preds = %38
%46 = 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 %39)
br label %64
47: ; preds = %42, %59
%48 = phi i64 [ %60, %59 ], [ 0, %42 ]
%49 = getelementptr inbounds [4 x i32*], [4 x i32*]* %4, i64 0, i64 %48
%50 = load i32*, i32** %49
%51 = load i32, i32* @CL_DEVICE_NAME
%52 = call i64 @clGetDeviceInfo(i32* %50, i32 %51, i32 1024, i8* %28, i32* null)
%53 = icmp eq i64 %48, %29
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %28, i8* %26)
%56 = sext i32 %1 to i64
%57 = getelementptr inbounds [4 x i32*], [4 x i32*]* %4, i64 0, i64 %56
%58 = load i32*, i32** %57
br label %64
59: ; preds = %47
%60 = add nuw nsw i64 %48, 1
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = icmp slt i64 %60, %62
br i1 %63, label %47, label %66
64: ; preds = %54, %45
%65 = phi i32* [ null, %45 ], [ %58, %54 ]
br label %73
66: ; preds = %59, %42, %30
%67 = add nuw nsw i64 %31, 1
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = icmp slt i64 %67, %69
br i1 %70, label %30, label %71
71: ; preds = %66
%72 = load i32*, i32** %27
br label %73
73: ; preds = %20, %71, %64, %18
%74 = phi i32* [ null, %18 ], [ %65, %64 ], [ %72, %71 ], [ undef, %20 ]
ret i32* %74
}