// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/counter.c
// Function: countersCron
void countersCron(void) {
    int history_last_index;
    dictIterator *it;
    dictEntry *de;
    mstime_t now = mstime();

    history_last_index = server.history_index;
    server.history_index = (server.history_index + 1) % server.history_size;

    it = dictGetIterator(server.counters);
    while ((de = dictNext(it)) != NULL) {
        long double rvalue, elapsed, diff, change;
        counter *cntr;
        int miss;

        cntr = dictGetVal(de);

        counterPubSub(cntr, now);

        if (cntr->myshard == NULL) {
            continue;
        }

        change = counterHistoryChange(cntr, now, history_last_index);

        if (cntr->myshard->predict_time > 0) {
            miss = 0;

            /* When our shard doesn't change anymore we should make sure it's predicted value
             * matches the actual value otherwise we never have a mis-prediction and
             * other instances will keep using our wrongly predicted value */
            if (change == 0 && (cntr->myshard->predict_change != 0 || cntr->myshard->predict_value != cntr->myshard->value)) {
                miss = 1;

                serverLog(LL_DEBUG,"Counter %s needs a final prediction (%Lf != %Lf)",
                      cntr->name, cntr->myshard->predict_value, cntr->myshard->value);
            } else {
                elapsed = (now - cntr->myshard->predict_time);
                rvalue = cntr->myshard->predict_value + (elapsed * cntr->myshard->predict_change);
                diff = fabsl(rvalue - cntr->myshard->value);

                if (diff > cntr->precision) {
                    miss = 1;

                    serverLog(LL_DEBUG,"Counter %s needs new prediction (%Lf <= %f)",
                        cntr->name, diff, cntr->precision);
                }
            }

            if (miss == 0) {
                /* It's still up to date, check if we need to resend our last prediction. */
                counterMaybeResend(cntr);

                /* Only count hits for when something actually changed but
                 * is still within our last prediction. */
                if (cntr->history[history_last_index] != cntr->myshard->value) {
                    server.stat_hits++;
                    cntr->hits++;
                }

                counterHistoryUpdate(cntr);

                continue;
            }
        }

        /* Make a new prediction. */

        server.stat_misses++;
        cntr->misses++;
        cntr->revision++;

        counterPredict(cntr, now, change);
        clusterSendShard(cntr);

        counterHistoryUpdate(cntr);

        cntr->updated = mstime();
    }
    dictReleaseIterator(it);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
callq 0x15db3
movq %rax, %rbx
leaq 0x3f5c2(%rip), %rbp     # 0x6a088
movslq 0x62c(%rbp), %r15
leal 0x1(%r15), %eax
xorl %edx, %edx
divl 0x628(%rbp)
movl %edx, 0x62c(%rbp)
movq 0x618(%rbp), %rdi
callq 0x14f27
movq %rax, %r14
leaq 0x275dc(%rip), %r12     # 0x520d1
shlq $0x4, %r15
movq %r14, %rdi
callq 0x14f5b
testq %rax, %rax
je 0x2aca6
movq 0x8(%rax), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x29fd7
movq 0x10(%r13), %rcx
testq %rcx, %rcx
je 0x2aaf9
fldt 0x30(%rcx)
movq 0x30(%r13), %rax
movslq 0x62c(%rbp), %rdx
shlq $0x4, %rdx
fldt (%rax,%rdx)
fsubr %st(1), %st
movq 0x40(%rcx), %rax
testq %rax, %rax
jle 0x2abfd
fldz
fxch %st(1)
fucomi %st(1), %st
jne 0x2ab9b
jp 0x2ab9b
fldt 0x60(%rcx)
fxch %st(2)
fucompi %st(2), %st
setp %dl
setne %sil
orb %dl, %sil
fldt 0x50(%rcx)
fucomi %st(3), %st
setp %cl
setne %dl
orb %cl, %dl
orb %sil, %dl
cmpb $0x1, %dl
jne 0x2aba7
fstp %st(2)
movq (%r13), %rdx
fxch %st(2)
fstpt 0x10(%rsp)
fstpt (%rsp)
xorl %edi, %edi
leaq 0x27512(%rip), %rsi     # 0x520a0
xorl %eax, %eax
fstpt 0x24(%rsp)
callq 0x15b9e
jmp 0x2abf5
fstp %st(1)
fldt 0x50(%rcx)
fldt 0x60(%rcx)
fxch %st(2)
fxch %st(1)
movq %rbx, %rcx
subq %rax, %rcx
movq %rcx, 0x40(%rsp)
fildll 0x40(%rsp)
fmulp %st, %st(3)
faddp %st, %st(2)
fxch %st(1)
fsubp %st, %st(2)
fxch %st(1)
fabs
movsd 0x50(%r13), %xmm0
movsd %xmm0, 0x38(%rsp)
fldl 0x38(%rsp)
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
jbe 0x2ac65
movq (%r13), %rdx
fstpt (%rsp)
xorl %edi, %edi
movq %r12, %rsi
movb $0x1, %al
fstpt 0x24(%rsp)
callq 0x15b9e
fldt 0x24(%rsp)
fldz
fxch %st(1)
fstp %st(1)
incq 0x3c8(%rbp)
incl 0x5c(%r13)
incl 0x38(%r13)
movq 0x10(%r13), %rax
movq %rbx, 0x40(%rax)
fldt 0x30(%rax)
fstpt 0x50(%rax)
movl 0x628(%rbp), %ecx
movq %rcx, 0x30(%rsp)
fildll 0x30(%rsp)
fmuls 0x27343(%rip)           # 0x51f74
fdivrp %st, %st(1)
fstpt 0x60(%rax)
movq %r13, %rdi
callq 0x27b68
movq 0x10(%r13), %rax
movq 0x30(%r13), %rcx
fldt 0x30(%rax)
movslq 0x62c(%rbp), %rax
shlq $0x4, %rax
fstpt (%rcx,%rax)
callq 0x15db3
movq %rax, 0x48(%r13)
jmp 0x2aaf9
fstp %st(0)
fstp %st(0)
movq %r13, %rdi
callq 0x2aa1d
movq 0x10(%r13), %rcx
movq 0x30(%r13), %rax
fldt (%rax,%r15)
fldt 0x30(%rcx)
fxch %st(1)
fucompi %st(1), %st
jne 0x2ac88
jnp 0x2ac93
incq 0x3c0(%rbp)
incl 0x58(%r13)
movslq 0x62c(%rbp), %rcx
shlq $0x4, %rcx
fstpt (%rax,%rcx)
jmp 0x2aaf9
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1501c