// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/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 0x3ebc9(%rip), %rax     # 0x6f088
movq 0x678(%rax), %rdi
callq 0x191a0
movq %rax, %r14
testq %rax, %rax
je 0x304d9
movl 0x4(%r14), %eax
jmp 0x304db
xorl %eax, %eax
movl %eax, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rbx)
movups %xmm0, 0x10(%rbx)
testq %r14, %r14
je 0x305c2
leaq 0x4(%rbx), %r15
xorl %ecx, %ecx
xorl %eax, %eax
movslq 0x8(%r14,%rcx,8), %rdx
testq %rdx, %rdx
je 0x3054c
movl 0x14(%rbx), %esi
leal 0x1(%rsi), %edi
movl %edi, 0x14(%rbx)
testl %esi, %esi
je 0x30527
movl 0xc(%r14,%rcx,8), %esi
cmpl %esi, 0x8(%rbx)
jbe 0x3051d
movl %esi, 0x8(%rbx)
cmpl %esi, 0xc(%rbx)
jae 0x30532
movl %esi, 0xc(%rbx)
jmp 0x30532
movl 0xc(%r14,%rcx,8), %esi
movl %esi, 0xc(%rbx)
movl %esi, 0x8(%rbx)
movl 0xc(%r14,%rcx,8), %esi
addq %rsi, %rax
movq 0x18(%rbx), %rsi
testq %rsi, %rsi
je 0x30548
cmpq %rdx, %rsi
jle 0x3054c
movq %rdx, 0x18(%rbx)
incq %rcx
cmpq $0xa0, %rcx
jne 0x304f9
movl 0x14(%rbx), %ecx
testq %rcx, %rcx
je 0x30580
xorl %edx, %edx
divq %rcx
movl %eax, 0x4(%rbx)
xorl %edi, %edi
callq 0x12550
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 0x305a6
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 0x30584
movl 0x14(%rbx), %ecx
testq %rcx, %rcx
je 0x305c2
xorl %edx, %edx
divq %rcx
movl %eax, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq