#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_id;

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_NAME;
int /*<<< orphan*/ CL_DEVICE_TYPE_ALL;
int /*<<< orphan*/ CL_PLATFORM_NAME;
int /*<<< orphan*/ CL_PLATFORM_VERSION;
int /*<<< orphan*/ clError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ clGetDeviceInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                   char *, size_t *);
int /*<<< orphan*/ clGetPlatformIDs(int, int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ clGetPlatformInfo(int /*<<< orphan*/, int /*<<< orphan*/,
                                     int, char *, size_t *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void list_all_devices() {
  cl_int error;
  cl_uint n_platforms;
  error = clGetPlatformIDs(0, NULL, &n_platforms);
  if (n_platforms == 0) {
    printf("No OpenCL platforms found\n");
  }
  cl_platform_id *all_platforms =
      (cl_platform_id *)malloc(sizeof(cl_platform_id) * n_platforms);
  error = clGetPlatformIDs(n_platforms, all_platforms, NULL);
  char string[100];
  char string2[100];
  size_t returnSize;
  cl_uint n_devices;
  for (int i = 0; i < n_platforms; i++) {
    clGetPlatformInfo(all_platforms[i], CL_PLATFORM_NAME, 100, string,
                      &returnSize);
    clGetPlatformInfo(all_platforms[i], CL_PLATFORM_VERSION, 100, string2,
                      &returnSize);
    printf("Platform %d: %s (%s)\n", i, string, string2);
    error = clGetDeviceIDs(all_platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL,
                           &n_devices);
    cl_device_id *all_devices =
        (cl_device_id *)malloc(sizeof(cl_device_id) * n_devices);
    error = clGetDeviceIDs(all_platforms[i], CL_DEVICE_TYPE_ALL, n_devices,
                           all_devices, NULL);
    for (int j = 0; j < n_devices; j++) {
      clGetDeviceInfo(all_devices[j], CL_DEVICE_NAME, 100, string, &returnSize);
      printf("\tDevice %d: %s\n", j, string);
    }
    printf("\n");
  }
  clError("list all devices", error);
}


x86 assembly (clang, -O0):
<list_all_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x8(%rbp),%rdx
call   1060 <clGetPlatformIDs@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a5 <list_all_devices+0x35>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <clGetPlatformIDs@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x100(%rbp)
mov    -0x100(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    136d <list_all_devices+0x1fd>
mov    -0x10(%rbp),%rax
movslq -0x100(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rcx
mov    $0x64,%edx
lea    -0xf8(%rbp),%r8
call   1080 <clGetPlatformInfo@plt>
mov    -0x10(%rbp),%rax
movslq -0x100(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    -0xf0(%rbp),%rcx
mov    $0x64,%edx
lea    -0xf8(%rbp),%r8
call   1080 <clGetPlatformInfo@plt>
mov    -0x100(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    -0xf0(%rbp),%rcx
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x100(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xfc(%rbp),%r8
call   1040 <clGetDeviceIDs@plt>
mov    %eax,-0x4(%rbp)
movslq -0xfc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x108(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x100(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    -0xfc(%rbp),%edx
mov    -0x108(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <clGetDeviceIDs@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x10c(%rbp)
mov    -0x10c(%rbp),%eax
cmp    -0xfc(%rbp),%eax
jge    134b <list_all_devices+0x1db>
mov    -0x108(%rbp),%rax
movslq -0x10c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rcx
mov    $0x64,%edx
lea    -0xf8(%rbp),%r8
call   1050 <clGetDeviceInfo@plt>
mov    -0x10c(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    12de <list_all_devices+0x16e>
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x100(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x100(%rbp)
jmp    11d5 <list_all_devices+0x65>
mov    -0x4(%rbp),%esi
lea    0xcca(%rip),%rdi
call   1030 <clError@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_all_devices>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
lea    0xc(%rsp),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1080 <clGetPlatformIDs@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
jne    11c7 <list_all_devices+0x37>
lea    0xe79(%rip),%rdi
call   1050 <puts@plt>
mov    0xc(%rsp),%edi
shl    $0x2,%edi
call   10b0 <malloc@plt>
mov    0xc(%rsp),%edi
mov    %rax,0x18(%rsp)
mov    %rax,%rsi
xor    %edx,%edx
call   1080 <clGetPlatformIDs@plt>
cmpl   $0x0,0xc(%rsp)
jle    133f <list_all_devices+0x1af>
lea    0x28(%rsp),%r15
xor    %ebx,%ebx
jmp    1224 <list_all_devices+0x94>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x20(%rsp),%rbx
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
mov    %rbp,%r15
jge    133b <list_all_devices+0x1ab>
mov    0x18(%rsp),%r14
mov    (%r14,%rbx,4),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    $0x64,%edx
lea    0xa0(%rsp),%r13
mov    %r13,%rcx
mov    %r15,%r8
call   10a0 <clGetPlatformInfo@plt>
mov    (%r14,%rbx,4),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    $0x64,%edx
lea    0x30(%rsp),%r12
mov    %r12,%rcx
mov    %r15,%r8
call   10a0 <clGetPlatformInfo@plt>
lea    0xd89(%rip),%rdi
mov    %ebx,%esi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1090 <printf@plt>
mov    (%r14,%rbx,4),%edi
mov    0x2d57(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1060 <clGetDeviceIDs@plt>
mov    0x10(%rsp),%edi
shl    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
mov    %rbx,0x20(%rsp)
mov    (%r14,%rbx,4),%edi
mov    %r13,%rbx
mov    0x0(%rbp),%esi
mov    0x10(%rsp),%edx
mov    %rax,%rcx
xor    %r8d,%r8d
call   1060 <clGetDeviceIDs@plt>
mov    %eax,0x14(%rsp)
cmpl   $0x0,0x10(%rsp)
mov    0x2cf1(%rip),%r13
mov    %r15,%rbp
lea    0xd2d(%rip),%r15
jle    1200 <list_all_devices+0x70>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12,%r14,4),%edi
mov    0x0(%r13),%esi
mov    $0x64,%edx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1070 <clGetDeviceInfo@plt>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
add    $0x1,%r14
movslq 0x10(%rsp),%rax
cmp    %rax,%r14
jl     1300 <list_all_devices+0x170>
jmp    1200 <list_all_devices+0x70>
mov    0x14(%rsp),%eax
lea    0xce0(%rip),%rdi
mov    %eax,%esi
call   1040 <clError@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"No OpenCL platforms found\0A\00"
@CL_PLATFORM_NAME = global i32 0
@CL_PLATFORM_VERSION = global i32 0
@.str.1 = [22 x i8] c"Platform %d: %s (%s)\0A\00"
@CL_DEVICE_TYPE_ALL = global i32 0
@CL_DEVICE_NAME = global i32 0
@.str.2 = [16 x i8] c"\09Device %d: %s\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [17 x i8] c"list all devices\00"
define void @list_all_devices() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = call i32 @clGetPlatformIDs(i32 0, i32* null, i32* %2)
store i32 %11, i32* %1
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = mul i64 4, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %3
%23 = load i32, i32* %2
%24 = load i32*, i32** %3
%25 = call i32 @clGetPlatformIDs(i32 %23, i32* %24, i32* null)
store i32 %25, i32* %1
store i32 0, i32* %8
br label %26
26: ; preds = %94, %16
%27 = load i32, i32* %8
%28 = load i32, i32* %2
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %97
30: ; preds = %26
%31 = load i32*, i32** %3
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = load i32, i32* @CL_PLATFORM_NAME
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%38 = call i32 @clGetPlatformInfo(i32 %35, i32 %36, i32 100, i8* %37, i64* %6)
%39 = load i32*, i32** %3
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = load i32, i32* @CL_PLATFORM_VERSION
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%46 = call i32 @clGetPlatformInfo(i32 %43, i32 %44, i32 100, i8* %45, i64* %6)
%47 = load i32, i32* %8
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %47, i8* %48, i8* %49)
%51 = load i32*, i32** %3
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = load i32, i32* @CL_DEVICE_TYPE_ALL
%57 = call i32 @clGetDeviceIDs(i32 %55, i32 %56, i32 0, i32* null, i32* %7)
store i32 %57, i32* %1
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = mul i64 4, %59
%61 = trunc i64 %60 to i32
%62 = call i64 @malloc(i32 %61)
%63 = inttoptr i64 %62 to i32*
store i32* %63, i32** %9
%64 = load i32*, i32** %3
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %64, i64 %66
%68 = load i32, i32* %67
%69 = load i32, i32* @CL_DEVICE_TYPE_ALL
%70 = load i32, i32* %7
%71 = load i32*, i32** %9
%72 = call i32 @clGetDeviceIDs(i32 %68, i32 %69, i32 %70, i32* %71, i32* null)
store i32 %72, i32* %1
store i32 0, i32* %10
br label %73
73: ; preds = %89, %30
%74 = load i32, i32* %10
%75 = load i32, i32* %7
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %92
77: ; preds = %73
%78 = load i32*, i32** %9
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %78, i64 %80
%82 = load i32, i32* %81
%83 = load i32, i32* @CL_DEVICE_NAME
%84 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%85 = call i32 @clGetDeviceInfo(i32 %82, i32 %83, i32 100, i8* %84, i64* %6)
%86 = load i32, i32* %10
%87 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %86, i8* %87)
br label %89
89: ; preds = %77
%90 = load i32, i32* %10
%91 = add nsw i32 %90, 1
store i32 %91, i32* %10
br label %73
92: ; preds = %73
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %94
94: ; preds = %92
%95 = load i32, i32* %8
%96 = add nsw i32 %95, 1
store i32 %96, i32* %8
br label %26
97: ; preds = %26
%98 = load i32, i32* %1
%99 = call i32 @clError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %98)
ret void
}

LLVM IR (-O2):
@CL_PLATFORM_NAME = global i32 0
@CL_PLATFORM_VERSION = global i32 0
@.str.1 = [22 x i8] c"Platform %d: %s (%s)\0A\00"
@CL_DEVICE_TYPE_ALL = global i32 0
@CL_DEVICE_NAME = global i32 0
@.str.2 = [16 x i8] c"\09Device %d: %s\0A\00"
@.str.4 = [17 x i8] c"list all devices\00"
@str = [26 x i8] c"No OpenCL platforms found\00"
define void @list_all_devices() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = call i32 @clGetPlatformIDs(i32 0, i32* null, i32* %1)
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* %1
br label %13
13: ; preds = %10, %0
%14 = phi i32 [ %12, %10 ], [ %8, %0 ]
%15 = shl i32 %14, 2
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = load i32, i32* %1
%19 = call i32 @clGetPlatformIDs(i32 %18, i32* %17, i32* null)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%22 = bitcast i64* %4 to i8*
%23 = bitcast i32* %5 to i8*
%24 = load i32, i32* %1
%25 = icmp sgt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %53, %13
%27 = phi i32 [ %19, %13 ], [ %50, %53 ]
%28 = call i32 @clError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %27)
ret void
29: ; preds = %13, %53
%30 = phi i64 [ %55, %53 ], [ 0, %13 ]
%31 = getelementptr inbounds i32, i32* %17, i64 %30
%32 = load i32, i32* %31
%33 = load i32, i32* @CL_PLATFORM_NAME
%34 = call i32 @clGetPlatformInfo(i32 %32, i32 %33, i32 100, i8* %20, i64* %4)
%35 = load i32, i32* %31
%36 = load i32, i32* @CL_PLATFORM_VERSION
%37 = call i32 @clGetPlatformInfo(i32 %35, i32 %36, i32 100, i8* %21, i64* %4)
%38 = trunc i64 %30 to i32
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %38, i8* %20, i8* %21)
%40 = load i32, i32* %31
%41 = load i32, i32* @CL_DEVICE_TYPE_ALL
%42 = call i32 @clGetDeviceIDs(i32 %40, i32 %41, i32 0, i32* null, i32* %5)
%43 = load i32, i32* %5
%44 = shl i32 %43, 2
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i32*
%47 = load i32, i32* %31
%48 = load i32, i32* @CL_DEVICE_TYPE_ALL
%49 = load i32, i32* %5
%50 = call i32 @clGetDeviceIDs(i32 %47, i32 %48, i32 %49, i32* %46, i32* null)
%51 = load i32, i32* %5
%52 = icmp sgt i32 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %59, %29
%54 = call i32 @putchar(i32 10)
%55 = add nuw nsw i64 %30, 1
%56 = load i32, i32* %1
%57 = sext i32 %56 to i64
%58 = icmp slt i64 %55, %57
br i1 %58, label %29, label %26
59: ; preds = %29, %59
%60 = phi i64 [ %67, %59 ], [ 0, %29 ]
%61 = getelementptr inbounds i32, i32* %46, i64 %60
%62 = load i32, i32* %61
%63 = load i32, i32* @CL_DEVICE_NAME
%64 = call i32 @clGetDeviceInfo(i32 %62, i32 %63, i32 100, i8* %20, i64* %4)
%65 = trunc i64 %60 to i32
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %65, i8* %20)
%67 = add nuw nsw i64 %60, 1
%68 = load i32, i32* %5
%69 = sext i32 %68 to i64
%70 = icmp slt i64 %67, %69
br i1 %70, label %59, label %53
}