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

/* Variables and functions */
int abs(int);
int /*<<< orphan*/ clock_gettime(int, struct timespec *);
scalar_t__ timespec_sub_to_ns(struct timespec *, struct timespec *);
int /*<<< orphan*/ waste_cpu_cycles(int);
int calibrate_cpu_cycles_2(int clock) {
  struct timespec start, stop;
  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--;
    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_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x2710,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x3e8,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11fa <calibrate_cpu_cycles_2+0xba>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x1c(%rbp),%edi
call   1060 <waste_cpu_cycles@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <timespec_sub_to_ns@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
sub    -0x28(%rbp),%edi
call   1030 <abs@plt>
imul   $0x32,%eax,%eax
cmp    -0x28(%rbp),%eax
jge    11dc <calibrate_cpu_cycles_2+0x9c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <calibrate_cpu_cycles_2+0xc1>
mov    -0x1c(%rbp),%eax
add    $0x8235,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
jmp    1160 <calibrate_cpu_cycles_2+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_cpu_cycles_2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    $0x2710,%ebp
xor    %ebx,%ebx
mov    $0xfffffc18,%r13d
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
mov    %ebp,%edi
call   1050 <waste_cpu_cycles@plt>
mov    %r14d,%edi
mov    %r12,%rsi
call   1030 <clock_gettime@plt>
mov    %r12,%rdi
mov    %r15,%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     11cc <calibrate_cpu_cycles_2+0x9c>
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    %r13d
jne    1160 <calibrate_cpu_cycles_2+0x30>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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