#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 */
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ load_cpu();
unsigned long rdtscl();
double get_cycles_per_usec(void) {
  unsigned long pre, post;
  struct timespec start_t, end_t;
  unsigned long tsc_start, tsc_end;
  double elapsed;
  int i;
  load_cpu();
  pre = rdtscl();
  clock_gettime(CLOCK_MONOTONIC, &start_t);
  post = rdtscl();
  tsc_start = ((double)pre + (double)post) / 2;
  for (i = 0; i < 10; i++)
    load_cpu();
  pre = rdtscl();
  clock_gettime(CLOCK_MONOTONIC, &end_t);
  post = rdtscl();
  tsc_end = ((double)pre + (double)post) / 2;
  struct timespec delta;
  delta.tv_sec = end_t.tv_sec - start_t.tv_sec;
  delta.tv_nsec = end_t.tv_nsec - start_t.tv_nsec;
  if (delta.tv_nsec < 0) {
    delta.tv_nsec += 1e9;
    delta.tv_sec--;
  }
  elapsed = delta.tv_nsec / 1000 + delta.tv_sec * 1e6;
  return (tsc_end - tsc_start) / elapsed;
}


x86 assembly (clang, -O0):
<get_cycles_per_usec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
xor    %eax,%eax
mov    %al,-0x49(%rbp)
call   1050 <load_cpu@plt>
mov    -0x49(%rbp),%al
call   1040 <rdtscl@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x49(%rbp),%al
call   1040 <rdtscl@plt>
mov    %rax,-0x10(%rbp)
movaps 0xe8d(%rip),%xmm3
movq   -0x8(%rbp),%xmm0
punpckldq %xmm3,%xmm0
movapd 0xe8c(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movq   -0x10(%rbp),%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  0xe69(%rip),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe56(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0xa,-0x3c(%rbp)
jge    120b <get_cycles_per_usec+0xdb>
mov    $0x0,%al
call   1050 <load_cpu@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ec <get_cycles_per_usec+0xbc>
xor    %eax,%eax
mov    %al,-0x4a(%rbp)
call   1040 <rdtscl@plt>
mov    %rax,-0x8(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x4a(%rbp),%al
call   1040 <rdtscl@plt>
mov    %rax,-0x10(%rbp)
movaps 0xdc2(%rip),%xmm3
movq   -0x8(%rbp),%xmm0
punpckldq %xmm3,%xmm0
movapd 0xdc1(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movq   -0x10(%rbp),%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  0xd9e(%rip),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xd8b(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    12ed <get_cycles_per_usec+0x1bd>
cvtsi2sdl -0x44(%rbp),%xmm0
movsd  0xd57(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x48(%rbp)
movslq -0x44(%rbp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
cvtsi2sd %eax,%xmm1
cvtsi2sdl -0x48(%rbp),%xmm0
movsd  0xd22(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm0
movaps 0xcc6(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xcca(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
divsd  -0x38(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cycles_per_usec>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1040 <rdtscl@plt>
mov    %rax,%r14
mov    0x2e95(%rip),%r12
mov    (%r12),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %eax,%eax
call   1040 <rdtscl@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1050 <load_cpu@plt>
xor    %eax,%eax
call   1040 <rdtscl@plt>
mov    %rax,%rbx
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %eax,%eax
call   1040 <rdtscl@plt>
mov    0x8(%rsp),%esi
sub    0x10(%rsp),%esi
mov    0xc(%rsp),%ecx
sub    0x14(%rsp),%ecx
lea    0x3b9aca00(%rcx),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x1f,%ecx
add    %esi,%ecx
movq   %rbx,%xmm2
movdqa 0xe03(%rip),%xmm0
punpckldq %xmm0,%xmm2
movapd 0xe07(%rip),%xmm1
subpd  %xmm1,%xmm2
movapd %xmm2,%xmm3
unpckhpd %xmm2,%xmm3
addsd  %xmm2,%xmm3
movq   %rax,%xmm2
punpckldq %xmm0,%xmm2
subpd  %xmm1,%xmm2
movapd %xmm2,%xmm4
unpckhpd %xmm2,%xmm4
addsd  %xmm2,%xmm4
addsd  %xmm3,%xmm4
movsd  0xde2(%rip),%xmm2
mulsd  %xmm2,%xmm4
cvttsd2si %xmm4,%rsi
mov    %rsi,%rdi
sar    $0x3f,%rdi
movsd  0xdd2(%rip),%xmm3
subsd  %xmm3,%xmm4
cvttsd2si %xmm4,%rax
and    %rdi,%rax
or     %rsi,%rax
movq   %r14,%xmm4
punpckldq %xmm0,%xmm4
subpd  %xmm1,%xmm4
movapd %xmm4,%xmm5
unpckhpd %xmm4,%xmm5
addsd  %xmm4,%xmm5
movq   %r15,%xmm4
punpckldq %xmm0,%xmm4
subpd  %xmm1,%xmm4
movapd %xmm4,%xmm6
unpckhpd %xmm4,%xmm6
addsd  %xmm4,%xmm6
addsd  %xmm5,%xmm6
mulsd  %xmm2,%xmm6
cvttsd2si %xmm6,%rsi
mov    %rsi,%rdi
sar    $0x3f,%rdi
subsd  %xmm3,%xmm6
cvttsd2si %xmm6,%rbx
and    %rdi,%rbx
or     %rsi,%rbx
movslq %edx,%rdx
imul   $0x10624dd3,%rdx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rdx
add    %esi,%edx
xorps  %xmm2,%xmm2
cvtsi2sd %edx,%xmm2
xorps  %xmm3,%xmm3
cvtsi2sd %ecx,%xmm3
mulsd  0xd49(%rip),%xmm3
sub    %rbx,%rax
addsd  %xmm2,%xmm3
movq   %rax,%xmm2
punpckldq %xmm0,%xmm2
subpd  %xmm1,%xmm2
movapd %xmm2,%xmm0
unpckhpd %xmm2,%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm3,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define double @get_cycles_per_usec() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i64
%7 = alloca double
%8 = alloca i32
%9 = alloca %struct.timespec
%10 = call i32 (...) @load_cpu()
%11 = call i64 (...) @rdtscl()
store i64 %11, i64* %1
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %3)
%14 = call i64 (...) @rdtscl()
store i64 %14, i64* %2
%15 = load i64, i64* %1
%16 = uitofp i64 %15 to double
%17 = load i64, i64* %2
%18 = uitofp i64 %17 to double
%19 = fadd double %16, %18
%20 = fdiv double %19, 2.000000e+00
%21 = fptoui double %20 to i64
store i64 %21, i64* %5
store i32 0, i32* %8
br label %22
22: ; preds = %27, %0
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 10
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = call i32 (...) @load_cpu()
br label %27
27: ; preds = %25
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %22
30: ; preds = %22
%31 = call i64 (...) @rdtscl()
store i64 %31, i64* %1
%32 = load i32, i32* @CLOCK_MONOTONIC
%33 = call i32 @clock_gettime(i32 %32, %struct.timespec* %4)
%34 = call i64 (...) @rdtscl()
store i64 %34, i64* %2
%35 = load i64, i64* %1
%36 = uitofp i64 %35 to double
%37 = load i64, i64* %2
%38 = uitofp i64 %37 to double
%39 = fadd double %36, %38
%40 = fdiv double %39, 2.000000e+00
%41 = fptoui double %40 to i64
store i64 %41, i64* %6
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%45 = load i32, i32* %44
%46 = sub nsw i32 %43, %45
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
store i32 %46, i32* %47
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%51 = load i32, i32* %50
%52 = sub nsw i32 %49, %51
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%55 = load i32, i32* %54
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %30
%58 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%59 = load i32, i32* %58
%60 = sitofp i32 %59 to double
%61 = fadd double %60, 1.000000e+09
%62 = fptosi double %61 to i32
store i32 %62, i32* %58
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%64 = load i32, i32* %63
%65 = add nsw i32 %64, -1
store i32 %65, i32* %63
br label %66
66: ; preds = %57, %30
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%68 = load i32, i32* %67
%69 = sdiv i32 %68, 1000
%70 = sitofp i32 %69 to double
%71 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%72 = load i32, i32* %71
%73 = sitofp i32 %72 to double
%74 = call double @llvm.fmuladd.f64(double %73, double 1.000000e+06, double %70)
store double %74, double* %7
%75 = load i64, i64* %6
%76 = load i64, i64* %5
%77 = sub i64 %75, %76
%78 = uitofp i64 %77 to double
%79 = load double, double* %7
%80 = fdiv double %78, %79
ret double %80
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define double @get_cycles_per_usec() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = tail call i32 (...) @load_cpu()
%6 = tail call i64 (...) @rdtscl()
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %1)
%9 = call i64 (...) @rdtscl()
%10 = call i32 (...) @load_cpu()
%11 = call i32 (...) @load_cpu()
%12 = call i32 (...) @load_cpu()
%13 = call i32 (...) @load_cpu()
%14 = call i32 (...) @load_cpu()
%15 = call i32 (...) @load_cpu()
%16 = call i32 (...) @load_cpu()
%17 = call i32 (...) @load_cpu()
%18 = call i32 (...) @load_cpu()
%19 = call i32 (...) @load_cpu()
%20 = call i64 (...) @rdtscl()
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %2)
%23 = call i64 (...) @rdtscl()
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%27 = load i32, i32* %26
%28 = sub i32 %25, %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%32 = load i32, i32* %31
%33 = sub nsw i32 %30, %32
%34 = icmp slt i32 %33, 0
%35 = add i32 %33, 1000000000
%36 = select i1 %34, i32 %35, i32 %33
%37 = ashr i32 %33, 31
%38 = add nsw i32 %28, %37
%39 = uitofp i64 %20 to double
%40 = uitofp i64 %23 to double
%41 = fadd double %39, %40
%42 = fmul double %41, 5.000000e-01
%43 = fptoui double %42 to i64
%44 = uitofp i64 %6 to double
%45 = uitofp i64 %9 to double
%46 = fadd double %44, %45
%47 = fmul double %46, 5.000000e-01
%48 = fptoui double %47 to i64
%49 = sdiv i32 %36, 1000
%50 = sitofp i32 %49 to double
%51 = sitofp i32 %38 to double
%52 = call double @llvm.fmuladd.f64(double %51, double 1.000000e+06, double %50)
%53 = sub i64 %43, %48
%54 = uitofp i64 %53 to double
%55 = fdiv double %54, %52
ret double %55
}