#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__ uint64_t;

/* Variables and functions */
scalar_t__ current_ms();
scalar_t__ processor_bind(int /*<<< orphan*/);
int /*<<< orphan*/ processor_unbind();
scalar_t__ rdtsc();
void calibrate_by_tsc(double *nsofclk, double *clkofns, uint64_t *clkofsec) {
  uint64_t start_ms, end_ms, diff_ms;
  uint64_t start_tsc, end_tsc;
  if (processor_bind(0) != 0) {
    return;
  }
  end_ms = current_ms();
  while ((start_ms = current_ms()) == end_ms) {
  }
  start_tsc = rdtsc();
  while ((end_ms = current_ms()) < (start_ms + 100)) {
  }
  end_tsc = rdtsc();
  diff_ms = end_ms - start_ms;
  *nsofclk = (double)(diff_ms * 1000000) / (double)(end_tsc - start_tsc);
  *clkofns = (double)1.0 / *nsofclk;
  *clkofsec = (uint64_t)((double)1000000000 / *nsofclk);
  (void)processor_unbind();
}


x86 assembly (clang, -O0):
<calibrate_by_tsc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %edi,%edi
call   1060 <processor_bind@plt>
cmp    $0x0,%rax
je     116a <calibrate_by_tsc+0x2a>
jmp    1233 <calibrate_by_tsc+0xf3>
mov    $0x0,%al
call   1030 <current_ms@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <current_ms@plt>
mov    %rax,-0x20(%rbp)
cmp    -0x28(%rbp),%rax
jne    118f <calibrate_by_tsc+0x4f>
jmp    1175 <calibrate_by_tsc+0x35>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <current_ms@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
add    $0x64,%rcx
cmp    %rcx,%rax
jge    11bb <calibrate_by_tsc+0x7b>
jmp    119a <calibrate_by_tsc+0x5a>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
imul   $0xf4240,-0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x8(%rbp),%rax
movsd  0xe04(%rip),%xmm0
divsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x8(%rbp),%rax
movsd  0xde4(%rip),%xmm0
divsd  (%rax),%xmm0
cvttsd2si %xmm0,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x0,%al
call   1050 <processor_unbind@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_by_tsc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
call   1060 <processor_bind@plt>
test   %rax,%rax
je     116f <calibrate_by_tsc+0x2f>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,(%rsp)
xor    %eax,%eax
call   1030 <current_ms@plt>
mov    %rax,%rbp
nopl   (%rax)
xor    %eax,%eax
call   1030 <current_ms@plt>
cmp    %rbp,%rax
je     1180 <calibrate_by_tsc+0x40>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %rax,%r13
lea    0x64(%rbx),%r14
nopl   (%rax)
xor    %eax,%eax
call   1030 <current_ms@plt>
cmp    %r14,%rax
jl     11a0 <calibrate_by_tsc+0x60>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <rdtsc@plt>
sub    %rbx,%rbp
imul   $0xf4240,%rbp,%rcx
cvtsi2sd %rcx,%xmm0
sub    %r13,%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%r12)
movsd  0xe21(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%r15)
movsd  0xe18(%rip),%xmm0
divsd  (%r12),%xmm0
cvttsd2si %xmm0,%rax
mov    (%rsp),%rcx
mov    %rax,(%rcx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <processor_unbind@plt>

LLVM IR (-O0):
define void @calibrate_by_tsc(double* %0, double* %1, i64* %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store double* %0, double** %4
store double* %1, double** %5
store i64* %2, i64** %6
%12 = call i64 @processor_bind(i32 0)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
br label %54
15: ; preds = %3
%16 = call i64 (...) @current_ms()
store i64 %16, i64* %8
br label %17
17: ; preds = %21, %15
%18 = call i64 (...) @current_ms()
store i64 %18, i64* %7
%19 = load i64, i64* %8
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
br label %17
22: ; preds = %17
%23 = call i64 (...) @rdtsc()
store i64 %23, i64* %10
br label %24
24: ; preds = %29, %22
%25 = call i64 (...) @current_ms()
store i64 %25, i64* %8
%26 = load i64, i64* %7
%27 = add nsw i64 %26, 100
%28 = icmp slt i64 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %24
br label %24
30: ; preds = %24
%31 = call i64 (...) @rdtsc()
store i64 %31, i64* %11
%32 = load i64, i64* %8
%33 = load i64, i64* %7
%34 = sub nsw i64 %32, %33
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = mul nsw i64 %35, 1000000
%37 = sitofp i64 %36 to double
%38 = load i64, i64* %11
%39 = load i64, i64* %10
%40 = sub nsw i64 %38, %39
%41 = sitofp i64 %40 to double
%42 = fdiv double %37, %41
%43 = load double*, double** %4
store double %42, double* %43
%44 = load double*, double** %4
%45 = load double, double* %44
%46 = fdiv double 1.000000e+00, %45
%47 = load double*, double** %5
store double %46, double* %47
%48 = load double*, double** %4
%49 = load double, double* %48
%50 = fdiv double 1.000000e+09, %49
%51 = fptosi double %50 to i64
%52 = load i64*, i64** %6
store i64 %51, i64* %52
%53 = call i32 (...) @processor_unbind()
br label %54
54: ; preds = %30, %14
ret void
}

LLVM IR (-O2):
define void @calibrate_by_tsc(double* nocapture %0, double* nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i64 @processor_bind(i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %30
6: ; preds = %3
%7 = tail call i64 (...) @current_ms()
br label %8
8: ; preds = %8, %6
%9 = tail call i64 (...) @current_ms()
%10 = icmp eq i64 %9, %7
br i1 %10, label %8, label %11
11: ; preds = %8
%12 = tail call i64 (...) @rdtsc()
%13 = add nsw i64 %9, 100
br label %14
14: ; preds = %14, %11
%15 = tail call i64 (...) @current_ms()
%16 = icmp slt i64 %15, %13
br i1 %16, label %14, label %17
17: ; preds = %14
%18 = tail call i64 (...) @rdtsc()
%19 = sub nsw i64 %15, %9
%20 = mul nsw i64 %19, 1000000
%21 = sitofp i64 %20 to double
%22 = sub nsw i64 %18, %12
%23 = sitofp i64 %22 to double
%24 = fdiv double %21, %23
store double %24, double* %0
%25 = fdiv double 1.000000e+00, %24
store double %25, double* %1
%26 = load double, double* %0
%27 = fdiv double 1.000000e+09, %26
%28 = fptosi double %27 to i64
store i64 %28, i64* %2
%29 = tail call i32 (...) @processor_unbind()
br label %30
30: ; preds = %3, %17
ret void
}