void cutils_cfps_getfps(double (*gettime)(void), void (*getter)(int, void *),
                        void *data) {
  static double old_time = 0.0;
  static double time_sum = 0.0;
  static float interval = 1.f;
  static int fpscount = 0;
  double new_time = gettime();
  time_sum += new_time - old_time;
  old_time = new_time;
  fpscount++;
  if (time_sum > interval) {
    getter(fpscount, data);
    fpscount = 0;
    time_sum = 0;
  }
}


x86 assembly (clang, -O0):
<cutils_cfps_getfps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
call   *-0x8(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  0x2f07(%rip),%xmm0
addsd  0x2f07(%rip),%xmm0
movsd  %xmm0,0x2eff(%rip)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,0x2eea(%rip)
mov    0x2ef4(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eeb(%rip)
movsd  0x2edb(%rip),%xmm0
movss  0x2ebb(%rip),%xmm1
cvtss2sd %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1198 <cutils_cfps_getfps+0x98>
mov    -0x10(%rbp),%rax
mov    0x2ec3(%rip),%edi
mov    -0x18(%rbp),%rsi
call   *%rax
movl   $0x0,0x2eb3(%rip)
xorps  %xmm0,%xmm0
movsd  %xmm0,0x2ea0(%rip)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cutils_cfps_getfps>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
call   *%rdi
movapd %xmm0,%xmm1
subsd  0x2f10(%rip),%xmm1
addsd  0x2f10(%rip),%xmm1
movsd  %xmm1,0x2f08(%rip)
movsd  %xmm0,0x2ef8(%rip)
mov    0x2f02(%rip),%edi
add    $0x1,%edi
mov    %edi,0x2ef9(%rip)
ucomisd 0xeb9(%rip),%xmm1
jbe    1163 <cutils_cfps_getfps+0x63>
mov    %r14,%rsi
call   *%rbx
movl   $0x0,0x2ee0(%rip)
movq   $0x0,0x2ecd(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@cutils_cfps_getfps.old_time = internal global double 0.000000e+00
@cutils_cfps_getfps.time_sum = internal global double 0.000000e+00
@cutils_cfps_getfps.interval = internal global float 1.000000e+00
@cutils_cfps_getfps.fpscount = internal global i32 0
define void @cutils_cfps_getfps(double ()* %0, void (i32, i8*)* %1, i8* %2) {
%4 = alloca double ()*
%5 = alloca void (i32, i8*)*
%6 = alloca i8*
%7 = alloca double
store double ()* %0, double ()** %4
store void (i32, i8*)* %1, void (i32, i8*)** %5
store i8* %2, i8** %6
%8 = load double ()*, double ()** %4
%9 = call double %8()
store double %9, double* %7
%10 = load double, double* %7
%11 = load double, double* @cutils_cfps_getfps.old_time
%12 = fsub double %10, %11
%13 = load double, double* @cutils_cfps_getfps.time_sum
%14 = fadd double %13, %12
store double %14, double* @cutils_cfps_getfps.time_sum
%15 = load double, double* %7
store double %15, double* @cutils_cfps_getfps.old_time
%16 = load i32, i32* @cutils_cfps_getfps.fpscount
%17 = add nsw i32 %16, 1
store i32 %17, i32* @cutils_cfps_getfps.fpscount
%18 = load double, double* @cutils_cfps_getfps.time_sum
%19 = load float, float* @cutils_cfps_getfps.interval
%20 = fpext float %19 to double
%21 = fcmp ogt double %18, %20
br i1 %21, label %22, label %26
22: ; preds = %3
%23 = load void (i32, i8*)*, void (i32, i8*)** %5
%24 = load i32, i32* @cutils_cfps_getfps.fpscount
%25 = load i8*, i8** %6
store i32 0, i32* @cutils_cfps_getfps.fpscount
store double 0.000000e+00, double* @cutils_cfps_getfps.time_sum
br label %26
26: ; preds = %22, %3
ret void
}

LLVM IR (-O2):
@cutils_cfps_getfps.old_time = internal unnamed_addr global double 0.000000e+00
@cutils_cfps_getfps.time_sum = internal unnamed_addr global double 0.000000e+00
@cutils_cfps_getfps.fpscount = internal unnamed_addr global i32 0
define void @cutils_cfps_getfps(double ()* nocapture %0, void (i32, i8*)* nocapture %1, i8* %2) {
%4 = tail call double %0()
%5 = load double, double* @cutils_cfps_getfps.old_time
%6 = fsub double %4, %5
%7 = load double, double* @cutils_cfps_getfps.time_sum
%8 = fadd double %7, %6
store double %8, double* @cutils_cfps_getfps.time_sum
store double %4, double* @cutils_cfps_getfps.old_time
%9 = load i32, i32* @cutils_cfps_getfps.fpscount
%10 = add nsw i32 %9, 1
store i32 %10, i32* @cutils_cfps_getfps.fpscount
%11 = fcmp ogt double %8, 1.000000e+00
br i1 %11, label %12, label %13
12: ; preds = %3
tail call void %1(i32 %10, i8* %2)
store i32 0, i32* @cutils_cfps_getfps.fpscount
store double 0.000000e+00, double* @cutils_cfps_getfps.time_sum
br label %13
13: ; preds = %12, %3
ret void
}