// File: /workspace/github_c_makefile_star2_O3/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 0x19e27
movq %rax, %rbx
leaq 0x3d43d(%rip), %rbp     # 0x6f088
movslq 0x62c(%rbp), %r15
leal 0x1(%r15), %eax
xorl %edx, %edx
divl 0x628(%rbp)
movl %edx, 0x62c(%rbp)
movq 0x618(%rbp), %rdi
callq 0x18e9d
movq %rax, %r14
movq %rax, %rdi
callq 0x18ef7
testq %rax, %rax
je 0x31e38
leaq 0x26d8e(%rip), %r12     # 0x58a19
shlq $0x4, %r15
movq 0x8(%rax), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x30f51
movq 0x10(%r13), %rcx
testq %rcx, %rcx
je 0x31e27
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 0x31d86
fldz
fxch %st(1)
fucomi %st(1), %st
jne 0x31d24
jp 0x31d24
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 0x31d30
fstp %st(2)
movq (%r13), %rdx
fxch %st(2)
fstpt 0x10(%rsp)
fstpt (%rsp)
xorl %edi, %edi
leaq 0x26cd1(%rip), %rsi     # 0x589e8
xorl %eax, %eax
fstpt 0x24(%rsp)
callq 0x19c06
jmp 0x31d7e
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 0x31deb
movq (%r13), %rdx
fstpt (%rsp)
xorl %edi, %edi
movq %r12, %rsi
movb $0x1, %al
fstpt 0x24(%rsp)
callq 0x19c06
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 0x26aca(%rip)           # 0x58884
fdivrp %st, %st(1)
fstpt 0x60(%rax)
movq %r13, %rdi
callq 0x2e737
movq 0x10(%r13), %rax
movq 0x30(%r13), %rcx
fldt 0x30(%rax)
movslq 0x62c(%rbp), %rax
shlq $0x4, %rax
fstpt (%rcx,%rax)
callq 0x19e27
movq %rax, 0x48(%r13)
jmp 0x31e27
fstp %st(0)
fstp %st(0)
movq %r13, %rdi
callq 0x31b94
movq 0x10(%r13), %rcx
movq 0x30(%r13), %rax
fldt (%rax,%r15)
fldt 0x30(%rcx)
fxch %st(1)
fucompi %st(1), %st
jne 0x31e0e
jnp 0x31e19
incq 0x3c0(%rbp)
incl 0x58(%r13)
movslq 0x62c(%rbp), %rcx
shlq $0x4, %rcx
fstpt (%rax,%rcx)
movq %r14, %rdi
callq 0x18ef7
testq %rax, %rax
jne 0x31c8f
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18fbf