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

/* Variables and functions */
int /*<<< orphan*/ CL_DEVICE_TYPE_ALL;
int /*<<< orphan*/ clError(char *, int /*<<< orphan*/);
int /*<<< orphan*/ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/,
                                  int /*<<< orphan*/, int /*<<< orphan*/ *,
                                  int *);
int /*<<< orphan*/ clGetPlatformIDs(int, int /*<<< orphan*/ *, int *);
scalar_t__ malloc(int);
void get_n_devices_per_platform(int *n_platforms,
                                int **n_devices_per_platform) {
  cl_int error;
  cl_uint n_platforms_cluint;
  error = clGetPlatformIDs(0, NULL, &n_platforms_cluint);
  *n_platforms = n_platforms_cluint;
  *n_devices_per_platform = (int *)malloc(sizeof(int) * (*n_platforms));
  cl_platform_id *all_platforms =
      (cl_platform_id *)malloc(sizeof(cl_platform_id) * n_platforms_cluint);
  error = clGetPlatformIDs(n_platforms_cluint, all_platforms, NULL);
  cl_uint n_devices;
  for (int i = 0; i < *n_platforms; i++) {
    error = clGetDeviceIDs(all_platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL,
                           &n_devices);
    (*n_devices_per_platform)[i] = n_devices;
  }
  clError("get_n_devices", error);
}


x86 assembly (clang, -O0):
<get_n_devices_per_platform>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1050 <clGetPlatformIDs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <clGetPlatformIDs@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    1208 <get_n_devices_per_platform+0xc8>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%r8
call   1040 <clGetDeviceIDs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b4 <get_n_devices_per_platform+0x74>
mov    -0x14(%rbp),%esi
lea    0xdee(%rip),%rdi
call   1030 <clError@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_n_devices_per_platform>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1050 <clGetPlatformIDs@plt>
mov    (%rsp),%edi
mov    %edi,(%r15)
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
mov    (%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    (%rsp),%edi
mov    %rax,%rsi
xor    %edx,%edx
call   1050 <clGetPlatformIDs@plt>
cmpl   $0x0,(%r15)
jle    11ca <get_n_devices_per_platform+0x8a>
xor    %ebx,%ebx
mov    0x2e50(%rip),%rbp
lea    0x4(%rsp),%r12
nopl   (%rax)
mov    0x0(%r13,%rbx,4),%edi
mov    0x0(%rbp),%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1040 <clGetDeviceIDs@plt>
mov    0x4(%rsp),%ecx
mov    (%r14),%rdx
mov    %ecx,(%rdx,%rbx,4)
add    $0x1,%rbx
movslq (%r15),%rcx
cmp    %rcx,%rbx
jl     11a0 <get_n_devices_per_platform+0x60>
lea    0xe2f(%rip),%rdi
mov    %eax,%esi
call   1030 <clError@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@CL_DEVICE_TYPE_ALL = global i32 0
@.str = [14 x i8] c"get_n_devices\00"
define void @get_n_devices_per_platform(i32* nocapture %0, i32** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = call i32 @clGetPlatformIDs(i32 0, i32* null, i32* %3)
%7 = load i32, i32* %3
store i32 %7, i32* %0
%8 = shl i32 %7, 2
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %1
%11 = load i32, i32* %3
%12 = shl i32 %11, 2
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %3
%16 = call i32 @clGetPlatformIDs(i32 %15, i32* %14, i32* null)
%17 = bitcast i32* %4 to i8*
%18 = load i32, i32* %0
%19 = icmp sgt i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %23, %2
%21 = phi i32 [ %16, %2 ], [ %28, %23 ]
%22 = call i32 @clError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %21)
ret void
23: ; preds = %2, %23
%24 = phi i64 [ %32, %23 ], [ 0, %2 ]
%25 = getelementptr inbounds i32, i32* %14, i64 %24
%26 = load i32, i32* %25
%27 = load i32, i32* @CL_DEVICE_TYPE_ALL
%28 = call i32 @clGetDeviceIDs(i32 %26, i32 %27, i32 0, i32* null, i32* %4)
%29 = load i32, i32* %4
%30 = load i32*, i32** %1
%31 = getelementptr inbounds i32, i32* %30, i64 %24
store i32 %29, i32* %31
%32 = add nuw nsw i64 %24, 1
%33 = load i32, i32* %0
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %32, %34
br i1 %35, label %23, label %20
}