#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;

/* Variables and functions */
scalar_t__ read_tsc_after_std();
scalar_t__ read_tsc_before_std();
uint64_t measure_tsc_overhead() {
  enum { NMEASURES = 100 };
  volatile uint64_t t0, t1, ticks, minticks = (uint64_t)~0x1;
  for (int i = 0; i < NMEASURES;) {
    t0 = read_tsc_before_std();
    t1 = read_tsc_after_std();
    if (t1 > t0) {
      ticks = t1 - t0;
      if (ticks < minticks)
        minticks = ticks;
      i++;
    }
  }
  return minticks;
}


x86 assembly (clang, -O0):
<measure_tsc_overhead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0xfffffffffffffffe,-0x20(%rbp)
ff
movl   $0x0,-0x24(%rbp)
cmpl   $0x64,-0x24(%rbp)
jge    119e <measure_tsc_overhead+0x7e>
mov    $0x0,%al
call   1030 <read_tsc_before_std@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <read_tsc_after_std@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
cmp    %rcx,%rax
jle    1199 <measure_tsc_overhead+0x79>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jge    1190 <measure_tsc_overhead+0x70>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1137 <measure_tsc_overhead+0x17>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_tsc_overhead>:
push   %rbx
sub    $0x20,%rsp
movq   $0xfffffffffffffffe,(%rsp)
ff
xor    %ebx,%ebx
jmp    1148 <measure_tsc_overhead+0x28>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
cmp    $0x64,%ebx
jge    1191 <measure_tsc_overhead+0x71>
xor    %eax,%eax
call   1030 <read_tsc_before_std@plt>
mov    %rax,0x18(%rsp)
xor    %eax,%eax
call   1040 <read_tsc_after_std@plt>
mov    %rax,0x10(%rsp)
mov    0x10(%rsp),%rax
cmp    0x18(%rsp),%rax
jle    1143 <measure_tsc_overhead+0x23>
mov    0x10(%rsp),%rax
sub    0x18(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x8(%rsp),%rax
cmp    (%rsp),%rax
jge    1140 <measure_tsc_overhead+0x20>
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
jmp    1140 <measure_tsc_overhead+0x20>
mov    (%rsp),%rax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i64 @measure_tsc_overhead() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store volatile i64 -2, i64* %4
store i32 0, i32* %5
br label %6
6: ; preds = %27, %0
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 100
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = call i64 (...) @read_tsc_before_std()
store volatile i64 %10, i64* %1
%11 = call i64 (...) @read_tsc_after_std()
store volatile i64 %11, i64* %2
%12 = load volatile i64, i64* %2
%13 = load volatile i64, i64* %1
%14 = icmp sgt i64 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %9
%16 = load volatile i64, i64* %2
%17 = load volatile i64, i64* %1
%18 = sub nsw i64 %16, %17
store volatile i64 %18, i64* %3
%19 = load volatile i64, i64* %3
%20 = load volatile i64, i64* %4
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load volatile i64, i64* %3
store volatile i64 %23, i64* %4
br label %24
24: ; preds = %22, %15
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %27
27: ; preds = %24, %9
br label %6
28: ; preds = %6
%29 = load volatile i64, i64* %4
ret i64 %29
}

LLVM IR (-O2):
define i64 @measure_tsc_overhead() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %1 to i8*
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i64* %4 to i8*
store volatile i64 -2, i64* %4
br label %11
9: ; preds = %29
%10 = load volatile i64, i64* %4
ret i64 %10
11: ; preds = %0, %29
%12 = phi i32 [ 0, %0 ], [ %30, %29 ]
%13 = tail call i64 (...) @read_tsc_before_std()
store volatile i64 %13, i64* %1
%14 = tail call i64 (...) @read_tsc_after_std()
store volatile i64 %14, i64* %2
%15 = load volatile i64, i64* %2
%16 = load volatile i64, i64* %1
%17 = icmp sgt i64 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %11
%19 = load volatile i64, i64* %2
%20 = load volatile i64, i64* %1
%21 = sub nsw i64 %19, %20
store volatile i64 %21, i64* %3
%22 = load volatile i64, i64* %3
%23 = load volatile i64, i64* %4
%24 = icmp slt i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = load volatile i64, i64* %3
store volatile i64 %26, i64* %4
br label %27
27: ; preds = %25, %18
%28 = add nsw i32 %12, 1
br label %29
29: ; preds = %27, %11
%30 = phi i32 [ %28, %27 ], [ %12, %11 ]
%31 = icmp slt i32 %30, 100
br i1 %31, label %11, label %9
}