// File: /workspace/github_c_makefile_star2_O2/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 0x878c7(%rip), %rax     # 0xf98a8
movq 0xa20(%rax), %rdi
callq 0x2a900
movq %rax, %r14
testq %rax, %rax
je 0x71ffb
movl 0x4(%r14), %eax
jmp 0x71ffd
xorl %eax, %eax
movl %eax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rbx)
movups %xmm0, 0x10(%rbx)
testq %r14, %r14
je 0x7210d
leaq 0x4(%rbx), %r15
xorl %edi, %edi
xorl %edx, %edx
xorl %esi, %esi
xorl %r10d, %r10d
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0xa0, %rcx
je 0x720a3
movslq 0x8(%r14,%rcx,8), %r8
testq %r8, %r8
je 0x72063
leal 0x1(%r10), %r9d
movl %r9d, 0x14(%rbx)
movl 0xc(%r14,%rcx,8), %r11d
testl %r10d, %r10d
je 0x7206b
cmpl %r11d, %esi
jbe 0x72055
movl %r11d, 0x8(%rbx)
movl %r11d, %esi
cmpl %r11d, %edx
jae 0x72079
movl %r11d, 0xc(%rbx)
movl %r11d, %edx
jmp 0x72079
movq %rdi, %r8
movl %r10d, %r9d
jmp 0x72098
movl %r11d, 0xc(%rbx)
movl %r11d, 0x8(%rbx)
movl %r11d, %edx
movl %r11d, %esi
addq %r11, %rax
testq %rdi, %rdi
setne %r10b
cmpq %r8, %rdi
setle %r11b
testb %r11b, %r10b
jne 0x72095
movq %r8, 0x18(%rbx)
jmp 0x72098
movq %rdi, %r8
incq %rcx
movq %r8, %rdi
movl %r9d, %r10d
jmp 0x72024
testl %r10d, %r10d
je 0x720c9
movl %r10d, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, 0x4(%rbx)
xorl %edi, %edi
callq 0x26cd0
subq 0x18(%rbx), %rax
pushq $0x1
popq %rcx
cmovneq %rax, %rcx
movq %rcx, 0x18(%rbx)
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0xa0, %rcx
je 0x720fd
cmpl $0x0, 0x8(%r14,%rcx,8)
je 0x720f8
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
jmp 0x720cd
movl 0x14(%rbx), %ecx
testq %rcx, %rcx
je 0x7210d
xorl %edx, %edx
divq %rcx
movl %eax, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq