#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 size_t cl_uint;
typedef int /*<<< orphan*/ cl_program;
typedef int /*<<< orphan*/ *****cl_platform_id;
typedef int /*<<< orphan*/ ****cl_device_id;
typedef int /*<<< orphan*/ cl_context;
typedef int /*<<< orphan*/ cl_command_queue;

/* Variables and functions */
int /*<<< orphan*/ clGetPlatformIDs(int /*<<< orphan*/, int /*<<< orphan*/ *,
                                    size_t *);
int /*<<< orphan*/ clReleaseCommandQueue(int /*<<< orphan*/);
int /*<<< orphan*/ clReleaseContext(int /*<<< orphan*/);
int /*<<< orphan*/ clReleaseProgram(int /*<<< orphan*/);
int /*<<< orphan*/ free(int /*<<< orphan*/ ******);
void CleanUpCLEnvironment(cl_platform_id **platform, cl_device_id ***device_id,
                          cl_context *context, cl_command_queue *queue,
                          cl_program *program) {
  clReleaseProgram(*program);
  clReleaseCommandQueue(*queue);
  clReleaseContext(*context);
  cl_uint numPlatforms;
  clGetPlatformIDs(0, NULL, &numPlatforms);
  for (cl_uint i = 0; i < numPlatforms; i++) {
    free((*device_id)[i]);
  }
  free(*platform);
  free(*device_id);
}


x86 assembly (clang, -O0):
<CleanUpCLEnvironment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <clReleaseProgram@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <clReleaseCommandQueue@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <clReleaseContext@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1050 <clGetPlatformIDs@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11d7 <CleanUpCLEnvironment+0x87>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11a4 <CleanUpCLEnvironment+0x54>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CleanUpCLEnvironment>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    (%r8),%edi
call   1040 <clReleaseProgram@plt>
mov    (%r15),%edi
call   1060 <clReleaseCommandQueue@plt>
mov    (%r12),%edi
call   1070 <clReleaseContext@plt>
lea    0x8(%rsp),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1050 <clGetPlatformIDs@plt>
cmpq   $0x0,0x8(%rsp)
je     11b8 <CleanUpCLEnvironment+0x68>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%rax
mov    (%rax,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    0x8(%rsp),%rbx
jb     11a0 <CleanUpCLEnvironment+0x50>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x0(%r13),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @CleanUpCLEnvironment(i32******* %0, i32******* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32*******
%7 = alloca i32*******
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
store i32******* %0, i32******** %6
store i32******* %1, i32******** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%13 = load i32*, i32** %10
%14 = load i32, i32* %13
%15 = call i32 @clReleaseProgram(i32 %14)
%16 = load i32*, i32** %9
%17 = load i32, i32* %16
%18 = call i32 @clReleaseCommandQueue(i32 %17)
%19 = load i32*, i32** %8
%20 = load i32, i32* %19
%21 = call i32 @clReleaseContext(i32 %20)
%22 = call i32 @clGetPlatformIDs(i32 0, i32* null, i64* %11)
store i64 0, i64* %12
br label %23
23: ; preds = %35, %5
%24 = load i64, i64* %12
%25 = load i64, i64* %11
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i32*******, i32******** %7
%29 = load i32******, i32******* %28
%30 = load i64, i64* %12
%31 = getelementptr inbounds i32*****, i32****** %29, i64 %30
%32 = load i32*****, i32****** %31
%33 = bitcast i32***** %32 to i32******
%34 = call i32 @free(i32****** %33)
br label %35
35: ; preds = %27
%36 = load i64, i64* %12
%37 = add i64 %36, 1
store i64 %37, i64* %12
br label %23
38: ; preds = %23
%39 = load i32*******, i32******** %6
%40 = load i32******, i32******* %39
%41 = call i32 @free(i32****** %40)
%42 = load i32*******, i32******** %7
%43 = load i32******, i32******* %42
%44 = call i32 @free(i32****** %43)
ret void
}

LLVM IR (-O2):
define void @CleanUpCLEnvironment(i32******* nocapture %0, i32******* nocapture %1, i32* nocapture %2, i32* nocapture %3, i32* nocapture %4) {
%6 = alloca i64
%7 = load i32, i32* %4
%8 = tail call i32 @clReleaseProgram(i32 %7)
%9 = load i32, i32* %3
%10 = tail call i32 @clReleaseCommandQueue(i32 %9)
%11 = load i32, i32* %2
%12 = tail call i32 @clReleaseContext(i32 %11)
%13 = bitcast i64* %6 to i8*
%14 = call i32 @clGetPlatformIDs(i32 0, i32* null, i64* %6)
%15 = load i64, i64* %6
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %22, %5
%18 = load i32******, i32******* %0
%19 = call i32 @free(i32****** %18)
%20 = load i32******, i32******* %1
%21 = call i32 @free(i32****** %20)
ret void
22: ; preds = %5, %22
%23 = phi i64 [ %29, %22 ], [ 0, %5 ]
%24 = load i32******, i32******* %1
%25 = getelementptr inbounds i32*****, i32****** %24, i64 %23
%26 = bitcast i32****** %25 to i32*******
%27 = load i32******, i32******* %26
%28 = call i32 @free(i32****** %27)
%29 = add nuw i64 %23, 1
%30 = load i64, i64* %6
%31 = icmp ult i64 %29, %30
br i1 %31, label %22, label %17
}