#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, double, int, double,
                           double, double);
scalar_t__ pow(double, int);
scalar_t__ sqrt(int);
int /*<<< orphan*/ stderr;
void nvstusb_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) {
      fprintf(stderr,
              "nvstusb: 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):
<nvstusb_print_refresh_rate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
cmpl   $0x0,0x2ef5(%rip)
jne    11ae <nvstusb_print_refresh_rate+0x6e>
mov    0x2e7c(%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    12d8 <nvstusb_print_refresh_rate+0x198>
mov    0x2e23(%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    12d3 <nvstusb_print_refresh_rate+0x193>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%edx
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl 0x2dba(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd72(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xd66(%rip),%xmm1
divsd  -0x8(%rbp),%xmm1
cvttsd2si %xmm1,%ecx
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
movsd  0xd43(%rip),%xmm3
mulsd  -0x10(%rbp),%xmm3
divsd  -0x8(%rbp),%xmm3
lea    0xd42(%rip),%rsi
call   1040 <fprintf@plt>
jmp    12d8 <nvstusb_print_refresh_rate+0x198>
mov    0x2d66(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d5d(%rip)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvstusb_print_refresh_rate>:
push   %rbx
sub    $0x20,%rsp
cmpl   $0x0,0x2ef8(%rip)
je     1246 <nvstusb_print_refresh_rate+0x106>
mov    0x2e7f(%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),%edx
test   $0x1ff,%edx
jne    1288 <nvstusb_print_refresh_rate+0x148>
cvtsi2sd %rax,%xmm2
mov    0x2ddf(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
movsd  0x18(%rsp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xde6(%rip),%xmm3
divsd  %xmm3,%xmm0
divsd  %xmm1,%xmm3
cvttsd2si %xmm3,%ecx
mov    (%rax),%edi
movsd  0xdd8(%rip),%xmm3
mulsd  %xmm2,%xmm3
divsd  %xmm1,%xmm3
lea    0xdd1(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1288 <nvstusb_print_refresh_rate+0x148>
mov    0x2d8b(%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,0x2dca(%rip)
mov    %ecx,0x2dc0(%rip)
addl   $0x1,0x2db5(%rip)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@nvstusb_print_refresh_rate.i_it = internal global i32 0
@nvstusb_print_refresh_rate.i_last = internal global i32 0
@nvstusb_print_refresh_rate.i_first = internal global i32 0
@nvstusb_print_refresh_rate.i_acc_var = internal global i32 0
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [83 x i8] c"nvstusb: frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us (%0.1f %%)\0A\00"
define void @nvstusb_print_refresh_rate() {
%1 = alloca double
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = load i32, i32* @nvstusb_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* @nvstusb_print_refresh_rate.i_first
%20 = load i32, i32* @nvstusb_print_refresh_rate.i_first
store i32 %20, i32* @nvstusb_print_refresh_rate.i_last
store double 0.000000e+00, double* %1
store double 0.000000e+00, double* %2
br label %79
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* @nvstusb_print_refresh_rate.i_first
%35 = sub nsw i32 %33, %34
%36 = sitofp i32 %35 to double
%37 = load i32, i32* @nvstusb_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* @nvstusb_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* @nvstusb_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* @nvstusb_print_refresh_rate.i_acc_var
%51 = load i32, i32* @nvstusb_print_refresh_rate.i_acc_var
%52 = load i32, i32* @nvstusb_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* @nvstusb_print_refresh_rate.i_last
%57 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%58 = srem i32 %57, 512
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %78
60: ; preds = %21
%61 = load i32, i32* @stderr
%62 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%63 = load double, double* %1
%64 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%65 = sitofp i32 %64 to double
%66 = fmul double %63, %65
%67 = fdiv double %66, 1.000000e+06
%68 = load double, double* %1
%69 = fdiv double 1.000000e+06, %68
%70 = fptosi double %69 to i32
%71 = load double, double* %1
%72 = load double, double* %2
%73 = load double, double* %2
%74 = fmul double 1.000000e+02, %73
%75 = load double, double* %1
%76 = fdiv double %74, %75
%77 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i32 %62, double %67, i32 %70, double %71, double %72, double %76)
br label %78
78: ; preds = %60, %21
br label %79
79: ; preds = %78, %8
%80 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%81 = add nsw i32 %80, 1
store i32 %81, i32* @nvstusb_print_refresh_rate.i_it
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@nvstusb_print_refresh_rate.i_it = internal unnamed_addr global i32 0
@nvstusb_print_refresh_rate.i_last = internal unnamed_addr global i32 0
@nvstusb_print_refresh_rate.i_first = internal unnamed_addr global i32 0
@nvstusb_print_refresh_rate.i_acc_var = internal unnamed_addr global i32 0
@CLOCK_REALTIME = global i32 0
@stderr = global i32 0
@.str = [83 x i8] c"nvstusb: frame:%d (%0.2f s) mean: %f Hz (%0.2f us) sqrt(var): %0.2f us (%0.1f %%)\0A\00"
define void @nvstusb_print_refresh_rate() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = load i32, i32* @nvstusb_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* @nvstusb_print_refresh_rate.i_first
store i32 %17, i32* @nvstusb_print_refresh_rate.i_last
br label %63
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* @nvstusb_print_refresh_rate.i_first
%32 = sub nsw i32 %30, %31
%33 = sitofp i32 %32 to double
%34 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%35 = sitofp i32 %34 to double
%36 = fdiv double %33, %35
%37 = load i32, i32* @nvstusb_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* @nvstusb_print_refresh_rate.i_acc_var
%43 = trunc i64 %41 to i32
%44 = add i32 %42, %43
store i32 %44, i32* @nvstusb_print_refresh_rate.i_acc_var
%45 = load i32, i32* @nvstusb_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* @nvstusb_print_refresh_rate.i_last
%49 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%50 = and i32 %49, 511
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %62
52: ; preds = %18
%53 = load i32, i32* @stderr
%54 = sitofp i32 %49 to double
%55 = fmul double %36, %54
%56 = fdiv double %55, 1.000000e+06
%57 = fdiv double 1.000000e+06, %36
%58 = fptosi double %57 to i32
%59 = fmul double %48, 1.000000e+02
%60 = fdiv double %59, %36
%61 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i32 %49, double %56, i32 %58, double %36, double %48, double %60)
br label %62
62: ; preds = %52, %18
br label %63
63: ; preds = %62, %5
%64 = load i32, i32* @nvstusb_print_refresh_rate.i_it
%65 = add nsw i32 %64, 1
store i32 %65, i32* @nvstusb_print_refresh_rate.i_it
ret void
}