#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 */

/* Variables and functions */
long long cpucycles_x86estimate();
long long microseconds();
double guessfreq(void) {
  long long tb0;
  long long us0;
  long long tb1;
  long long us1;
  tb0 = cpucycles_x86estimate();
  us0 = microseconds();
  do {
    tb1 = cpucycles_x86estimate();
    us1 = microseconds();
  } while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
  if (tb1 <= tb0)
    return 0;
  tb1 -= tb0;
  us1 -= us0;
  return ((double)tb1) / (0.000001 * (double)us1);
}


x86 assembly (clang, -O0):
<guessfreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1030 <cpucycles_x86estimate@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <microseconds@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <cpucycles_x86estimate@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <microseconds@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
sub    -0x18(%rbp),%rcx
mov    $0x1,%al
cmp    $0x2710,%rcx
mov    %al,-0x29(%rbp)
jl     1182 <guessfreq+0x62>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x3e8,%rax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    113e <guessfreq+0x1e>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jg     11a8 <guessfreq+0x88>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11e7 <guessfreq+0xc7>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm0
cvtsi2sdq -0x28(%rbp),%xmm2
movsd  0xe26(%rip),%xmm1
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guessfreq>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1030 <cpucycles_x86estimate@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <microseconds@plt>
mov    %rax,%r15
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <cpucycles_x86estimate@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <microseconds@plt>
sub    %r15,%rax
mov    %rbx,%rcx
sub    %r14,%rcx
cmp    $0x2710,%rax
jl     1140 <guessfreq+0x20>
cmp    $0x3e8,%rcx
jl     1140 <guessfreq+0x20>
xorpd  %xmm0,%xmm0
cmp    %r14,%rbx
jle    118d <guessfreq+0x6d>
xorps  %xmm0,%xmm0
cvtsi2sd %rcx,%xmm0
cvtsi2sd %rax,%xmm1
mulsd  0xe77(%rip),%xmm1
divsd  %xmm1,%xmm0
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @guessfreq() {
%1 = alloca double
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = call i64 (...) @cpucycles_x86estimate()
store i64 %6, i64* %2
%7 = call i64 (...) @microseconds()
store i64 %7, i64* %3
br label %8
8: ; preds = %21, %0
%9 = call i64 (...) @cpucycles_x86estimate()
store i64 %9, i64* %4
%10 = call i64 (...) @microseconds()
store i64 %10, i64* %5
br label %11
11: ; preds = %8
%12 = load i64, i64* %5
%13 = load i64, i64* %3
%14 = sub nsw i64 %12, %13
%15 = icmp slt i64 %14, 10000
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i64, i64* %4
%18 = load i64, i64* %2
%19 = sub nsw i64 %17, %18
%20 = icmp slt i64 %19, 1000
br label %21
21: ; preds = %16, %11
%22 = phi i1 [ true, %11 ], [ %20, %16 ]
br i1 %22, label %8, label %23
23: ; preds = %21
%24 = load i64, i64* %4
%25 = load i64, i64* %2
%26 = icmp sle i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store double 0.000000e+00, double* %1
br label %41
28: ; preds = %23
%29 = load i64, i64* %2
%30 = load i64, i64* %4
%31 = sub nsw i64 %30, %29
store i64 %31, i64* %4
%32 = load i64, i64* %3
%33 = load i64, i64* %5
%34 = sub nsw i64 %33, %32
store i64 %34, i64* %5
%35 = load i64, i64* %4
%36 = sitofp i64 %35 to double
%37 = load i64, i64* %5
%38 = sitofp i64 %37 to double
%39 = fmul double 0x3EB0C6F7A0B5ED8D, %38
%40 = fdiv double %36, %39
store double %40, double* %1
br label %41
41: ; preds = %28, %27
%42 = load double, double* %1
ret double %42
}

LLVM IR (-O2):
define double @guessfreq() {
%1 = tail call i64 (...) @cpucycles_x86estimate()
%2 = tail call i64 (...) @microseconds()
br label %3
3: ; preds = %3, %0
%4 = tail call i64 (...) @cpucycles_x86estimate()
%5 = tail call i64 (...) @microseconds()
%6 = sub nsw i64 %5, %2
%7 = icmp slt i64 %6, 10000
%8 = sub nsw i64 %4, %1
%9 = icmp slt i64 %8, 1000
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %3, label %11
11: ; preds = %3
%12 = icmp sgt i64 %4, %1
br i1 %12, label %13, label %18
13: ; preds = %11
%14 = sitofp i64 %8 to double
%15 = sitofp i64 %6 to double
%16 = fmul double %15, 0x3EB0C6F7A0B5ED8D
%17 = fdiv double %14, %16
br label %18
18: ; preds = %11, %13
%19 = phi double [ %17, %13 ], [ 0.000000e+00, %11 ]
ret double %19
}