#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 timezone {
  int dummy;
};
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
unsigned long long aarch64_timer_get_cntfrq();
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
int llabs(unsigned long long);
long long pqcpucycles_impl();
int /*<<< orphan*/ sleep(int);
long long pqcpucycles_impl_persecond(void) {
  struct timeval t0, t1;
  long long c0, c1;
  unsigned long long f;
  long long r;
  double d0, d1;
  gettimeofday(&t0, (struct timezone *)0);
  c0 = pqcpucycles_impl();
  sleep(1);
  gettimeofday(&t1, (struct timezone *)0);
  c1 = pqcpucycles_impl();
  d0 = (double)t0.tv_sec;
  d0 += ((double)t0.tv_usec) / 1000000.0;
  d1 = (double)t1.tv_sec;
  d1 += ((double)t1.tv_usec) / 1000000.0;
  r = (c1 - c0) / (d1 - d0);
  f = aarch64_timer_get_cntfrq();
  if (llabs(f - r) < 100)
    return f;
  return r;
}


x86 assembly (clang, -O0):
<pqcpucycles_impl_persecond>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x0,%al
call   1030 <pqcpucycles_impl@plt>
mov    %rax,-0x30(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x0,%al
call   1030 <pqcpucycles_impl@plt>
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  0xe55(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdq -0x20(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
cvtsi2sdq -0x28(%rbp),%xmm0
movsd  0xe2e(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
cvtsi2sd %rax,%xmm0
movsd  -0x58(%rbp),%xmm1
subsd  -0x50(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <aarch64_timer_get_cntfrq@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
sub    -0x48(%rbp),%rdi
call   1060 <llabs@plt>
cmp    $0x64,%eax
jge    1232 <pqcpucycles_impl_persecond+0xe2>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123a <pqcpucycles_impl_persecond+0xea>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pqcpucycles_impl_persecond>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
xor    %eax,%eax
call   1030 <pqcpucycles_impl@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
xor    %eax,%eax
call   1030 <pqcpucycles_impl@plt>
cvtsi2sdq 0x20(%rsp),%xmm0
cvtsi2sdq 0x18(%rsp),%xmm1
movsd  0xe60(%rip),%xmm2
divsd  %xmm2,%xmm1
cvtsi2sdq 0x10(%rsp),%xmm3
addsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sdq 0x8(%rsp),%xmm0
divsd  %xmm2,%xmm0
addsd  %xmm3,%xmm0
sub    %rbx,%rax
xorps  %xmm2,%xmm2
cvtsi2sd %rax,%xmm2
subsd  %xmm1,%xmm0
divsd  %xmm0,%xmm2
cvttsd2si %xmm2,%rbx
xor    %eax,%eax
call   1050 <aarch64_timer_get_cntfrq@plt>
mov    %rax,%r14
mov    %rax,%rdi
sub    %rbx,%rdi
call   1060 <llabs@plt>
cmp    $0x64,%eax
cmovl  %r14,%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
define i64 @pqcpucycles_impl_persecond() {
%1 = alloca i64
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca double
%10 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* null)
%11 = call i64 (...) @pqcpucycles_impl()
store i64 %11, i64* %4
%12 = call i32 @sleep(i32 1)
%13 = call i32 @gettimeofday(%struct.timeval* %3, %struct.timezone* null)
%14 = call i64 (...) @pqcpucycles_impl()
store i64 %14, i64* %5
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%16 = load i64, i64* %15
%17 = sitofp i64 %16 to double
store double %17, double* %8
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%19 = load i64, i64* %18
%20 = sitofp i64 %19 to double
%21 = fdiv double %20, 1.000000e+06
%22 = load double, double* %8
%23 = fadd double %22, %21
store double %23, double* %8
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%25 = load i64, i64* %24
%26 = sitofp i64 %25 to double
store double %26, double* %9
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%28 = load i64, i64* %27
%29 = sitofp i64 %28 to double
%30 = fdiv double %29, 1.000000e+06
%31 = load double, double* %9
%32 = fadd double %31, %30
store double %32, double* %9
%33 = load i64, i64* %5
%34 = load i64, i64* %4
%35 = sub nsw i64 %33, %34
%36 = sitofp i64 %35 to double
%37 = load double, double* %9
%38 = load double, double* %8
%39 = fsub double %37, %38
%40 = fdiv double %36, %39
%41 = fptosi double %40 to i64
store i64 %41, i64* %7
%42 = call i64 (...) @aarch64_timer_get_cntfrq()
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = load i64, i64* %7
%45 = sub i64 %43, %44
%46 = call i32 @llabs(i64 %45)
%47 = icmp slt i32 %46, 100
br i1 %47, label %48, label %50
48: ; preds = %0
%49 = load i64, i64* %6
store i64 %49, i64* %1
br label %52
50: ; preds = %0
%51 = load i64, i64* %7
store i64 %51, i64* %1
br label %52
52: ; preds = %50, %48
%53 = load i64, i64* %1
ret i64 %53
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timezone = type { i32 }
define i64 @pqcpucycles_impl_persecond() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, %struct.timezone* null)
%6 = tail call i64 (...) @pqcpucycles_impl()
%7 = tail call i32 @sleep(i32 1)
%8 = call i32 @gettimeofday(%struct.timeval* %2, %struct.timezone* null)
%9 = tail call i64 (...) @pqcpucycles_impl()
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%11 = load i64, i64* %10
%12 = sitofp i64 %11 to double
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%14 = load i64, i64* %13
%15 = sitofp i64 %14 to double
%16 = fdiv double %15, 1.000000e+06
%17 = fadd double %16, %12
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%19 = load i64, i64* %18
%20 = sitofp i64 %19 to double
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%22 = load i64, i64* %21
%23 = sitofp i64 %22 to double
%24 = fdiv double %23, 1.000000e+06
%25 = fadd double %24, %20
%26 = sub nsw i64 %9, %6
%27 = sitofp i64 %26 to double
%28 = fsub double %25, %17
%29 = fdiv double %27, %28
%30 = fptosi double %29 to i64
%31 = tail call i64 (...) @aarch64_timer_get_cntfrq()
%32 = sub i64 %31, %30
%33 = tail call i32 @llabs(i64 %32)
%34 = icmp slt i32 %33, 100
%35 = select i1 %34, i64 %31, i64 %30
ret i64 %35
}