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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ pow(double, int);
int /*<<< orphan*/ printf(char *, int, double, int, double, double, double);
scalar_t__ sqrt(int);
void print_refresh_rate(void) {
  static int i_it = 0;
  static uint64_t i_last = 0;
  static uint64_t i_first = 0;
  static uint64_t i_acc_var = 0;
  double f_mean, f_var;
  if (i_it == 0) {
    struct timespec s_tmp;
    clock_gettime(CLOCK_REALTIME, &s_tmp);
    i_first = (uint64_t)s_tmp.tv_sec * 1000000 + (uint64_t)s_tmp.tv_nsec / 1000;
    i_last = i_first;
    f_mean = 0;
    f_var = 0;
  } else {
    struct timespec s_tmp;
    clock_gettime(CLOCK_REALTIME, &s_tmp);
    uint64_t i_new =
        (uint64_t)s_tmp.tv_sec * 1000000 + (uint64_t)s_tmp.tv_nsec / 1000;
    f_mean = (double)(i_new - i_first) / (i_it);
    i_acc_var += pow((double)(i_new - i_last) - f_mean, 2);
    f_var = (double)sqrt(i_acc_var / i_it);
    i_last = i_new;
    if (i_it % 512 == 0) {
      printf("frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us "
             "(%0.1f %%)\n",
             i_it, f_mean * i_it / 1000000.0, 1000000 / f_mean, f_mean, f_var,
             100.0 * f_var / f_mean);
    }
  }
  i_it++;
}


x86 assembly (clang, -O0):
<print_refresh_rate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
cmpl   $0x0,0x2ef5(%rip)
jne    11ae <print_refresh_rate+0x6e>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,0x2ebf(%rip)
mov    0x2eb9(%rip),%eax
mov    %eax,0x2eaf(%rip)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    12cf <print_refresh_rate+0x18f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
imul   $0xf4240,%eax,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
sub    0x2e60(%rip),%eax
cvtsi2sd %eax,%xmm0
cvtsi2sdl 0x2e4c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x34(%rbp),%eax
sub    0x2e3e(%rip),%eax
cvtsi2sd %eax,%xmm0
subsd  -0x8(%rbp),%xmm0
mov    $0x2,%edi
call   1050 <pow@plt>
mov    %rax,%rcx
movslq 0x2e29(%rip),%rax
add    %rcx,%rax
mov    %eax,0x2e20(%rip)
mov    0x2e1a(%rip),%eax
cltd
idivl  0x2e07(%rip)
mov    %eax,%edi
call   1060 <sqrt@plt>
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,0x2df1(%rip)
mov    0x2de7(%rip),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12ca <print_refresh_rate+0x18a>
mov    0x2dd0(%rip),%esi
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl 0x2dc3(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd7b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xd6f(%rip),%xmm1
divsd  -0x8(%rbp),%xmm1
cvttsd2si %xmm1,%edx
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
movsd  0xd4c(%rip),%xmm3
mulsd  -0x10(%rbp),%xmm3
divsd  -0x8(%rbp),%xmm3
lea    0xd4b(%rip),%rdi
call   1040 <printf@plt>
jmp    12cf <print_refresh_rate+0x18f>
mov    0x2d6f(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d66(%rip)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_refresh_rate>:
push   %rbx
sub    $0x20,%rsp
cmpl   $0x0,0x2ef8(%rip)
je     123d <print_refresh_rate+0xfd>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
imul   $0xf4240,0x10(%rsp),%eax
movslq 0x8(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
add    %eax,%ebx
mov    %ebx,%eax
sub    0x2ebc(%rip),%eax
cvtsi2sd %eax,%xmm1
cvtsi2sdl 0x2ea8(%rip),%xmm0
mov    %ebx,%eax
sub    0x2ea4(%rip),%eax
divsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm1,0x18(%rsp)
subsd  %xmm1,%xmm0
mov    $0x2,%edi
call   1050 <pow@plt>
add    0x2e87(%rip),%eax
mov    %eax,0x2e81(%rip)
cltd
idivl  0x2e6e(%rip)
mov    %eax,%edi
call   1060 <sqrt@plt>
mov    %ebx,0x2e65(%rip)
mov    0x2e5b(%rip),%esi
test   $0x1ff,%esi
jne    127f <print_refresh_rate+0x13f>
cvtsi2sd %rax,%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
movsd  0x18(%rsp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xded(%rip),%xmm3
divsd  %xmm3,%xmm0
divsd  %xmm1,%xmm3
cvttsd2si %xmm3,%edx
movsd  0xde1(%rip),%xmm3
mulsd  %xmm2,%xmm3
divsd  %xmm1,%xmm3
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
jmp    127f <print_refresh_rate+0x13f>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
imul   $0xf4240,0x10(%rsp),%eax
movslq 0x8(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
mov    %ecx,0x2dd3(%rip)
mov    %ecx,0x2dc9(%rip)
addl   $0x1,0x2dbe(%rip)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@print_refresh_rate.i_it = internal global i32 0
@print_refresh_rate.i_last = internal global i32 0
@print_refresh_rate.i_first = internal global i32 0
@print_refresh_rate.i_acc_var = internal global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [74 x i8] c"frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us (%0.1f %%)\0A\00"
define void @print_refresh_rate() {
%1 = alloca double
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = load i32, i32* @print_refresh_rate.i_it
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %0
%9 = load i32, i32* @CLOCK_REALTIME
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %3)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = mul nsw i32 %13, 1000000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = sdiv i32 %17, 1000
%19 = add nsw i32 %14, %18
store i32 %19, i32* @print_refresh_rate.i_first
%20 = load i32, i32* @print_refresh_rate.i_first
store i32 %20, i32* @print_refresh_rate.i_last
store double 0.000000e+00, double* %1
store double 0.000000e+00, double* %2
br label %78
21: ; preds = %0
%22 = load i32, i32* @CLOCK_REALTIME
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %4)
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i32
%27 = mul nsw i32 %26, 1000000
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = sdiv i32 %30, 1000
%32 = add nsw i32 %27, %31
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = load i32, i32* @print_refresh_rate.i_first
%35 = sub nsw i32 %33, %34
%36 = sitofp i32 %35 to double
%37 = load i32, i32* @print_refresh_rate.i_it
%38 = sitofp i32 %37 to double
%39 = fdiv double %36, %38
store double %39, double* %1
%40 = load i32, i32* %5
%41 = load i32, i32* @print_refresh_rate.i_last
%42 = sub nsw i32 %40, %41
%43 = sitofp i32 %42 to double
%44 = load double, double* %1
%45 = fsub double %43, %44
%46 = call i64 @pow(double %45, i32 2)
%47 = load i32, i32* @print_refresh_rate.i_acc_var
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* @print_refresh_rate.i_acc_var
%51 = load i32, i32* @print_refresh_rate.i_acc_var
%52 = load i32, i32* @print_refresh_rate.i_it
%53 = sdiv i32 %51, %52
%54 = call i64 @sqrt(i32 %53)
%55 = sitofp i64 %54 to double
store double %55, double* %2
%56 = load i32, i32* %5
store i32 %56, i32* @print_refresh_rate.i_last
%57 = load i32, i32* @print_refresh_rate.i_it
%58 = srem i32 %57, 512
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %77
60: ; preds = %21
%61 = load i32, i32* @print_refresh_rate.i_it
%62 = load double, double* %1
%63 = load i32, i32* @print_refresh_rate.i_it
%64 = sitofp i32 %63 to double
%65 = fmul double %62, %64
%66 = fdiv double %65, 1.000000e+06
%67 = load double, double* %1
%68 = fdiv double 1.000000e+06, %67
%69 = fptosi double %68 to i32
%70 = load double, double* %1
%71 = load double, double* %2
%72 = load double, double* %2
%73 = fmul double 1.000000e+02, %72
%74 = load double, double* %1
%75 = fdiv double %73, %74
%76 = call i32 @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i32 %61, double %66, i32 %69, double %70, double %71, double %75)
br label %77
77: ; preds = %60, %21
br label %78
78: ; preds = %77, %8
%79 = load i32, i32* @print_refresh_rate.i_it
%80 = add nsw i32 %79, 1
store i32 %80, i32* @print_refresh_rate.i_it
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@print_refresh_rate.i_it = internal unnamed_addr global i32 0
@print_refresh_rate.i_last = internal unnamed_addr global i32 0
@print_refresh_rate.i_first = internal unnamed_addr global i32 0
@print_refresh_rate.i_acc_var = internal unnamed_addr global i32 0
@CLOCK_REALTIME = global i32 0
@.str = [74 x i8] c"frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us (%0.1f %%)\0A\00"
define void @print_refresh_rate() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = load i32, i32* @print_refresh_rate.i_it
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %18
5: ; preds = %0
%6 = bitcast %struct.timespec* %1 to i8*
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %1)
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = mul nsw i32 %11, 1000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i32
%16 = sdiv i32 %15, 1000
%17 = add nsw i32 %16, %12
store i32 %17, i32* @print_refresh_rate.i_first
store i32 %17, i32* @print_refresh_rate.i_last
br label %62
18: ; preds = %0
%19 = bitcast %struct.timespec* %2 to i8*
%20 = load i32, i32* @CLOCK_REALTIME
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %2)
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i32
%25 = mul nsw i32 %24, 1000000
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i32
%29 = sdiv i32 %28, 1000
%30 = add nsw i32 %29, %25
%31 = load i32, i32* @print_refresh_rate.i_first
%32 = sub nsw i32 %30, %31
%33 = sitofp i32 %32 to double
%34 = load i32, i32* @print_refresh_rate.i_it
%35 = sitofp i32 %34 to double
%36 = fdiv double %33, %35
%37 = load i32, i32* @print_refresh_rate.i_last
%38 = sub nsw i32 %30, %37
%39 = sitofp i32 %38 to double
%40 = fsub double %39, %36
%41 = call i64 @pow(double %40, i32 2)
%42 = load i32, i32* @print_refresh_rate.i_acc_var
%43 = trunc i64 %41 to i32
%44 = add i32 %42, %43
store i32 %44, i32* @print_refresh_rate.i_acc_var
%45 = load i32, i32* @print_refresh_rate.i_it
%46 = sdiv i32 %44, %45
%47 = call i64 @sqrt(i32 %46)
%48 = sitofp i64 %47 to double
store i32 %30, i32* @print_refresh_rate.i_last
%49 = load i32, i32* @print_refresh_rate.i_it
%50 = and i32 %49, 511
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %61
52: ; preds = %18
%53 = sitofp i32 %49 to double
%54 = fmul double %36, %53
%55 = fdiv double %54, 1.000000e+06
%56 = fdiv double 1.000000e+06, %36
%57 = fptosi double %56 to i32
%58 = fmul double %48, 1.000000e+02
%59 = fdiv double %58, %36
%60 = call i32 @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i32 %49, double %55, i32 %57, double %36, double %48, double %59)
br label %61
61: ; preds = %52, %18
br label %62
62: ; preds = %61, %5
%63 = load i32, i32* @print_refresh_rate.i_it
%64 = add nsw i32 %63, 1
store i32 %64, i32* @print_refresh_rate.i_it
ret void
}