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

/* Variables and functions */
int /*<<< orphan*/ ITIMER_REAL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setitimer(int /*<<< orphan*/, int /*<<< orphan*/ *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
void event_handler(int signum) {
  static unsigned long cnt = 0;
  static struct timeval tsFirst;
  if (cnt == 0) {
    gettimeofday(&tsFirst, 0);
  }
  cnt++;
  if (cnt >= 1000) {
    struct timeval tsNow;
    struct timeval diff;
    setitimer(ITIMER_REAL, NULL, NULL);
    gettimeofday(&tsNow, 0);
    timersub(&tsNow, &tsFirst, &diff);
    unsigned long long udiff = (diff.tv_sec * 1000000) + diff.tv_usec;
    double delta = (double)(udiff / cnt) / 1000000;
    int hz = (unsigned)(1.0 / delta);
    printf("kernel timer interrupt frequency is approx. %d Hz", hz);
    if (hz >= (int)(1.0 / ((double)(250) / 1000000))) {
      printf(" or higher");
    }
    printf("\n");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<event_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
cmpq   $0x0,0x2eed(%rip)
jne    1177 <event_handler+0x27>
lea    0x2ee8(%rip),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    0x2ed2(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2ec7(%rip)
cmpq   $0x3e8,0x2ebc(%rip)
jb     1292 <event_handler+0x142>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
mov    %eax,%edx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsi
call   1050 <setitimer@plt>
mov    -0x44(%rbp),%esi
lea    -0x10(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1040 <gettimeofday@plt>
mov    -0x40(%rbp),%rdi
lea    0x2e87(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <timersub@plt>
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e53(%rip),%rcx
div    %rcx
movq   %rax,%xmm0
movaps 0xdf4(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xdf8(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xdf1(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  0xde3(%rip),%xmm0
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xfa0,-0x2c(%rbp)
jl     127d <event_handler+0x12d>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<event_handler>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eec(%rip),%rax
test   %rax,%rax
jne    1186 <event_handler+0x26>
lea    0x2ee8(%rip),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x2ed2(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2ec7(%rip)
cmp    $0x3e8,%rax
jae    119f <event_handler+0x3f>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <setitimer@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x2e99(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1080 <timersub@plt>
movslq (%rsp),%rax
imul   $0xf4240,%rax,%rcx
movslq 0x4(%rsp),%rax
add    %rcx,%rax
mov    0x2e6c(%rip),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    11fe <event_handler+0x9e>
xor    %edx,%edx
div    %ecx
jmp    1203 <event_handler+0xa3>
xor    %edx,%edx
div    %rcx
movq   %rax,%xmm0
punpckldq 0xdf0(%rip),%xmm0
subpd  0xdf8(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
divsd  0xdf4(%rip),%xmm1
movsd  0xdf4(%rip),%xmm0
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rbx
lea    0xdec(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xfa0,%ebx
jl     1263 <event_handler+0x103>
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@event_handler.cnt = internal global i64 0
@event_handler.tsFirst = internal global %struct.timeval zeroinitializer
@ITIMER_REAL = global i32 0
@.str = [50 x i8] c"kernel timer interrupt frequency is approx. %d Hz\00"
@.str.1 = [11 x i8] c" or higher\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @event_handler(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i64
%6 = alloca double
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i64, i64* @event_handler.cnt
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @gettimeofday(%struct.timeval* @event_handler.tsFirst, i32 0)
br label %12
12: ; preds = %10, %1
%13 = load i64, i64* @event_handler.cnt
%14 = add i64 %13, 1
store i64 %14, i64* @event_handler.cnt
%15 = load i64, i64* @event_handler.cnt
%16 = icmp uge i64 %15, 1000
br i1 %16, label %17, label %46
17: ; preds = %12
%18 = load i32, i32* @ITIMER_REAL
%19 = call i32 @setitimer(i32 %18, i32* null, i32* null)
%20 = call i32 @gettimeofday(%struct.timeval* %3, i32 0)
%21 = call i32 @timersub(%struct.timeval* %3, %struct.timeval* @event_handler.tsFirst, %struct.timeval* %4)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = mul nsw i32 %23, 1000000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%26 = load i32, i32* %25
%27 = add nsw i32 %24, %26
%28 = sext i32 %27 to i64
store i64 %28, i64* %5
%29 = load i64, i64* %5
%30 = load i64, i64* @event_handler.cnt
%31 = udiv i64 %29, %30
%32 = uitofp i64 %31 to double
%33 = fdiv double %32, 1.000000e+06
store double %33, double* %6
%34 = load double, double* %6
%35 = fdiv double 1.000000e+00, %34
%36 = fptoui double %35 to i32
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %7
%40 = icmp sge i32 %39, 4000
br i1 %40, label %41, label %43
41: ; preds = %17
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %17
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@event_handler.cnt = internal unnamed_addr global i64 0
@event_handler.tsFirst = internal global %struct.timeval zeroinitializer
@ITIMER_REAL = global i32 0
@.str = [50 x i8] c"kernel timer interrupt frequency is approx. %d Hz\00"
@.str.1 = [11 x i8] c" or higher\00"
define void @event_handler(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = load i64, i64* @event_handler.cnt
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @gettimeofday(%struct.timeval* @event_handler.tsFirst, i32 0)
%8 = load i64, i64* @event_handler.cnt
br label %9
9: ; preds = %6, %1
%10 = phi i64 [ %8, %6 ], [ %4, %1 ]
%11 = add i64 %10, 1
store i64 %11, i64* @event_handler.cnt
%12 = icmp ugt i64 %11, 999
br i1 %12, label %13, label %40
13: ; preds = %9
%14 = bitcast %struct.timeval* %2 to i8*
%15 = bitcast %struct.timeval* %3 to i8*
%16 = load i32, i32* @ITIMER_REAL
%17 = tail call i32 @setitimer(i32 %16, i32* null, i32* null)
%18 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%19 = call i32 @timersub(%struct.timeval* %2, %struct.timeval* @event_handler.tsFirst, %struct.timeval* %3)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000000
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = add nsw i32 %22, %24
%26 = sext i32 %25 to i64
%27 = load i64, i64* @event_handler.cnt
%28 = udiv i64 %26, %27
%29 = uitofp i64 %28 to double
%30 = fdiv double %29, 1.000000e+06
%31 = fdiv double 1.000000e+00, %30
%32 = fptoui double %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = icmp sgt i32 %32, 3999
br i1 %34, label %35, label %37
35: ; preds = %13
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %13
%38 = call i32 @putchar(i32 10)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %9
ret void
}