// File: /workspace/github_c_makefile_star2_O1/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 0x1a1d3
movq %rax, %rbx
leaq 0x3ce8a(%rip), %rbp     # 0x6f088
movslq 0x62c(%rbp), %r12
leal 0x1(%r12), %eax
xorl %edx, %edx
divl 0x628(%rbp)
movl %edx, 0x62c(%rbp)
movq 0x618(%rbp), %rdi
callq 0x19242
movq %rax, %r14
movq %rax, %rdi
callq 0x1929d
testq %rax, %rax
je 0x323dd
leaq 0x267b9(%rip), %r15     # 0x589f8
shlq $0x4, %r12
movq 0x8(%rax), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x31509
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x3238c
fldt 0x30(%rax)
movq 0x30(%r13), %rcx
movslq 0x62c(%rbp), %rdx
shlq $0x4, %rdx
fldt (%rcx,%rdx)
fsubr %st(1), %st
movq 0x40(%rax), %rcx
testq %rcx, %rcx
jle 0x32329
fldz
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
jne 0x322cb
jp 0x322cb
fldt 0x60(%rax)
fldz
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jne 0x322a7
jp 0x322a7
fldt 0x50(%rax)
fucompi %st(2), %st
jne 0x322a7
jnp 0x322cb
movq (%r13), %rdx
fldt 0x50(%rax)
fxch %st(2)
fstpt 0x10(%rsp)
fxch %st(1)
fstpt (%rsp)
xorl %edi, %edi
movq %r15, %rsi
xorl %eax, %eax
fstpt 0x24(%rsp)
callq 0x19fb1
jmp 0x32321
movq %rbx, %rdx
subq %rcx, %rdx
movq %rdx, 0x40(%rsp)
fildll 0x40(%rsp)
fldt 0x50(%rax)
fldt 0x60(%rax)
fmulp %st, %st(2)
faddp %st, %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 0x3239f
movq (%r13), %rdx
fstpt (%rsp)
xorl %edi, %edi
leaq 0x26713(%rip), %rsi     # 0x58a29
movb $0x1, %al
fstpt 0x24(%rsp)
callq 0x19fb1
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 0x26537(%rip)           # 0x58894
fdivrp %st, %st(1)
fstpt 0x60(%rax)
movq %r13, %rdi
callq 0x2ed11
movq 0x10(%r13), %rax
movq 0x30(%r13), %rcx
fldt 0x30(%rax)
movslq 0x62c(%rbp), %rax
shlq $0x4, %rax
fstpt (%rcx,%rax)
callq 0x1a1d3
movq %rax, 0x48(%r13)
movq %r14, %rdi
callq 0x1929d
testq %rax, %rax
jne 0x32243
jmp 0x323dd
fstp %st(0)
fstp %st(0)
movq %r13, %rdi
callq 0x32147
movq 0x10(%r13), %rcx
movq 0x30(%r13), %rax
fldt (%rax,%r12)
fldt 0x30(%rcx)
fxch %st(1)
fucompi %st(1), %st
jne 0x323c2
jnp 0x323cd
incq 0x3c0(%rbp)
incl 0x58(%r13)
movslq 0x62c(%rbp), %rcx
shlq $0x4, %rcx
fstpt (%rax,%rcx)
jmp 0x3238c
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1935a