#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 int /*<<< orphan*/ tz;
struct timezone {
  int dummy;
};
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
unsigned long long UINT32_MAX;
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ memset(struct timezone *, int /*<<< orphan*/, int);
unsigned long long rdtsc();
int /*<<< orphan*/ usleep(int);
double estimate_MHz() {
  struct timezone tz;
  struct timeval tvstart, tvstop;
  unsigned long long int cycles[2];
  unsigned long long int microseconds;
  memset(&tz, 0, sizeof(tz));
  gettimeofday(&tvstart, &tz);
  cycles[0] = rdtsc();
  gettimeofday(&tvstart, &tz);
  usleep(10000);
  cycles[1] = rdtsc();
  gettimeofday(&tvstop, &tz);
  microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
                 (tvstop.tv_usec - tvstart.tv_usec);
  unsigned long long int elapsed = 0;
  if (cycles[1] < cycles[0]) {
    elapsed = UINT32_MAX - cycles[0];
    elapsed = elapsed + cycles[1];
  } else {
    elapsed = cycles[1] - cycles[0];
  }
  double mhz = elapsed / microseconds;
  return (mhz);
}


x86 assembly (clang, -O0):
<estimate_MHz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1030 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1030 <gettimeofday@plt>
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    -0x14(%rbp),%ecx
sub    -0xc(%rbp),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11f2 <estimate_MHz+0xb2>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11fe <estimate_MHz+0xbe>
mov    -0x28(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
xor    %edx,%edx
div    %rcx
movq   %rax,%xmm0
movaps 0xde9(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xded(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<estimate_MHz>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%r14
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <gettimeofday@plt>
mov    $0x2710,%edi
call   1060 <usleep@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    %r14,%rsi
call   1030 <gettimeofday@plt>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    %r15,%rbx
jae    11bc <estimate_MHz+0x7c>
mov    0x2e3f(%rip),%rdx
mov    (%rdx),%r12
sub    0x8(%rsp),%eax
add    %r12,%rbx
imul   $0xf4240,%eax,%eax
sub    0xc(%rsp),%ecx
add    %eax,%ecx
movslq %ecx,%rsi
mov    %rbx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     11e8 <estimate_MHz+0xa8>
mov    %rbx,%rax
xor    %edx,%edx
div    %rsi
jmp    11ee <estimate_MHz+0xae>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
movq   %rax,%xmm1
punpckldq 0xe05(%rip),%xmm1
subpd  0xe0d(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timezone = type { i32 }
%struct.timeval = type { i32, i32 }
@UINT32_MAX = global i64 0
define double @estimate_MHz() {
%1 = alloca %struct.timezone
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca [2 x i64]
%5 = alloca i64
%6 = alloca i64
%7 = alloca double
%8 = call i32 @memset(%struct.timezone* %1, i32 0, i32 4)
%9 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* %1)
%10 = call i64 (...) @rdtsc()
%11 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
store i64 %10, i64* %11
%12 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* %1)
%13 = call i32 @usleep(i32 10000)
%14 = call i64 (...) @rdtsc()
%15 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
store i64 %14, i64* %15
%16 = call i32 @gettimeofday(%struct.timeval* %3, %struct.timezone* %1)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = mul nsw i32 %21, 1000000
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%26 = load i32, i32* %25
%27 = sub nsw i32 %24, %26
%28 = add nsw i32 %22, %27
%29 = sext i32 %28 to i64
store i64 %29, i64* %5
store i64 0, i64* %6
%30 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
%31 = load i64, i64* %30
%32 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
%33 = load i64, i64* %32
%34 = icmp ult i64 %31, %33
br i1 %34, label %35, label %44
35: ; preds = %0
%36 = load i64, i64* @UINT32_MAX
%37 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
%38 = load i64, i64* %37
%39 = sub i64 %36, %38
store i64 %39, i64* %6
%40 = load i64, i64* %6
%41 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
%42 = load i64, i64* %41
%43 = add i64 %40, %42
store i64 %43, i64* %6
br label %50
44: ; preds = %0
%45 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 1
%46 = load i64, i64* %45
%47 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
%48 = load i64, i64* %47
%49 = sub i64 %46, %48
store i64 %49, i64* %6
br label %50
50: ; preds = %44, %35
%51 = load i64, i64* %6
%52 = load i64, i64* %5
%53 = udiv i64 %51, %52
%54 = uitofp i64 %53 to double
store double %54, double* %7
%55 = load double, double* %7
ret double %55
}

LLVM IR (-O2):
%struct.timezone = type { i32 }
%struct.timeval = type { i32, i32 }
@UINT32_MAX = global i64 0
define double @estimate_MHz() {
%1 = alloca %struct.timezone
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timezone* %1 to i8*
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = call i32 @memset(%struct.timezone* %1, i32 0, i32 4)
%8 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* %1)
%9 = call i64 (...) @rdtsc()
%10 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* %1)
%11 = call i32 @usleep(i32 10000)
%12 = call i64 (...) @rdtsc()
%13 = call i32 @gettimeofday(%struct.timeval* %3, %struct.timezone* %1)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%21 = load i32, i32* %20
%22 = icmp ult i64 %12, %9
%23 = load i64, i64* @UINT32_MAX
%24 = select i1 %22, i64 %23, i64 0
%25 = sub i64 %12, %9
%26 = add i64 %25, %24
%27 = sub nsw i32 %15, %17
%28 = mul nsw i32 %27, 1000000
%29 = sub i32 %19, %21
%30 = add nsw i32 %29, %28
%31 = sext i32 %30 to i64
%32 = udiv i64 %26, %31
%33 = uitofp i64 %32 to double
ret double %33
}