#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 double uint64_t;
struct timespec {
  unsigned long long tv_nsec;
  unsigned long long tv_sec;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
double rdtsc();
int calc_num_cycles_per_us(int *cycles_per_us) {
  struct timespec sleeptime = {0, 0};
  struct timespec ts_start, ts_end;
  sleeptime.tv_nsec = (1000000000ULL) / 2;
  if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts_start) == 0) {
    uint64_t tsc_start, tsc_end, ns;
    double usecs;
    tsc_start = rdtsc();
    nanosleep(&sleeptime, NULL);
    clock_gettime(CLOCK_MONOTONIC_RAW, &ts_end);
    tsc_end = rdtsc();
    ns = ((ts_end.tv_sec - ts_start.tv_sec) * (1000000000ULL));
    ns += (ts_end.tv_nsec - ts_start.tv_nsec);
    usecs = (double)ns / 1000;
    *cycles_per_us = (uint64_t)((tsc_end - tsc_start) / usecs);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<calc_num_cycles_per_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
movq   $0x1dcd6500,-0x28(%rbp)
1d
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
jne    1268 <calc_num_cycles_per_us+0x128>
xor    %eax,%eax
mov    %eax,-0x80(%rbp)
mov    %al,-0x79(%rbp)
call   1050 <rdtsc@plt>
mov    -0x80(%rbp),%eax
movsd  %xmm0,-0x60(%rbp)
mov    %eax,%esi
lea    -0x28(%rbp),%rdi
call   1040 <nanosleep@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x79(%rbp),%al
call   1050 <rdtsc@plt>
movsd  %xmm0,-0x68(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
imul   $0x3b9aca00,%rax,%rax
movq   %rax,%xmm0
movaps 0xe30(%rip),%xmm2
punpckldq %xmm2,%xmm0
movapd 0xe34(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm3
unpckhpd %xmm0,%xmm0
addsd  %xmm3,%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm0
punpckldq %xmm2,%xmm0
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x70(%rbp),%xmm0
movsd  0xdc6(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x68(%rbp),%xmm0
subsd  -0x60(%rbp),%xmm0
divsd  -0x78(%rbp),%xmm0
cvttsd2si %xmm0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    126f <calc_num_cycles_per_us+0x12f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_num_cycles_per_us>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdi,%r14
xorpd  %xmm0,%xmm0
movupd %xmm0,0x40(%rsp)
movq   $0x1dcd6500,0x38(%rsp)
cd 1d
mov    0x2e92(%rip),%r15
mov    (%r15),%edi
lea    0x20(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1218 <calc_num_cycles_per_us+0xe8>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <rdtsc@plt>
movsd  %xmm0,(%rsp)
lea    0x38(%rsp),%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %eax,%eax
call   1050 <rdtsc@plt>
mov    0x10(%rsp),%rax
sub    0x28(%rsp),%rax
mov    0x8(%rsp),%rcx
imul   $0x3b9aca00,%rax,%rax
movq   %rax,%xmm1
movdqa 0xe3e(%rip),%xmm2
punpckldq %xmm2,%xmm1
movapd 0xe42(%rip),%xmm3
subpd  %xmm3,%xmm1
movapd %xmm1,%xmm4
unpckhpd %xmm1,%xmm4
addsd  %xmm1,%xmm4
sub    0x20(%rsp),%rcx
movq   %rcx,%xmm1
punpckldq %xmm2,%xmm1
subpd  %xmm3,%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
addsd  %xmm4,%xmm2
divsd  0xe18(%rip),%xmm2
subsd  (%rsp),%xmm0
divsd  %xmm2,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,(%r14)
mov    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64, i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define i32 @calc_num_cycles_per_us(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store i32* %0, i32** %3
%11 = bitcast %struct.timespec* %4 to i8*
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 500000000, i64* %12
%13 = load i32, i32* @CLOCK_MONOTONIC_RAW
%14 = call i64 @clock_gettime(i32 %13, %struct.timespec* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %46
16: ; preds = %1
%17 = call double (...) @rdtsc()
store double %17, double* %7
%18 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
%19 = load i32, i32* @CLOCK_MONOTONIC_RAW
%20 = call i64 @clock_gettime(i32 %19, %struct.timespec* %6)
%21 = call double (...) @rdtsc()
store double %21, double* %8
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%25 = load i64, i64* %24
%26 = sub i64 %23, %25
%27 = mul i64 %26, 1000000000
%28 = uitofp i64 %27 to double
store double %28, double* %9
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%32 = load i64, i64* %31
%33 = sub i64 %30, %32
%34 = uitofp i64 %33 to double
%35 = load double, double* %9
%36 = fadd double %35, %34
store double %36, double* %9
%37 = load double, double* %9
%38 = fdiv double %37, 1.000000e+03
store double %38, double* %10
%39 = load double, double* %8
%40 = load double, double* %7
%41 = fsub double %39, %40
%42 = load double, double* %10
%43 = fdiv double %41, %42
%44 = fptosi double %43 to i32
%45 = load i32*, i32** %3
store i32 %44, i32* %45
store i32 0, i32* %2
br label %47
46: ; preds = %1
store i32 -1, i32* %2
br label %47
47: ; preds = %46, %16
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64, i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
define i32 @calc_num_cycles_per_us(i32* nocapture writeonly %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %2 to i8*
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%7 = bitcast i64* %6 to i8*
%8 = bitcast %struct.timespec* %3 to i8*
%9 = bitcast %struct.timespec* %4 to i8*
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 500000000, i64* %10
%11 = load i32, i32* @CLOCK_MONOTONIC_RAW
%12 = call i64 @clock_gettime(i32 %11, %struct.timespec* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %38
14: ; preds = %1
%15 = call double (...) @rdtsc()
%16 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%17 = load i32, i32* @CLOCK_MONOTONIC_RAW
%18 = call i64 @clock_gettime(i32 %17, %struct.timespec* %4)
%19 = call double (...) @rdtsc()
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%23 = load i64, i64* %22
%24 = sub i64 %21, %23
%25 = mul i64 %24, 1000000000
%26 = uitofp i64 %25 to double
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = sub i64 %28, %30
%32 = uitofp i64 %31 to double
%33 = fadd double %26, %32
%34 = fdiv double %33, 1.000000e+03
%35 = fsub double %19, %15
%36 = fdiv double %35, %34
%37 = fptosi double %36 to i32
store i32 %37, i32* %0
br label %38
38: ; preds = %1, %14
%39 = phi i32 [ 0, %14 ], [ -1, %1 ]
ret i32 %39
}