#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 scalar_t__ cl_ulong;
typedef scalar_t__ cl_int;
typedef int /*<<< orphan*/ cl_event;

/* Variables and functions */
int /*<<< orphan*/ CL_PROFILING_COMMAND_END;
int /*<<< orphan*/ CL_PROFILING_COMMAND_START;
scalar_t__ CL_SUCCESS;
scalar_t__ clGetEventProfilingInfo(int /*<<< orphan*/, int /*<<< orphan*/, int,
                                   scalar_t__ *, int /*<<< orphan*/ *);
unsigned long long int readElapsedTime(cl_event internal) {
  cl_int status;
  cl_ulong t_begin, t_end;
  status = clGetEventProfilingInfo(internal, CL_PROFILING_COMMAND_START,
                                   sizeof(cl_ulong), &t_begin, NULL);
  if (status != CL_SUCCESS)
    return 0;
  status = clGetEventProfilingInfo(internal, CL_PROFILING_COMMAND_END,
                                   sizeof(cl_ulong), &t_end, NULL);
  if (status != CL_SUCCESS)
    return 0;
  return (unsigned long long int)(t_end - t_begin);
}


x86 assembly (clang, -O0):
<readElapsedTime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetEventProfilingInfo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
je     115f <readElapsedTime+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11af <readElapsedTime+0x9f>
mov    -0xc(%rbp),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clGetEventProfilingInfo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11a3 <readElapsedTime+0x93>
movq   $0x0,-0x8(%rbp)
jmp    11af <readElapsedTime+0x9f>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readElapsedTime>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2eb7(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
lea    0x8(%rsp),%rcx
mov    $0x8,%edx
xor    %r8d,%r8d
call   1030 <clGetEventProfilingInfo@plt>
mov    0x2e92(%rip),%r14
cmp    (%r14),%rax
jne    116e <readElapsedTime+0x5e>
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x8,%edx
xor    %r8d,%r8d
call   1030 <clGetEventProfilingInfo@plt>
cmp    (%r14),%rax
jne    116e <readElapsedTime+0x5e>
mov    (%rsp),%rbx
sub    0x8(%rsp),%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@CL_PROFILING_COMMAND_START = global i32 0
@CL_SUCCESS = global i64 0
@CL_PROFILING_COMMAND_END = global i32 0
define i64 @readElapsedTime(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @CL_PROFILING_COMMAND_START
%9 = call i64 @clGetEventProfilingInfo(i32 %7, i32 %8, i32 8, i64* %5, i32* null)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = load i64, i64* @CL_SUCCESS
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %1
store i64 0, i64* %2
br label %26
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @CL_PROFILING_COMMAND_END
%17 = call i64 @clGetEventProfilingInfo(i32 %15, i32 %16, i32 8, i64* %6, i32* null)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = load i64, i64* @CL_SUCCESS
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %14
store i64 0, i64* %2
br label %26
22: ; preds = %14
%23 = load i64, i64* %6
%24 = load i64, i64* %5
%25 = sub nsw i64 %23, %24
store i64 %25, i64* %2
br label %26
26: ; preds = %22, %21, %13
%27 = load i64, i64* %2
ret i64 %27
}

LLVM IR (-O2):
@CL_PROFILING_COMMAND_START = global i32 0
@CL_SUCCESS = global i64 0
@CL_PROFILING_COMMAND_END = global i32 0
define i64 @readElapsedTime(i32 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = load i32, i32* @CL_PROFILING_COMMAND_START
%7 = call i64 @clGetEventProfilingInfo(i32 %0, i32 %6, i32 8, i64* %2, i32* null)
%8 = load i64, i64* @CL_SUCCESS
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %19
10: ; preds = %1
%11 = load i32, i32* @CL_PROFILING_COMMAND_END
%12 = call i64 @clGetEventProfilingInfo(i32 %0, i32 %11, i32 8, i64* %3, i32* null)
%13 = load i64, i64* @CL_SUCCESS
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i64, i64* %3
%17 = load i64, i64* %2
%18 = sub nsw i64 %16, %17
br label %19
19: ; preds = %10, %1, %15
%20 = phi i64 [ %18, %15 ], [ 0, %1 ], [ 0, %10 ]
ret i64 %20
}