#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_platform_id;
typedef int /*<<< orphan*/ cl_mem;
typedef scalar_t__ cl_int;
typedef int /*<<< orphan*/ cl_event;
typedef int /*<<< orphan*/ cl_device_id;
typedef int /*<<< orphan*/ cl_context;
typedef scalar_t__ cl_command_type;
typedef scalar_t__ cl_command_queue;

/* Variables and functions */
scalar_t__ CL_COMMAND_READ_BUFFER;
scalar_t__ CL_COMPLETE;
int /*<<< orphan*/ CL_DEVICE_TYPE_ALL;
int /*<<< orphan*/ CL_EVENT_COMMAND_EXECUTION_STATUS;
int /*<<< orphan*/ CL_EVENT_COMMAND_QUEUE;
int /*<<< orphan*/ CL_EVENT_COMMAND_TYPE;
int /*<<< orphan*/ CL_EVENT_REFERENCE_COUNT;
int /*<<< orphan*/ CL_MEM_READ_WRITE;
scalar_t__ CL_SUCCESS;
int /*<<< orphan*/ CL_TRUE;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ clCreateBuffer(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *, scalar_t__ *);
scalar_t__ clCreateCommandQueue(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/, scalar_t__ *);
int /*<<< orphan*/ clCreateContext(int /*<<< orphan*/ *, int,
                                   int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *, scalar_t__ *);
scalar_t__ clEnqueueReadBuffer(scalar_t__, int /*<<< orphan*/,
                               int /*<<< orphan*/, int /*<<< orphan*/, int,
                               scalar_t__ **, int /*<<< orphan*/,
                               int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ clFinish(scalar_t__);
scalar_t__ clGetDeviceIDs(int /*<<< orphan*/, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *, size_t *);
scalar_t__ clGetEventInfo(int /*<<< orphan*/, int /*<<< orphan*/, int, ...);
scalar_t__ clGetPlatformIDs(int, int /*<<< orphan*/ *, size_t *);
int /*<<< orphan*/ clReleaseCommandQueue(scalar_t__);
int /*<<< orphan*/ clReleaseEvent(int /*<<< orphan*/);
int /*<<< orphan*/ clReleaseMemObject(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, size_t);
int main(void) {
  cl_int err;
  cl_platform_id platforms[32];
  cl_uint nplatforms;
  cl_device_id devices[32];
  cl_uint ndevices;
  cl_uint i, j;
  err = clGetPlatformIDs(32, platforms, &nplatforms);
  if (err != CL_SUCCESS)
    return EXIT_FAILURE;
  for (i = 0; i < nplatforms; i++) {
    err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 32, devices,
                         &ndevices);
    if (err != CL_SUCCESS)
      return EXIT_FAILURE;
    for (j = 0; j < ndevices; j++) {
      cl_context context =
          clCreateContext(NULL, 1, &devices[j], NULL, NULL, &err);
      if (err != CL_SUCCESS)
        return EXIT_FAILURE;
      cl_command_queue queue =
          clCreateCommandQueue(context, devices[j], 0, &err);
      if (err != CL_SUCCESS)
        return EXIT_FAILURE;
      const int buf_size = 1024;
      cl_int host_buf[buf_size];
      cl_mem buf = clCreateBuffer(context, CL_MEM_READ_WRITE,
                                  sizeof(cl_int) * buf_size, NULL, &err);
      if (err != CL_SUCCESS)
        return EXIT_FAILURE;
      cl_event buf_event;
      if (clEnqueueReadBuffer(queue, buf, CL_TRUE, 0, sizeof(cl_int) * buf_size,
                              &host_buf, 0, NULL, &buf_event) != CL_SUCCESS)
        return EXIT_FAILURE;
      clFinish(queue);
      cl_command_queue event_command_queue;
      size_t param_val_size_ret;
      if (clGetEventInfo(buf_event, CL_EVENT_COMMAND_QUEUE,
                         sizeof(cl_command_queue), &event_command_queue,
                         &param_val_size_ret) != CL_SUCCESS)
        return EXIT_FAILURE;
      if (param_val_size_ret != sizeof(cl_command_queue) ||
          event_command_queue != queue)
        return EXIT_FAILURE;
      cl_command_type command_type;
      if (clGetEventInfo(buf_event, CL_EVENT_COMMAND_TYPE,
                         sizeof(cl_command_type), &command_type,
                         &param_val_size_ret) != CL_SUCCESS)
        return EXIT_FAILURE;
      if (param_val_size_ret != sizeof(cl_command_type) ||
          command_type != CL_COMMAND_READ_BUFFER)
        return EXIT_FAILURE;
      cl_int execution_status;
      if (clGetEventInfo(buf_event, CL_EVENT_COMMAND_EXECUTION_STATUS,
                         sizeof(cl_int), &execution_status,
                         &param_val_size_ret) != CL_SUCCESS)
        return EXIT_FAILURE;
      if (param_val_size_ret != sizeof(cl_int) ||
          execution_status != CL_COMPLETE)
        return EXIT_FAILURE;
      cl_uint ref_count;
      if (clGetEventInfo(buf_event, CL_EVENT_REFERENCE_COUNT, sizeof(cl_uint),
                         &ref_count, &param_val_size_ret) != CL_SUCCESS)
        return EXIT_FAILURE;
      if (param_val_size_ret != sizeof(cl_uint) || ref_count != 1) {
        printf("FAIL: expected refcount 1, got %d\n", ref_count);
        return EXIT_FAILURE;
      }
      clReleaseEvent(buf_event);
      clReleaseMemObject(buf);
      clReleaseCommandQueue(queue);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x90(%rbp),%rsi
mov    $0x20,%edi
lea    -0x98(%rbp),%rdx
call   1070 <clGetPlatformIDs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <main+0x53>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    173a <main+0x57a>
movq   $0x0,-0x130(%rbp)
mov    -0x130(%rbp),%rax
cmp    -0x98(%rbp),%rax
jae    172e <main+0x56e>
mov    -0x130(%rbp),%rax
mov    -0x90(%rbp,%rax,4),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    -0x120(%rbp),%rcx
mov    $0x20,%edx
lea    -0x128(%rbp),%r8
call   1050 <clGetDeviceIDs@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d18(%rip),%rcx
cmp    (%rcx),%rax
je     128a <main+0xca>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    173a <main+0x57a>
movq   $0x0,-0x138(%rbp)
mov    -0x138(%rbp),%rax
cmp    -0x128(%rbp),%rax
jae    1712 <main+0x552>
mov    -0x138(%rbp),%rax
lea    -0x120(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x1,%esi
lea    -0x10(%rbp),%r9
mov    %r8,%rdi
mov    %r8,%rcx
call   10a0 <clCreateContext@plt>
mov    %eax,-0x13c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ca0(%rip),%rcx
cmp    (%rcx),%rax
je     1302 <main+0x142>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    173a <main+0x57a>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rax
mov    -0x120(%rbp,%rax,4),%esi
xor    %edx,%edx
lea    -0x10(%rbp),%rcx
call   1060 <clCreateCommandQueue@plt>
mov    %rax,-0x148(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2c55(%rip),%rcx
cmp    (%rcx),%rax
je     134d <main+0x18d>
mov    0x2c65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    173a <main+0x57a>
movl   $0x400,-0x14c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x158(%rbp)
mov    %rsp,%rax
add    $0xffffffffffffe000,%rax
mov    %rax,-0x198(%rbp)
mov    %rax,%rsp
mov    -0x13c(%rbp),%edi
mov    0x2c57(%rip),%rax
mov    (%rax),%esi
mov    $0x2000,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
call   1040 <clCreateBuffer@plt>
mov    %eax,-0x15c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2be2(%rip),%rcx
cmp    (%rcx),%rax
je     13ca <main+0x20a>
mov    0x2bf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x198(%rbp),%r9
mov    -0x148(%rbp),%rdi
mov    -0x15c(%rbp),%esi
mov    0x2c03(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    $0x2000,%r8d
xor    %eax,%eax
lea    -0x164(%rbp),%rax
sub    $0x20,%rsp
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   10c0 <clEnqueueReadBuffer@plt>
add    $0x20,%rsp
mov    0x2b67(%rip),%rcx
cmp    (%rcx),%rax
je     1445 <main+0x285>
mov    0x2b77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x148(%rbp),%rdi
call   1030 <clFinish@plt>
mov    -0x164(%rbp),%edi
mov    0x2b5a(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x170(%rbp),%rcx
lea    -0x178(%rbp),%r8
mov    $0x0,%al
call   1090 <clGetEventInfo@plt>
mov    0x2b07(%rip),%rcx
cmp    (%rcx),%rax
je     14a5 <main+0x2e5>
mov    0x2b17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
cmpq   $0x8,-0x178(%rbp)
08
jne    14c7 <main+0x307>
mov    -0x170(%rbp),%rax
cmp    -0x148(%rbp),%rax
je     14e2 <main+0x322>
mov    0x2ada(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x164(%rbp),%edi
mov    0x2ae1(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x180(%rbp),%rcx
lea    -0x178(%rbp),%r8
mov    $0x0,%al
call   1090 <clGetEventInfo@plt>
mov    0x2a76(%rip),%rcx
cmp    (%rcx),%rax
je     1536 <main+0x376>
mov    0x2a86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
cmpq   $0x8,-0x178(%rbp)
08
jne    155b <main+0x39b>
mov    -0x180(%rbp),%rax
mov    0x2a4e(%rip),%rcx
cmp    (%rcx),%rax
je     1576 <main+0x3b6>
mov    0x2a46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x164(%rbp),%edi
mov    0x2a5d(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x188(%rbp),%rcx
lea    -0x178(%rbp),%r8
mov    $0x0,%al
call   1090 <clGetEventInfo@plt>
mov    0x29e2(%rip),%rcx
cmp    (%rcx),%rax
je     15ca <main+0x40a>
mov    0x29f2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
cmpq   $0x8,-0x178(%rbp)
08
jne    15ef <main+0x42f>
mov    -0x188(%rbp),%rax
mov    0x29aa(%rip),%rcx
cmp    (%rcx),%rax
je     160a <main+0x44a>
mov    0x29b2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x164(%rbp),%edi
mov    0x2999(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x190(%rbp),%rcx
lea    -0x178(%rbp),%r8
mov    $0x0,%al
call   1090 <clGetEventInfo@plt>
mov    0x294e(%rip),%rcx
cmp    (%rcx),%rax
je     165e <main+0x49e>
mov    0x295e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
cmpq   $0x8,-0x178(%rbp)
08
jne    167a <main+0x4ba>
cmpq   $0x1,-0x190(%rbp)
01
je     16a8 <main+0x4e8>
mov    -0x190(%rbp),%rsi
lea    0x978(%rip),%rdi
call   1080 <printf@plt>
mov    0x2914(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x160(%rbp)
jmp    16d4 <main+0x514>
mov    -0x164(%rbp),%edi
call   10e0 <clReleaseEvent@plt>
mov    -0x15c(%rbp),%edi
call   10d0 <clReleaseMemObject@plt>
mov    -0x148(%rbp),%rdi
call   10b0 <clReleaseCommandQueue@plt>
movl   $0x0,-0x160(%rbp)
mov    -0x158(%rbp),%rax
mov    %rax,%rsp
mov    -0x160(%rbp),%eax
test   %eax,%eax
je     16f6 <main+0x536>
jmp    16f1 <main+0x531>
jmp    173a <main+0x57a>
jmp    16fb <main+0x53b>
mov    -0x138(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x138(%rbp)
jmp    1295 <main+0xd5>
jmp    1717 <main+0x557>
mov    -0x130(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x130(%rbp)
jmp    121e <main+0x5e>
mov    0x2863(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x168,%rsp
lea    -0x190(%rbp),%rsi
lea    -0x60(%rbp),%rdx
mov    $0x20,%edi
call   1070 <clGetPlatformIDs@plt>
mov    %rax,-0x40(%rbp)
mov    0x2d94(%rip),%r13
cmp    0x0(%r13),%rax
jne    14dd <main+0x31d>
cmpq   $0x0,-0x60(%rbp)
je     14d4 <main+0x314>
mov    0x2db8(%rip),%rax
lea    -0x110(%rbp),%rcx
lea    -0x58(%rbp),%r8
lea    -0x40(%rbp),%r14
lea    -0x48(%rbp),%r12
xor    %ebx,%ebx
mov    -0x190(%rbp,%rbx,4),%edi
mov    (%rax),%esi
mov    $0x20,%edx
call   1050 <clGetDeviceIDs@plt>
mov    %rax,-0x40(%rbp)
cmp    0x0(%r13),%rax
jne    14dd <main+0x31d>
mov    %rbx,-0x68(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     14b0 <main+0x2f0>
xor    %ebx,%ebx
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
lea    (%rbx,%rbp,1),%rdx
add    $0xfffffffffffffef0,%rdx
xor    %edi,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   10a0 <clCreateContext@plt>
mov    %eax,%r15d
mov    -0x40(%rbp),%rax
cmp    0x0(%r13),%rax
jne    14dd <main+0x31d>
mov    -0x50(%rbp),%rax
mov    -0x110(%rbp,%rax,4),%esi
mov    %r15d,%edi
xor    %edx,%edx
mov    %r14,%rcx
call   1060 <clCreateCommandQueue@plt>
mov    %r14,%r8
mov    %rax,%r14
mov    -0x40(%rbp),%rax
cmp    0x0(%r13),%rax
jne    14dd <main+0x31d>
mov    %rbx,-0x70(%rbp)
mov    %r12,%rbx
mov    %rsp,-0x38(%rbp)
mov    %r13,%r12
mov    %rsp,%r13
add    $0xffffffffffffe000,%r13
mov    %r13,%rsp
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1040 <clCreateBuffer@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
cmp    (%r12),%rax
jne    14f4 <main+0x334>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
sub    $0x8,%rsp
mov    %r14,%r15
mov    %r14,%rdi
mov    %ecx,%r14d
mov    %ecx,%esi
xor    %ecx,%ecx
mov    $0x2000,%r8d
mov    %r13,%r9
lea    -0x2c(%rbp),%rax
push   %rax
push   $0x0
push   $0x0
call   10c0 <clEnqueueReadBuffer@plt>
add    $0x20,%rsp
cmp    (%r12),%rax
jne    14f4 <main+0x334>
mov    %r12,%r13
mov    %r15,%rdi
call   1030 <clFinish@plt>
mov    -0x2c(%rbp),%edi
mov    0x2c68(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x90(%rbp),%rcx
mov    %rbx,%r12
mov    %rbx,%r8
xor    %eax,%eax
call   1090 <clGetEventInfo@plt>
cmp    0x0(%r13),%rax
jne    14f4 <main+0x334>
cmpq   $0x8,-0x48(%rbp)
jne    14f4 <main+0x334>
cmp    %r15,-0x90(%rbp)
jne    14f4 <main+0x334>
mov    -0x2c(%rbp),%edi
mov    0x2c39(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x88(%rbp),%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1090 <clGetEventInfo@plt>
cmp    0x0(%r13),%rax
jne    14f4 <main+0x334>
cmpq   $0x8,-0x48(%rbp)
jne    14f4 <main+0x334>
mov    -0x88(%rbp),%rax
mov    0x2bce(%rip),%rcx
cmp    (%rcx),%rax
jne    14f4 <main+0x334>
mov    -0x2c(%rbp),%edi
mov    0x2bfb(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x80(%rbp),%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1090 <clGetEventInfo@plt>
cmp    0x0(%r13),%rax
jne    14f4 <main+0x334>
cmpq   $0x8,-0x48(%rbp)
jne    14f4 <main+0x334>
mov    -0x80(%rbp),%rax
mov    0x2b76(%rip),%rcx
cmp    (%rcx),%rax
jne    14f4 <main+0x334>
mov    -0x2c(%rbp),%edi
mov    0x2b83(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x78(%rbp),%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1090 <clGetEventInfo@plt>
cmp    0x0(%r13),%rax
jne    14f4 <main+0x334>
cmpq   $0x8,-0x48(%rbp)
mov    -0x78(%rbp),%rsi
jne    14e8 <main+0x328>
cmp    $0x1,%rsi
jne    14e8 <main+0x328>
mov    -0x2c(%rbp),%edi
call   10e0 <clReleaseEvent@plt>
mov    %r14d,%edi
call   10d0 <clReleaseMemObject@plt>
mov    %r15,%rdi
call   10b0 <clReleaseCommandQueue@plt>
mov    -0x38(%rbp),%rsp
mov    -0x50(%rbp),%rcx
add    $0x1,%rcx
mov    -0x70(%rbp),%rbx
add    $0x4,%rbx
mov    %rcx,%rax
mov    %rcx,-0x50(%rbp)
cmp    -0x58(%rbp),%rcx
lea    -0x40(%rbp),%r14
jb     125d <main+0x9d>
cs nopw 0x0(%rax,%rax,1)
mov    -0x68(%rbp),%rbx
add    $0x1,%rbx
cmp    -0x60(%rbp),%rbx
mov    0x2b05(%rip),%rax
lea    -0x110(%rbp),%rcx
lea    -0x58(%rbp),%r8
jb     1225 <main+0x65>
mov    0x2abd(%rip),%rax
jmp    14e4 <main+0x324>
mov    0x2ac4(%rip),%rax
mov    (%rax),%eax
jmp    1501 <main+0x341>
lea    0xb11(%rip),%rdi
call   1080 <printf@plt>
mov    0x2aad(%rip),%rax
mov    (%rax),%eax
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CL_SUCCESS = global i64 0
@EXIT_FAILURE = global i32 0
@CL_DEVICE_TYPE_ALL = global i32 0
@CL_MEM_READ_WRITE = global i32 0
@CL_TRUE = global i32 0
@CL_EVENT_COMMAND_QUEUE = global i32 0
@CL_EVENT_COMMAND_TYPE = global i32 0
@CL_COMMAND_READ_BUFFER = global i64 0
@CL_EVENT_COMMAND_EXECUTION_STATUS = global i32 0
@CL_COMPLETE = global i64 0
@CL_EVENT_REFERENCE_COUNT = global i32 0
@.str = [35 x i8] c"FAIL: expected refcount 1, got %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [32 x i32]
%4 = alloca i64
%5 = alloca [32 x i32]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64
%19 = alloca i64
%20 = alloca i64
store i32 0, i32* %1
%21 = getelementptr inbounds [32 x i32], [32 x i32]* %3, i64 0, i64 0
%22 = call i64 @clGetPlatformIDs(i32 32, i32* %21, i64* %4)
store i64 %22, i64* %2
%23 = load i64, i64* %2
%24 = load i64, i64* @CL_SUCCESS
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %1
br label %182
28: ; preds = %0
store i64 0, i64* %7
br label %29
29: ; preds = %177, %28
%30 = load i64, i64* %7
%31 = load i64, i64* %4
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %180
33: ; preds = %29
%34 = load i64, i64* %7
%35 = getelementptr inbounds [32 x i32], [32 x i32]* %3, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = load i32, i32* @CL_DEVICE_TYPE_ALL
%38 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 0
%39 = call i64 @clGetDeviceIDs(i32 %36, i32 %37, i32 32, i32* %38, i64* %6)
store i64 %39, i64* %2
%40 = load i64, i64* %2
%41 = load i64, i64* @CL_SUCCESS
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %33
%44 = load i32, i32* @EXIT_FAILURE
store i32 %44, i32* %1
br label %182
45: ; preds = %33
store i64 0, i64* %8
br label %46
46: ; preds = %173, %45
%47 = load i64, i64* %8
%48 = load i64, i64* %6
%49 = icmp ult i64 %47, %48
br i1 %49, label %50, label %176
50: ; preds = %46
%51 = load i64, i64* %8
%52 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 %51
%53 = call i32 @clCreateContext(i32* null, i32 1, i32* %52, i32* null, i32* null, i64* %2)
store i32 %53, i32* %9
%54 = load i64, i64* %2
%55 = load i64, i64* @CL_SUCCESS
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = load i32, i32* @EXIT_FAILURE
store i32 %58, i32* %1
br label %182
59: ; preds = %50
%60 = load i32, i32* %9
%61 = load i64, i64* %8
%62 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = call i64 @clCreateCommandQueue(i32 %60, i32 %63, i32 0, i64* %2)
store i64 %64, i64* %10
%65 = load i64, i64* %2
%66 = load i64, i64* @CL_SUCCESS
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %70
68: ; preds = %59
%69 = load i32, i32* @EXIT_FAILURE
store i32 %69, i32* %1
br label %182
70: ; preds = %59
store i32 1024, i32* %11
%71 = call i8* @llvm.stacksave()
store i8* %71, i8** %12
%72 = alloca i64, i64 1024
%73 = load i32, i32* %9
%74 = load i32, i32* @CL_MEM_READ_WRITE
%75 = call i32 @clCreateBuffer(i32 %73, i32 %74, i32 8192, i32* null, i64* %2)
store i32 %75, i32* %13
%76 = load i64, i64* %2
%77 = load i64, i64* @CL_SUCCESS
%78 = icmp ne i64 %76, %77
br i1 %78, label %79, label %81
79: ; preds = %70
%80 = load i32, i32* @EXIT_FAILURE
store i32 %80, i32* %1
store i32 1, i32* %14
br label %169
81: ; preds = %70
%82 = load i64, i64* %10
%83 = load i32, i32* %13
%84 = load i32, i32* @CL_TRUE
%85 = bitcast i64* %72 to i64**
%86 = call i64 @clEnqueueReadBuffer(i64 %82, i32 %83, i32 %84, i32 0, i32 8192, i64** %85, i32 0, i32* null, i32* %15)
%87 = load i64, i64* @CL_SUCCESS
%88 = icmp ne i64 %86, %87
br i1 %88, label %89, label %91
89: ; preds = %81
%90 = load i32, i32* @EXIT_FAILURE
store i32 %90, i32* %1
store i32 1, i32* %14
br label %169
91: ; preds = %81
%92 = load i64, i64* %10
%93 = call i32 @clFinish(i64 %92)
%94 = load i32, i32* %15
%95 = load i32, i32* @CL_EVENT_COMMAND_QUEUE
%96 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %94, i32 %95, i32 8, i64* %16, i64* %17)
%97 = load i64, i64* @CL_SUCCESS
%98 = icmp ne i64 %96, %97
br i1 %98, label %99, label %101
99: ; preds = %91
%100 = load i32, i32* @EXIT_FAILURE
store i32 %100, i32* %1
store i32 1, i32* %14
br label %169
101: ; preds = %91
%102 = load i64, i64* %17
%103 = icmp ne i64 %102, 8
br i1 %103, label %108, label %104
104: ; preds = %101
%105 = load i64, i64* %16
%106 = load i64, i64* %10
%107 = icmp ne i64 %105, %106
br i1 %107, label %108, label %110
108: ; preds = %104, %101
%109 = load i32, i32* @EXIT_FAILURE
store i32 %109, i32* %1
store i32 1, i32* %14
br label %169
110: ; preds = %104
%111 = load i32, i32* %15
%112 = load i32, i32* @CL_EVENT_COMMAND_TYPE
%113 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %111, i32 %112, i32 8, i64* %18, i64* %17)
%114 = load i64, i64* @CL_SUCCESS
%115 = icmp ne i64 %113, %114
br i1 %115, label %116, label %118
116: ; preds = %110
%117 = load i32, i32* @EXIT_FAILURE
store i32 %117, i32* %1
store i32 1, i32* %14
br label %169
118: ; preds = %110
%119 = load i64, i64* %17
%120 = icmp ne i64 %119, 8
br i1 %120, label %125, label %121
121: ; preds = %118
%122 = load i64, i64* %18
%123 = load i64, i64* @CL_COMMAND_READ_BUFFER
%124 = icmp ne i64 %122, %123
br i1 %124, label %125, label %127
125: ; preds = %121, %118
%126 = load i32, i32* @EXIT_FAILURE
store i32 %126, i32* %1
store i32 1, i32* %14
br label %169
127: ; preds = %121
%128 = load i32, i32* %15
%129 = load i32, i32* @CL_EVENT_COMMAND_EXECUTION_STATUS
%130 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %128, i32 %129, i32 8, i64* %19, i64* %17)
%131 = load i64, i64* @CL_SUCCESS
%132 = icmp ne i64 %130, %131
br i1 %132, label %133, label %135
133: ; preds = %127
%134 = load i32, i32* @EXIT_FAILURE
store i32 %134, i32* %1
store i32 1, i32* %14
br label %169
135: ; preds = %127
%136 = load i64, i64* %17
%137 = icmp ne i64 %136, 8
br i1 %137, label %142, label %138
138: ; preds = %135
%139 = load i64, i64* %19
%140 = load i64, i64* @CL_COMPLETE
%141 = icmp ne i64 %139, %140
br i1 %141, label %142, label %144
142: ; preds = %138, %135
%143 = load i32, i32* @EXIT_FAILURE
store i32 %143, i32* %1
store i32 1, i32* %14
br label %169
144: ; preds = %138
%145 = load i32, i32* %15
%146 = load i32, i32* @CL_EVENT_REFERENCE_COUNT
%147 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %145, i32 %146, i32 8, i64* %20, i64* %17)
%148 = load i64, i64* @CL_SUCCESS
%149 = icmp ne i64 %147, %148
br i1 %149, label %150, label %152
150: ; preds = %144
%151 = load i32, i32* @EXIT_FAILURE
store i32 %151, i32* %1
store i32 1, i32* %14
br label %169
152: ; preds = %144
%153 = load i64, i64* %17
%154 = icmp ne i64 %153, 8
br i1 %154, label %158, label %155
155: ; preds = %152
%156 = load i64, i64* %20
%157 = icmp ne i64 %156, 1
br i1 %157, label %158, label %162
158: ; preds = %155, %152
%159 = load i64, i64* %20
%160 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %159)
%161 = load i32, i32* @EXIT_FAILURE
store i32 %161, i32* %1
store i32 1, i32* %14
br label %169
162: ; preds = %155
%163 = load i32, i32* %15
%164 = call i32 @clReleaseEvent(i32 %163)
%165 = load i32, i32* %13
%166 = call i32 @clReleaseMemObject(i32 %165)
%167 = load i64, i64* %10
%168 = call i32 @clReleaseCommandQueue(i64 %167)
store i32 0, i32* %14
br label %169
169: ; preds = %162, %158, %150, %142, %133, %125, %116, %108, %99, %89, %79
%170 = load i8*, i8** %12
%171 = load i32, i32* %14
switch i32 %171, label %184 [
i32 0, label %172
i32 1, label %182
]
172: ; preds = %169
br label %173
173: ; preds = %172
%174 = load i64, i64* %8
%175 = add i64 %174, 1
store i64 %175, i64* %8
br label %46
176: ; preds = %46
br label %177
177: ; preds = %176
%178 = load i64, i64* %7
%179 = add i64 %178, 1
store i64 %179, i64* %7
br label %29
180: ; preds = %29
%181 = load i32, i32* @EXIT_SUCCESS
store i32 %181, i32* %1
br label %182
182: ; preds = %180, %169, %68, %57, %43, %26
%183 = load i32, i32* %1
ret i32 %183
184: ; preds = %169
unreachable
}

LLVM IR (-O2):
@CL_SUCCESS = global i64 0
@EXIT_FAILURE = global i32 0
@CL_DEVICE_TYPE_ALL = global i32 0
@CL_MEM_READ_WRITE = global i32 0
@CL_TRUE = global i32 0
@CL_EVENT_COMMAND_QUEUE = global i32 0
@CL_EVENT_COMMAND_TYPE = global i32 0
@CL_COMMAND_READ_BUFFER = global i64 0
@CL_EVENT_COMMAND_EXECUTION_STATUS = global i32 0
@CL_COMPLETE = global i64 0
@CL_EVENT_REFERENCE_COUNT = global i32 0
@.str = [35 x i8] c"FAIL: expected refcount 1, got %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i64
%2 = alloca [32 x i32]
%3 = alloca i64
%4 = alloca [32 x i32]
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = bitcast i64* %1 to i8*
%13 = bitcast [32 x i32]* %2 to i8*
%14 = bitcast i64* %3 to i8*
%15 = bitcast [32 x i32]* %4 to i8*
%16 = bitcast i64* %5 to i8*
%17 = getelementptr inbounds [32 x i32], [32 x i32]* %2, i64 0, i64 0
%18 = call i64 @clGetPlatformIDs(i32 32, i32* %17, i64* %3)
store i64 %18, i64* %1
%19 = load i64, i64* @CL_SUCCESS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %0
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %155, label %24
24: ; preds = %21
%25 = getelementptr inbounds [32 x i32], [32 x i32]* %4, i64 0, i64 0
%26 = bitcast i32* %6 to i8*
%27 = bitcast i64* %7 to i8*
%28 = bitcast i64* %8 to i8*
%29 = bitcast i64* %9 to i8*
%30 = bitcast i64* %10 to i8*
%31 = bitcast i64* %11 to i8*
br label %34
32: ; preds = %0
%33 = load i32, i32* @EXIT_FAILURE
br label %157
34: ; preds = %24, %151
%35 = phi i64 [ 0, %24 ], [ %152, %151 ]
%36 = getelementptr inbounds [32 x i32], [32 x i32]* %2, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* @CL_DEVICE_TYPE_ALL
%39 = call i64 @clGetDeviceIDs(i32 %37, i32 %38, i32 32, i32* %25, i64* %5)
store i64 %39, i64* %1
%40 = load i64, i64* @CL_SUCCESS
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = load i64, i64* %5
%44 = icmp eq i64 %43, 0
br i1 %44, label %151, label %47
45: ; preds = %34
%46 = load i32, i32* @EXIT_FAILURE
br label %157
47: ; preds = %42, %143
%48 = phi i64 [ %148, %143 ], [ 0, %42 ]
%49 = getelementptr inbounds [32 x i32], [32 x i32]* %4, i64 0, i64 %48
%50 = call i32 @clCreateContext(i32* null, i32 1, i32* %49, i32* null, i32* null, i64* %1)
%51 = load i64, i64* %1
%52 = load i64, i64* @CL_SUCCESS
%53 = icmp eq i64 %51, %52
br i1 %53, label %56, label %54
54: ; preds = %47
%55 = load i32, i32* @EXIT_FAILURE
br label %157
56: ; preds = %47
%57 = load i32, i32* %49
%58 = call i64 @clCreateCommandQueue(i32 %50, i32 %57, i32 0, i64* %1)
%59 = load i64, i64* %1
%60 = load i64, i64* @CL_SUCCESS
%61 = icmp eq i64 %59, %60
br i1 %61, label %64, label %62
62: ; preds = %56
%63 = load i32, i32* @EXIT_FAILURE
br label %157
64: ; preds = %56
%65 = call i8* @llvm.stacksave()
%66 = alloca [1024 x i64*]
%67 = load i32, i32* @CL_MEM_READ_WRITE
%68 = call i32 @clCreateBuffer(i32 %50, i32 %67, i32 8192, i32* null, i64* %1)
%69 = load i64, i64* %1
%70 = load i64, i64* @CL_SUCCESS
%71 = icmp eq i64 %69, %70
br i1 %71, label %74, label %72
72: ; preds = %64
%73 = load i32, i32* @EXIT_FAILURE
br label %157
74: ; preds = %64
%75 = getelementptr inbounds [1024 x i64*], [1024 x i64*]* %66, i64 0, i64 0
%76 = load i32, i32* @CL_TRUE
%77 = call i64 @clEnqueueReadBuffer(i64 %58, i32 %68, i32 %76, i32 0, i32 8192, i64** %75, i32 0, i32* null, i32* %6)
%78 = load i64, i64* @CL_SUCCESS
%79 = icmp eq i64 %77, %78
br i1 %79, label %82, label %80
80: ; preds = %74
%81 = load i32, i32* @EXIT_FAILURE
br label %157
82: ; preds = %74
%83 = call i32 @clFinish(i64 %58)
%84 = load i32, i32* %6
%85 = load i32, i32* @CL_EVENT_COMMAND_QUEUE
%86 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %84, i32 %85, i32 8, i64* %7, i64* %8)
%87 = load i64, i64* @CL_SUCCESS
%88 = icmp eq i64 %86, %87
br i1 %88, label %89, label %135
89: ; preds = %82
%90 = load i64, i64* %8
%91 = icmp eq i64 %90, 8
%92 = load i64, i64* %7
%93 = icmp eq i64 %92, %58
%94 = select i1 %91, i1 %93, i1 false
br i1 %94, label %95, label %135
95: ; preds = %89
%96 = load i32, i32* %6
%97 = load i32, i32* @CL_EVENT_COMMAND_TYPE
%98 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %96, i32 %97, i32 8, i64* %9, i64* %8)
%99 = load i64, i64* @CL_SUCCESS
%100 = icmp eq i64 %98, %99
%101 = load i64, i64* %8
%102 = icmp eq i64 %101, 8
%103 = select i1 %100, i1 %102, i1 false
br i1 %103, label %104, label %137
104: ; preds = %95
%105 = load i64, i64* %9
%106 = load i64, i64* @CL_COMMAND_READ_BUFFER
%107 = icmp eq i64 %105, %106
br i1 %107, label %108, label %137
108: ; preds = %104
%109 = load i32, i32* %6
%110 = load i32, i32* @CL_EVENT_COMMAND_EXECUTION_STATUS
%111 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %109, i32 %110, i32 8, i64* %10, i64* %8)
%112 = load i64, i64* @CL_SUCCESS
%113 = icmp eq i64 %111, %112
%114 = load i64, i64* %8
%115 = icmp eq i64 %114, 8
%116 = select i1 %113, i1 %115, i1 false
br i1 %116, label %117, label %139
117: ; preds = %108
%118 = load i64, i64* %10
%119 = load i64, i64* @CL_COMPLETE
%120 = icmp eq i64 %118, %119
br i1 %120, label %121, label %139
121: ; preds = %117
%122 = load i32, i32* %6
%123 = load i32, i32* @CL_EVENT_REFERENCE_COUNT
%124 = call i64 (i32, i32, i32, ...) @clGetEventInfo(i32 %122, i32 %123, i32 8, i64* %11, i64* %8)
%125 = load i64, i64* @CL_SUCCESS
%126 = icmp eq i64 %124, %125
br i1 %126, label %127, label %141
127: ; preds = %121
%128 = load i64, i64* %8
%129 = icmp ne i64 %128, 8
%130 = load i64, i64* %11
%131 = icmp ne i64 %130, 1
%132 = select i1 %129, i1 true, i1 %131
br i1 %132, label %133, label %143
133: ; preds = %127
%134 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %130)
br label %141
135: ; preds = %89, %82
%136 = load i32, i32* @EXIT_FAILURE
br label %157
137: ; preds = %104, %95
%138 = load i32, i32* @EXIT_FAILURE
br label %157
139: ; preds = %117, %108
%140 = load i32, i32* @EXIT_FAILURE
br label %157
141: ; preds = %121, %133
%142 = load i32, i32* @EXIT_FAILURE
br label %157
143: ; preds = %127
%144 = load i32, i32* %6
%145 = call i32 @clReleaseEvent(i32 %144)
%146 = call i32 @clReleaseMemObject(i32 %68)
%147 = call i32 @clReleaseCommandQueue(i64 %58)
%148 = add nuw i64 %48, 1
%149 = load i64, i64* %5
%150 = icmp ult i64 %148, %149
br i1 %150, label %47, label %151
151: ; preds = %143, %42
%152 = add nuw i64 %35, 1
%153 = load i64, i64* %3
%154 = icmp ult i64 %152, %153
br i1 %154, label %34, label %155
155: ; preds = %151, %21
%156 = load i32, i32* @EXIT_SUCCESS
br label %157
157: ; preds = %62, %54, %141, %139, %137, %135, %80, %72, %155, %45, %32
%158 = phi i32 [ %33, %32 ], [ %46, %45 ], [ %156, %155 ], [ %73, %72 ], [ %81, %80 ], [ %136, %135 ], [ %138, %137 ], [ %140, %139 ], [ %142, %141 ], [ %63, %62 ], [ %55, %54 ]
ret i32 %158
}