#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 timeval {
  long long tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, float);
int /*<<< orphan*/ stdout;
long long update_fps() {
  static int frame_count = 0;
  static long long time_start = 0;
  long long time_diff = 0;
  static long long time_now = 0;
  struct timeval te;
  float fps;
  frame_count++;
  gettimeofday(&te, NULL);
  if (time_now) {
    time_diff = te.tv_sec * 1000LL + te.tv_usec / 1000 - time_now;
    time_now += time_diff;
  } else {
    time_now = te.tv_sec * 1000LL + te.tv_usec / 1000;
  }
  if (time_start == 0) {
    time_start = time_now;
  } else if (time_now - time_start > 5000) {
    fps = (float)frame_count / ((time_now - time_start) / 1000.0);
    frame_count = 0;
    time_start = time_now;
    printf("%3.2f FPS\n", fps);
    fflush(stdout);
  }
}


x86 assembly (clang, -O0):
<update_fps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x10(%rbp)
mov    0x2efa(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ef1(%rip)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
cmpq   $0x0,0x2eec(%rip)
je     11af <update_fps+0x7f>
imul   $0x3e8,-0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
sub    0x2ebc(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    0x2ead(%rip),%rax
mov    %rax,0x2ea6(%rip)
jmp    11d9 <update_fps+0xa9>
imul   $0x3e8,-0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e77(%rip)
cmpq   $0x0,0x2e67(%rip)
jne    11fa <update_fps+0xca>
mov    0x2e62(%rip),%rax
mov    %rax,0x2e53(%rip)
jmp    1288 <update_fps+0x158>
mov    0x2e4f(%rip),%rax
sub    0x2e40(%rip),%rax
cmp    $0x1388,%rax
jle    1283 <update_fps+0x153>
cvtsi2ssl 0x2e24(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
mov    0x2e29(%rip),%rax
sub    0x2e1a(%rip),%rax
cvtsi2sd %rax,%xmm1
movsd  0xdc5(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x24(%rbp)
movl   $0x0,0x2dea(%rip)
mov    0x2df3(%rip),%rax
mov    %rax,0x2de4(%rip)
movss  -0x24(%rbp),%xmm0
lea    0xd98(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    1288 <update_fps+0x158>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_fps>:
sub    $0x18,%rsp
addl   $0x1,0x2f05(%rip)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
imul   $0x3e8,0x8(%rsp),%rcx
movslq 0x10(%rsp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
cltq
add    %rcx,%rax
mov    0x2ed3(%rip),%rdx
test   %rdx,%rdx
je     11da <update_fps+0xaa>
mov    %rax,%rcx
sub    %rdx,%rcx
cmp    $0x1389,%rcx
jl     11d5 <update_fps+0xa5>
cvtsi2ssl 0x2eaf(%rip),%xmm0
cvtsi2sd %rcx,%xmm1
cvtss2sd %xmm0,%xmm0
divsd  0xe5e(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movl   $0x0,0x2e8c(%rip)
mov    %rax,0x2e8d(%rip)
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
add    $0x18,%rsp
ret
mov    %rax,0x2e67(%rip)
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
@update_fps.frame_count = internal global i32 0
@update_fps.time_start = internal global i64 0
@update_fps.time_now = internal global i64 0
@.str = [11 x i8] c"%3.2f FPS\0A\00"
@stdout = global i32 0
define i64 @update_fps() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca %struct.timeval
%4 = alloca float
store i64 0, i64* %2
%5 = load i32, i32* @update_fps.frame_count
%6 = add nsw i32 %5, 1
store i32 %6, i32* @update_fps.frame_count
%7 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%8 = load i64, i64* @update_fps.time_now
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%12 = load i64, i64* %11
%13 = mul nsw i64 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000
%17 = sext i32 %16 to i64
%18 = add nsw i64 %13, %17
%19 = load i64, i64* @update_fps.time_now
%20 = sub nsw i64 %18, %19
store i64 %20, i64* %2
%21 = load i64, i64* %2
%22 = load i64, i64* @update_fps.time_now
%23 = add nsw i64 %22, %21
store i64 %23, i64* @update_fps.time_now
br label %33
24: ; preds = %0
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%26 = load i64, i64* %25
%27 = mul nsw i64 %26, 1000
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%29 = load i32, i32* %28
%30 = sdiv i32 %29, 1000
%31 = sext i32 %30 to i64
%32 = add nsw i64 %27, %31
store i64 %32, i64* @update_fps.time_now
br label %33
33: ; preds = %24, %10
%34 = load i64, i64* @update_fps.time_start
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i64, i64* @update_fps.time_now
store i64 %37, i64* @update_fps.time_start
br label %60
38: ; preds = %33
%39 = load i64, i64* @update_fps.time_now
%40 = load i64, i64* @update_fps.time_start
%41 = sub nsw i64 %39, %40
%42 = icmp sgt i64 %41, 5000
br i1 %42, label %43, label %59
43: ; preds = %38
%44 = load i32, i32* @update_fps.frame_count
%45 = sitofp i32 %44 to float
%46 = fpext float %45 to double
%47 = load i64, i64* @update_fps.time_now
%48 = load i64, i64* @update_fps.time_start
%49 = sub nsw i64 %47, %48
%50 = sitofp i64 %49 to double
%51 = fdiv double %50, 1.000000e+03
%52 = fdiv double %46, %51
%53 = fptrunc double %52 to float
store float %53, float* %4
store i32 0, i32* @update_fps.frame_count
%54 = load i64, i64* @update_fps.time_now
store i64 %54, i64* @update_fps.time_start
%55 = load float, float* %4
%56 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), float %55)
%57 = load i32, i32* @stdout
%58 = call i32 @fflush(i32 %57)
br label %59
59: ; preds = %43, %38
br label %60
60: ; preds = %59, %36
%61 = load i64, i64* %1
ret i64 %61
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@update_fps.frame_count = internal unnamed_addr global i32 0
@update_fps.time_start = internal unnamed_addr global i64 0
@.str = [11 x i8] c"%3.2f FPS\0A\00"
@stdout = global i32 0
define i64 @update_fps() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = load i32, i32* @update_fps.frame_count
%4 = add nsw i32 %3, 1
store i32 %4, i32* @update_fps.frame_count
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%7 = load i64, i64* %6
%8 = mul nsw i64 %7, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = sdiv i32 %10, 1000
%12 = sext i32 %11 to i64
%13 = add nsw i64 %8, %12
%14 = load i64, i64* @update_fps.time_start
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %0
store i64 %13, i64* @update_fps.time_start
br label %31
17: ; preds = %0
%18 = sub nsw i64 %13, %14
%19 = icmp sgt i64 %18, 5000
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load i32, i32* @update_fps.frame_count
%22 = sitofp i32 %21 to float
%23 = fpext float %22 to double
%24 = sitofp i64 %18 to double
%25 = fdiv double %24, 1.000000e+03
%26 = fdiv double %23, %25
%27 = fptrunc double %26 to float
store i32 0, i32* @update_fps.frame_count
store i64 %13, i64* @update_fps.time_start
%28 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), float %27)
%29 = load i32, i32* @stdout
%30 = tail call i32 @fflush(i32 %29)
br label %31
31: ; preds = %17, %20, %16
ret i64 undef
}