#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;
struct timeval {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
scalar_t__ rdtsc();
int /*<<< orphan*/ sleep(int);
scalar_t__ ts_delta(struct timeval *, struct timeval *);
double measure_tsc() {
  struct timeval t1, t2;
  gettimeofday(&t1, 0);
  uint64_t tsc1 = rdtsc();
  sleep(1);
  gettimeofday(&t2, 0);
  uint64_t tsc2 = rdtsc();
  return (tsc1 < tsc2 ? (tsc2 - tsc1) : (tsc1 - tsc2)) / ts_delta(&t1, &t2);
}


x86 assembly (clang, -O0):
<measure_tsc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    119d <measure_tsc+0x5d>
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11a9 <measure_tsc+0x69>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <ts_delta@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cqto
idiv   %rcx
cvtsi2sd %rax,%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_tsc>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %rbx,%rcx
sub    %rax,%rcx
neg    %rcx
sub    %rax,%rbx
cmovl  %rcx,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <ts_delta@plt>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
je     11b1 <measure_tsc+0x71>
mov    %rbx,%rax
cqto
idiv   %rcx
jmp    11b7 <measure_tsc+0x77>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
cvtsi2sd %rax,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define double @measure_tsc() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca i64
%4 = alloca i64
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%6 = call i64 (...) @rdtsc()
store i64 %6, i64* %3
%7 = call i32 @sleep(i32 1)
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%9 = call i64 (...) @rdtsc()
store i64 %9, i64* %4
%10 = load i64, i64* %3
%11 = load i64, i64* %4
%12 = icmp slt i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i64, i64* %4
%15 = load i64, i64* %3
%16 = sub nsw i64 %14, %15
br label %21
17: ; preds = %0
%18 = load i64, i64* %3
%19 = load i64, i64* %4
%20 = sub nsw i64 %18, %19
br label %21
21: ; preds = %17, %13
%22 = phi i64 [ %16, %13 ], [ %20, %17 ]
%23 = call i64 @ts_delta(%struct.timeval* %1, %struct.timeval* %2)
%24 = sdiv i64 %22, %23
%25 = sitofp i64 %24 to double
ret double %25
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define double @measure_tsc() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%6 = call i64 (...) @rdtsc()
%7 = call i32 @sleep(i32 1)
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%9 = call i64 (...) @rdtsc()
%10 = icmp slt i64 %6, %9
%11 = sub nsw i64 %9, %6
%12 = sub nsw i64 %6, %9
%13 = select i1 %10, i64 %11, i64 %12
%14 = call i64 @ts_delta(%struct.timeval* %1, %struct.timeval* %2)
%15 = sdiv i64 %13, %14
%16 = sitofp i64 %15 to double
ret double %16
}