#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 read_rdtsc();
long long system_time();
long long get_cpu_speed() {
  long long a, b, start, end;
  a = system_time();
  do {
    b = system_time();
  } while (a == b);
  start = read_rdtsc();
  do {
    a = system_time();
  } while (a - b < 1000000 / 4);
  end = read_rdtsc();
  return (end - start) * 4;
}


x86 assembly (clang, -O0):
<get_cpu_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <system_time@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <system_time@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1133 <get_cpu_speed+0x13>
mov    $0x0,%al
call   1040 <read_rdtsc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <system_time@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x3d090,%rax
jl     1157 <get_cpu_speed+0x37>
mov    $0x0,%al
call   1040 <read_rdtsc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
shl    $0x2,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_speed>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <system_time@plt>
mov    %rax,%r14
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <system_time@plt>
cmp    %rax,%r14
je     1130 <get_cpu_speed+0x10>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <read_rdtsc@plt>
mov    %rax,%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <system_time@plt>
sub    %rbx,%rax
cmp    $0x3d090,%rax
jl     1150 <get_cpu_speed+0x30>
xor    %eax,%eax
call   1040 <read_rdtsc@plt>
sub    %r14,%rax
shl    $0x2,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @get_cpu_speed() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = call i64 (...) @system_time()
store i64 %5, i64* %1
br label %6
6: ; preds = %8, %0
%7 = call i64 (...) @system_time()
store i64 %7, i64* %2
br label %8
8: ; preds = %6
%9 = load i64, i64* %1
%10 = load i64, i64* %2
%11 = icmp eq i64 %9, %10
br i1 %11, label %6, label %12
12: ; preds = %8
%13 = call i64 (...) @read_rdtsc()
store i64 %13, i64* %3
br label %14
14: ; preds = %16, %12
%15 = call i64 (...) @system_time()
store i64 %15, i64* %1
br label %16
16: ; preds = %14
%17 = load i64, i64* %1
%18 = load i64, i64* %2
%19 = sub nsw i64 %17, %18
%20 = icmp slt i64 %19, 250000
br i1 %20, label %14, label %21
21: ; preds = %16
%22 = call i64 (...) @read_rdtsc()
store i64 %22, i64* %4
%23 = load i64, i64* %4
%24 = load i64, i64* %3
%25 = sub nsw i64 %23, %24
%26 = mul nsw i64 %25, 4
ret i64 %26
}

LLVM IR (-O2):
define i64 @get_cpu_speed() {
%1 = tail call i64 (...) @system_time()
br label %2
2: ; preds = %2, %0
%3 = tail call i64 (...) @system_time()
%4 = icmp eq i64 %1, %3
br i1 %4, label %2, label %5
5: ; preds = %2
%6 = tail call i64 (...) @read_rdtsc()
br label %7
7: ; preds = %7, %5
%8 = tail call i64 (...) @system_time()
%9 = sub nsw i64 %8, %3
%10 = icmp slt i64 %9, 250000
br i1 %10, label %7, label %11
11: ; preds = %7
%12 = tail call i64 (...) @read_rdtsc()
%13 = sub nsw i64 %12, %6
%14 = shl nsw i64 %13, 2
ret i64 %14
}