// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/latency.c
// Function: analyzeLatencyForEvent
void analyzeLatencyForEvent(char *event, struct latencyStats *ls) {
    struct latencyTimeSeries *ts = dictFetchValue(server.latency_events,event);
    int j;
    uint64_t sum;

    ls->all_time_high = ts ? ts->max : 0;
    ls->avg = 0;
    ls->min = 0;
    ls->max = 0;
    ls->mad = 0;
    ls->samples = 0;
    ls->period = 0;
    if (!ts) return;

    /* First pass, populate everything but the MAD. */
    sum = 0;
    for (j = 0; j < LATENCY_TS_LEN; j++) {
        if (ts->samples[j].time == 0) continue;
        ls->samples++;
        if (ls->samples == 1) {
            ls->min = ls->max = ts->samples[j].latency;
        } else {
            if (ls->min > ts->samples[j].latency)
                ls->min = ts->samples[j].latency;
            if (ls->max < ts->samples[j].latency)
                ls->max = ts->samples[j].latency;
        }
        sum += ts->samples[j].latency;

        /* Track the oldest event time in ls->period. */
        if (ls->period == 0 || ts->samples[j].time < ls->period)
            ls->period = ts->samples[j].time;
    }

    /* So far avg is actually the sum of the latencies, and period is
     * the oldest event time. We need to make the first an average and
     * the second a range of seconds. */
    if (ls->samples) {
        ls->avg = sum / ls->samples;
        ls->period = time(NULL) - ls->period;
        if (ls->period == 0) ls->period = 1;
    }

    /* Second pass, compute MAD. */
    sum = 0;
    for (j = 0; j < LATENCY_TS_LEN; j++) {
        int64_t delta;

        if (ts->samples[j].time == 0) continue;
        delta = (int64_t)ls->avg - ts->samples[j].latency;
        if (delta < 0) delta = -delta;
        sum += delta;
    }
    if (ls->samples) ls->mad = sum / ls->samples;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %rbx
movq %rdi, %rsi
leaq 0x82bba(%rip), %rax     # 0x1018a8
movq 0xa20(%rax), %rdi
callq 0x2ef19
movq %rax, %r14
testq %rax, %rax
je 0x7ed08
movl 0x4(%r14), %eax
jmp 0x7ed0a
xorl %eax, %eax
movl %eax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rbx)
movups %xmm0, 0x10(%rbx)
testq %r14, %r14
je 0x7ee16
leaq 0x4(%rbx), %r15
xorl %r8d, %r8d
xorl %edx, %edx
xorl %esi, %esi
xorl %r10d, %r10d
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x8(%r14,%rcx,8), %rdi
testq %rdi, %rdi
je 0x7ed68
leal 0x1(%r10), %r9d
movl %r9d, 0x14(%rbx)
movl 0xc(%r14,%rcx,8), %r11d
testl %r10d, %r10d
je 0x7ed6d
cmpl %r11d, %esi
jbe 0x7ed5a
movl %r11d, 0x8(%rbx)
movl %r11d, %esi
cmpl %r11d, %edx
jae 0x7ed7b
movl %r11d, 0xc(%rbx)
movl %r11d, %edx
jmp 0x7ed7b
movq %r8, %rdi
jmp 0x7ed9d
movl %r11d, 0xc(%rbx)
movl %r11d, 0x8(%rbx)
movl %r11d, %edx
movl %r11d, %esi
addq %r11, %rax
testq %r8, %r8
setne %r10b
cmpq %rdi, %r8
setle %r11b
testb %r11b, %r10b
jne 0x7ed97
movq %rdi, 0x18(%rbx)
jmp 0x7ed9a
movq %r8, %rdi
movl %r9d, %r10d
incq %rcx
movq %rdi, %r8
cmpq $0xa0, %rcx
jne 0x7ed32
testl %r10d, %r10d
je 0x7edd4
movl %r10d, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, 0x4(%rbx)
xorl %edi, %edi
callq 0x26cd0
subq 0x18(%rbx), %rax
movl $0x1, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x18(%rbx)
xorl %ecx, %ecx
xorl %eax, %eax
cmpl $0x0, 0x8(%r14,%rcx,8)
je 0x7edfa
movl (%r15), %edx
movl 0xc(%r14,%rcx,8), %esi
subq %rsi, %rdx
movq %rdx, %rsi
negq %rsi
cmovsq %rdx, %rsi
movl %esi, %edx
addq %rdx, %rax
incq %rcx
cmpq $0xa0, %rcx
jne 0x7edd8
movl 0x14(%rbx), %ecx
testq %rcx, %rcx
je 0x7ee16
xorl %edx, %edx
divq %rcx
movl %eax, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq