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

/* Variables and functions */
int /*<<< orphan*/ sandglass_get_currtime(struct timespec *);
long sandglass_get_tsc();
int /*<<< orphan*/ sandglass_timespec_add(struct timespec *, struct timespec *);
scalar_t__ sandglass_timespec_cmp(struct timespec *, struct timespec *);
int /*<<< orphan*/ sandglass_timespec_sub(struct timespec *, struct timespec *);
double sandglass_tsc_freq() {
  static long tsc = 0;
  static struct timespec ts = {.tv_sec = 0, .tv_nsec = 10000000L};
  struct timespec curr, until;
  if (tsc == 0) {
    sandglass_get_currtime(&curr);
    until = curr;
    sandglass_timespec_add(&until, &ts);
    tsc = sandglass_get_tsc();
    do {
      sandglass_get_currtime(&curr);
    } while (sandglass_timespec_cmp(&curr, &until) < 0);
    tsc = sandglass_get_tsc() - tsc;
    sandglass_timespec_sub(&curr, &until);
    sandglass_timespec_add(&ts, &curr);
  }
  return tsc * 1.0e9 / ts.tv_nsec;
}


x86 assembly (clang, -O0):
<sandglass_tsc_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpq   $0x0,0x2f00(%rip)
jne    11ef <sandglass_tsc_freq+0x9f>
lea    -0x10(%rbp),%rdi
call   1060 <sandglass_get_currtime@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
lea    0x2ebe(%rip),%rsi
call   1050 <sandglass_timespec_add@plt>
mov    $0x0,%al
call   1070 <sandglass_get_tsc@plt>
mov    %rax,0x2ec3(%rip)
lea    -0x10(%rbp),%rdi
call   1060 <sandglass_get_currtime@plt>
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <sandglass_timespec_cmp@plt>
cmp    $0x0,%rax
jl     119d <sandglass_tsc_freq+0x4d>
mov    $0x0,%al
call   1070 <sandglass_get_tsc@plt>
sub    0x2e95(%rip),%rax
mov    %rax,0x2e8e(%rip)
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <sandglass_timespec_sub@plt>
lea    0x2e62(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <sandglass_timespec_add@plt>
cvtsi2sdq 0x2e68(%rip),%xmm0
movsd  0xe00(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsi2sdq 0x2e3b(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sandglass_tsc_freq>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2f02(%rip),%rax
test   %rax,%rax
jne    11f3 <sandglass_tsc_freq+0xa3>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sandglass_get_currtime@plt>
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
lea    0x2ec4(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <sandglass_timespec_add@plt>
xor    %eax,%eax
call   1070 <sandglass_get_tsc@plt>
mov    %rax,0x2ec3(%rip)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <sandglass_get_currtime@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <sandglass_timespec_cmp@plt>
test   %rax,%rax
js     11a0 <sandglass_tsc_freq+0x50>
xor    %eax,%eax
call   1070 <sandglass_get_tsc@plt>
sub    0x2e9a(%rip),%rax
mov    %rax,0x2e93(%rip)
lea    0x18(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <sandglass_timespec_sub@plt>
lea    0x2e64(%rip),%rdi
mov    %rbx,%rsi
call   1050 <sandglass_timespec_add@plt>
mov    0x2e6d(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0xdfd(%rip),%xmm0
cvtsi2sdq 0x2e3c(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@sandglass_tsc_freq.tsc = internal global i64 0
@sandglass_tsc_freq.ts = internal global %struct.timespec { i64 10000000, i32 0 }
define double @sandglass_tsc_freq() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = load i64, i64* @sandglass_tsc_freq.tsc
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %0
%6 = call i32 @sandglass_get_currtime(%struct.timespec* %1)
%7 = bitcast %struct.timespec* %2 to i8*
%8 = bitcast %struct.timespec* %1 to i8*
%9 = call i32 @sandglass_timespec_add(%struct.timespec* %2, %struct.timespec* @sandglass_tsc_freq.ts)
%10 = call i64 (...) @sandglass_get_tsc()
store i64 %10, i64* @sandglass_tsc_freq.tsc
br label %11
11: ; preds = %13, %5
%12 = call i32 @sandglass_get_currtime(%struct.timespec* %1)
br label %13
13: ; preds = %11
%14 = call i64 @sandglass_timespec_cmp(%struct.timespec* %1, %struct.timespec* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %11, label %16
16: ; preds = %13
%17 = call i64 (...) @sandglass_get_tsc()
%18 = load i64, i64* @sandglass_tsc_freq.tsc
%19 = sub nsw i64 %17, %18
store i64 %19, i64* @sandglass_tsc_freq.tsc
%20 = call i32 @sandglass_timespec_sub(%struct.timespec* %1, %struct.timespec* %2)
%21 = call i32 @sandglass_timespec_add(%struct.timespec* @sandglass_tsc_freq.ts, %struct.timespec* %1)
br label %22
22: ; preds = %16, %0
%23 = load i64, i64* @sandglass_tsc_freq.tsc
%24 = sitofp i64 %23 to double
%25 = fmul double %24, 1.000000e+09
%26 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @sandglass_tsc_freq.ts, i32 0, i32 0)
%27 = sitofp i64 %26 to double
%28 = fdiv double %25, %27
ret double %28
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@sandglass_tsc_freq.tsc = internal unnamed_addr global i64 0
@sandglass_tsc_freq.ts = internal global %struct.timespec { i64 10000000, i32 0 }
define double @sandglass_tsc_freq() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = load i64, i64* @sandglass_tsc_freq.tsc
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %0
%8 = call i32 @sandglass_get_currtime(%struct.timespec* %1)
%9 = call i32 @sandglass_timespec_add(%struct.timespec* %2, %struct.timespec* @sandglass_tsc_freq.ts)
%10 = call i64 (...) @sandglass_get_tsc()
store i64 %10, i64* @sandglass_tsc_freq.tsc
br label %11
11: ; preds = %11, %7
%12 = call i32 @sandglass_get_currtime(%struct.timespec* %1)
%13 = call i64 @sandglass_timespec_cmp(%struct.timespec* %1, %struct.timespec* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %11, label %15
15: ; preds = %11
%16 = call i64 (...) @sandglass_get_tsc()
%17 = load i64, i64* @sandglass_tsc_freq.tsc
%18 = sub nsw i64 %16, %17
store i64 %18, i64* @sandglass_tsc_freq.tsc
%19 = call i32 @sandglass_timespec_sub(%struct.timespec* %1, %struct.timespec* %2)
%20 = call i32 @sandglass_timespec_add(%struct.timespec* @sandglass_tsc_freq.ts, %struct.timespec* %1)
%21 = load i64, i64* @sandglass_tsc_freq.tsc
br label %22
22: ; preds = %15, %0
%23 = phi i64 [ %21, %15 ], [ %5, %0 ]
%24 = sitofp i64 %23 to double
%25 = fmul double %24, 1.000000e+09
%26 = load i64, i64* getelementptr inbounds (%struct.timespec, %struct.timespec* @sandglass_tsc_freq.ts, i64 0, i32 0)
%27 = sitofp i64 %26 to double
%28 = fdiv double %25, %27
ret double %28
}