#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 char *uint64_t;

/* Variables and functions */
char *bench_end();
char *bench_start();
uint64_t measure_tsc_overhead(void) {
  uint64_t t0, t1, overhead = ~0;
  int i;
  for (i = 0; i < 100000; i++) {
    t0 = bench_start();
    asm volatile("");
    t1 = bench_end();
    if (t1 - t0 < overhead)
      overhead = t1 - t0;
  }
  return overhead;
}


x86 assembly (clang, -O0):
<measure_tsc_overhead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x186a0,-0x1c(%rbp)
jge    1194 <measure_tsc_overhead+0x74>
mov    $0x0,%al
call   1040 <bench_start@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <bench_end@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x18(%rbp),%rax
jae    1181 <measure_tsc_overhead+0x61>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1186 <measure_tsc_overhead+0x66>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113a <measure_tsc_overhead+0x1a>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_tsc_overhead>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffffffffffff,%rax
mov    $0x186a0,%ebp
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <bench_start@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <bench_end@plt>
sub    %r14,%rax
cmp    %rax,%rbx
cmovbe %rbx,%rax
add    $0xffffffff,%ebp
jne    1130 <measure_tsc_overhead+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @measure_tsc_overhead() {
br label %1
1: ; preds = %0, %1
%2 = phi i32 [ 0, %0 ], [ %12, %1 ]
%3 = phi i8* [ inttoptr (i64 -1 to i8*), %0 ], [ %11, %1 ]
%4 = tail call i8* (...) @bench_start()
tail call void asm sideeffect "", "~{dirflag},~{fpsr},~{flags}"()
%5 = tail call i8* (...) @bench_end()
%6 = ptrtoint i8* %5 to i64
%7 = ptrtoint i8* %4 to i64
%8 = sub i64 %6, %7
%9 = inttoptr i64 %8 to i8*
%10 = icmp ugt i8* %3, %9
%11 = select i1 %10, i8* %9, i8* %3
%12 = add nuw nsw i32 %2, 1
%13 = icmp eq i32 %12, 100000
br i1 %13, label %14, label %1
14: ; preds = %1
ret i8* %11
}