#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_stabilized() {
  enum { NOTCHANGED_THRESHOLD = 10, NMEASURES_MAX = 100 };
  volatile uint64_t t0, t1, ticks, minticks = (uint64_t)~0x1;
  int notchanged = 0;
  for (int i = 0; i < NMEASURES_MAX && notchanged < NOTCHANGED_THRESHOLD;) {
    t0 = read_tsc_before_std();
    t1 = read_tsc_after_std();
    if (t1 > t0) {
      ticks = t1 - t0;
      notchanged++;
      if (ticks < minticks) {
        minticks = ticks;
        notchanged = 0;
      }
      i++;
    }
  }
  return minticks;
}


x86 assembly (clang, -O0):
<measure_tsc_overhead_stabilized>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0xfffffffffffffffe,-0x20(%rbp)
ff
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x64,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jge    1157 <measure_tsc_overhead_stabilized+0x37>
cmpl   $0xa,-0x24(%rbp)
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1167 <measure_tsc_overhead_stabilized+0x47>
jmp    11d4 <measure_tsc_overhead_stabilized+0xb4>
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    11cf <measure_tsc_overhead_stabilized+0xaf>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jge    11c6 <measure_tsc_overhead_stabilized+0xa6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    113e <measure_tsc_overhead_stabilized+0x1e>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_tsc_overhead_stabilized>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movq   $0xfffffffffffffffe,0x8(%rsp)
ff ff
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <read_tsc_before_std@plt>
mov    %rax,0x20(%rsp)
xor    %eax,%eax
call   1040 <read_tsc_after_std@plt>
mov    %rax,0x18(%rsp)
mov    0x18(%rsp),%rax
cmp    0x20(%rsp),%rax
jle    1196 <measure_tsc_overhead_stabilized+0x76>
mov    0x18(%rsp),%rax
sub    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    0x10(%rsp),%rax
cmp    0x8(%rsp),%rax
jge    1190 <measure_tsc_overhead_stabilized+0x70>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
jmp    1193 <measure_tsc_overhead_stabilized+0x73>
nopl   (%rax)
add    $0x1,%ebp
add    $0x1,%ebx
cmp    $0x63,%ebx
jg     11a0 <measure_tsc_overhead_stabilized+0x80>
cmp    $0xa,%ebp
jl     1140 <measure_tsc_overhead_stabilized+0x20>
mov    0x8(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @measure_tsc_overhead_stabilized() {
%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 = %32
%10 = load volatile i64, i64* %4
ret i64 %10
11: ; preds = %0, %32
%12 = phi i32 [ 0, %0 ], [ %34, %32 ]
%13 = phi i32 [ 0, %0 ], [ %33, %32 ]
%14 = tail call i64 (...) @read_tsc_before_std()
store volatile i64 %14, i64* %1
%15 = tail call i64 (...) @read_tsc_after_std()
store volatile i64 %15, i64* %2
%16 = load volatile i64, i64* %2
%17 = load volatile i64, i64* %1
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %11
%20 = load volatile i64, i64* %2
%21 = load volatile i64, i64* %1
%22 = sub nsw i64 %20, %21
store volatile i64 %22, i64* %3
%23 = add nsw i32 %13, 1
%24 = load volatile i64, i64* %3
%25 = load volatile i64, i64* %4
%26 = icmp slt i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = load volatile i64, i64* %3
store volatile i64 %28, i64* %4
br label %29
29: ; preds = %27, %19
%30 = phi i32 [ 0, %27 ], [ %23, %19 ]
%31 = add nsw i32 %12, 1
br label %32
32: ; preds = %29, %11
%33 = phi i32 [ %30, %29 ], [ %13, %11 ]
%34 = phi i32 [ %31, %29 ], [ %12, %11 ]
%35 = icmp slt i32 %34, 100
%36 = icmp slt i32 %33, 10
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %11, label %9
}