#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 */
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int abs(int);
int /*<<< orphan*/ clock_gettime(int, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
scalar_t__ timespec_sub_to_ns(struct timespec *, struct timespec *);
int /*<<< orphan*/ waste_cpu_cycles(int);
int calibrate_cpu_cycles_1(int clock) {
  struct timespec start, stop, sleep;
  int max_load_loop = 10000;
  unsigned int diff;
  int nsec_per_loop, avg_per_loop = 0;
  int cal_trial = 1000;
  while (cal_trial) {
    cal_trial--;
    sleep.tv_sec = 1;
    sleep.tv_nsec = 0;
    clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, NULL);
    clock_gettime(clock, &start);
    waste_cpu_cycles(max_load_loop);
    clock_gettime(clock, &stop);
    diff = (int)timespec_sub_to_ns(&stop, &start);
    nsec_per_loop = diff / max_load_loop;
    avg_per_loop = (avg_per_loop + nsec_per_loop) >> 1;
    if ((abs(nsec_per_loop - avg_per_loop) * 50) < avg_per_loop)
      return avg_per_loop;
    max_load_loop += 33333;
    max_load_loop %= 1000000;
  }
  return 0;
}


x86 assembly (clang, -O0):
<calibrate_cpu_cycles_1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x2710,-0x3c(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x3e8,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1231 <calibrate_cpu_cycles_1+0xe1>
mov    -0x4c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
movl   $0x1,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x38(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <clock_nanosleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x3c(%rbp),%edi
call   1070 <waste_cpu_cycles@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <timespec_sub_to_ns@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
xor    %edx,%edx
divl   -0x3c(%rbp)
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    -0x44(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
sub    -0x48(%rbp),%edi
call   1030 <abs@plt>
imul   $0x32,%eax,%eax
cmp    -0x48(%rbp),%eax
jge    1213 <calibrate_cpu_cycles_1+0xc3>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <calibrate_cpu_cycles_1+0xe8>
mov    -0x3c(%rbp),%eax
add    $0x8235,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x3c(%rbp)
jmp    1170 <calibrate_cpu_cycles_1+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_cpu_cycles_1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r14d
mov    $0x2710,%ebp
xor    %ebx,%ebx
mov    $0xfffffc18,%r15d
lea    0x28(%rsp),%r12
lea    0x18(%rsp),%r13
nopl   0x0(%rax,%rax,1)
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0x8(%rsp),%rdx
xor    %ecx,%ecx
call   1050 <clock_nanosleep@plt>
mov    %r14d,%edi
mov    %r12,%rsi
call   1030 <clock_gettime@plt>
mov    %ebp,%edi
call   1060 <waste_cpu_cycles@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   1030 <clock_gettime@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <timespec_sub_to_ns@plt>
xor    %edx,%edx
div    %ebp
add    %eax,%ebx
sar    %ebx
sub    %ebx,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
imul   $0x32,%ecx,%eax
cmp    %ebx,%eax
jl     1208 <calibrate_cpu_cycles_1+0xc8>
add    $0x8235,%ebp
movslq %ebp,%rbp
imul   $0x431bde83,%rbp,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rax
add    %ecx,%eax
imul   $0xf4240,%eax,%eax
sub    %eax,%ebp
inc    %r15d
jne    1170 <calibrate_cpu_cycles_1+0x30>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @calibrate_cpu_cycles_1(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i32 10000, i32* %7
store i32 0, i32* %10
store i32 1000, i32* %11
br label %12
12: ; preds = %46, %1
%13 = load i32, i32* %11
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %12
%16 = load i32, i32* %11
%17 = add nsw i32 %16, -1
store i32 %17, i32* %11
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %19
%20 = load i32, i32* @CLOCK_MONOTONIC
%21 = call i32 @clock_nanosleep(i32 %20, i32 0, %struct.timespec* %6, i32* null)
%22 = load i32, i32* %3
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %4)
%24 = load i32, i32* %7
%25 = call i32 @waste_cpu_cycles(i32 %24)
%26 = load i32, i32* %3
%27 = call i32 @clock_gettime(i32 %26, %struct.timespec* %5)
%28 = call i64 @timespec_sub_to_ns(%struct.timespec* %5, %struct.timespec* %4)
%29 = trunc i64 %28 to i32
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = udiv i32 %30, %31
store i32 %32, i32* %9
%33 = load i32, i32* %10
%34 = load i32, i32* %9
%35 = add nsw i32 %33, %34
%36 = ashr i32 %35, 1
store i32 %36, i32* %10
%37 = load i32, i32* %9
%38 = load i32, i32* %10
%39 = sub nsw i32 %37, %38
%40 = call i32 @abs(i32 %39)
%41 = mul nsw i32 %40, 50
%42 = load i32, i32* %10
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %15
%45 = load i32, i32* %10
store i32 %45, i32* %2
br label %52
46: ; preds = %15
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 33333
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = srem i32 %49, 1000000
store i32 %50, i32* %7
br label %12
51: ; preds = %12
store i32 0, i32* %2
br label %52
52: ; preds = %51, %44
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
define i32 @calibrate_cpu_cycles_1(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
br label %10
10: ; preds = %1, %28
%11 = phi i32 [ 1000, %1 ], [ %29, %28 ]
%12 = phi i32 [ 0, %1 ], [ %23, %28 ]
%13 = phi i32 [ 10000, %1 ], [ %31, %28 ]
store i32 1, i32* %8
store i64 0, i64* %9
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_nanosleep(i32 %14, i32 0, %struct.timespec* %4, i32* null)
%16 = call i32 @clock_gettime(i32 %0, %struct.timespec* %2)
%17 = call i32 @waste_cpu_cycles(i32 %13)
%18 = call i32 @clock_gettime(i32 %0, %struct.timespec* %3)
%19 = call i64 @timespec_sub_to_ns(%struct.timespec* %3, %struct.timespec* %2)
%20 = trunc i64 %19 to i32
%21 = udiv i32 %20, %13
%22 = add nsw i32 %21, %12
%23 = ashr i32 %22, 1
%24 = sub nsw i32 %21, %23
%25 = call i32 @llvm.abs.i32(i32 %24, i1 true)
%26 = mul nsw i32 %25, 50
%27 = icmp slt i32 %26, %23
br i1 %27, label %33, label %28
28: ; preds = %10
%29 = add nsw i32 %11, -1
%30 = add nsw i32 %13, 33333
%31 = srem i32 %30, 1000000
%32 = icmp eq i32 %29, 0
br i1 %32, label %33, label %10
33: ; preds = %28, %10
%34 = phi i32 [ %23, %10 ], [ 0, %28 ]
ret i32 %34
}