// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/cov.c
// Function: calc_cov_norms
cov_norm_t *calc_cov_norms(cov_t *cov, sdict_t *sdict, double q_drop)
{
    size_t i, j, k, s;
    int32_t c;
    uint32_t n, p, p1, w;
    int64_t m, c1;
    uint64_t *a;
    double *norm_a, **norm, avg_cov;
    cov_norm_t *cov_norm;
    
    n = sdict->n;
    m = 0;
    for (i = 0; i < n; ++i)
        m += div_ceil(sdict->s[i].len, COV_NORM_WINDOW);
    norm_a = (double *) calloc(m, sizeof(double));
    norm = (double **) calloc(n, sizeof(double *));
    norm[0] = norm_a;
    for (i = 1; i < n; ++i) 
        norm[i] = norm[i-1] + div_ceil(sdict->s[i-1].len, COV_NORM_WINDOW);

    // calculate average base coverage
    avg_cov = calc_avg_cov(cov, sdict, q_drop);
    for (i = 0; i < cov->n; ++i) {
        a = cov->p[i].a;
        s = cov->p[i].n;
        
        w = p = 0, c = 0, c1 = 0, k = 0;
        for (j = 0; j < s; ++j) {
            p1 = (uint32_t) (a[j] >> 32);
            if (p1 > p) {
                if (p1 - p < COV_NORM_WINDOW - w) {
                    c1 += (int64_t) c * (p1 - p);
                    w += p1 - p;
                    p = p1;
                } else {
                    while (p1 > p) {
                        if (p1 - p >= COV_NORM_WINDOW - w) {
                            c1 += (int64_t) c * (COV_NORM_WINDOW - w);
                            norm[i][k++] = MIN(max_cov_scale, c1 > 0? avg_cov * COV_NORM_WINDOW / c1 : DBL_MAX);
                            p += COV_NORM_WINDOW - w;
                            c1 = 0;
                            w = 0;
                        } else {
                            c1 += (int64_t) c * (p1 - p);
                            w += p1 - p;
                            p = p1;
                        }
                    }
                }
            }
            c += (int32_t) a[j];
        }

        p1 = sdict->s[i].len;
        while (p1 > p) {
            if (p1 - p >= COV_NORM_WINDOW - w) {
                norm[i][k++] = MIN(max_cov_scale, c1 > 0? avg_cov * COV_NORM_WINDOW / c1 : DBL_MAX);
                p += COV_NORM_WINDOW - w;
            } else {
                norm[i][k++] = MIN(max_cov_scale, c1 > 0? avg_cov * (p1 - p + w) / c1 : DBL_MAX);
                p = p1;
            }
            c1 = 0;
            w = 0;
        }

        if (w > 0)
            norm[i][k++] = MIN(max_cov_scale, c1 > 0? avg_cov * w / c1 : DBL_MAX);
        
        assert(k == div_ceil(p1, COV_NORM_WINDOW));
    }
    
    cov_norm = (cov_norm_t *) malloc(sizeof(cov_norm_t));
    cov_norm->n = m;
    cov_norm->w = COV_NORM_WINDOW;
    cov_norm->norm = norm;

    return cov_norm;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, 0x20(%rsp)
movl (%rsi), %r13d
testq %r13, %r13
jne 0x332b
xorl %r15d, %r15d
jmp 0x3357
imulq $0x18, %r13, %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%r12), %rax
movl 0x10(%rax,%r14), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4b01
movl %eax, %eax
addq %rax, %r15
addq $0x18, %r14
cmpq %r14, %rbx
jne 0x3335
movq %r12, 0x10(%rsp)
pushq $0x8
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2290
movq %rax, %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0x2290
movq %rax, %r12
movq %rbp, (%rax)
cmpl $0x2, %r13d
jb 0x33c7
leaq -0x3(,%r13,2), %rbx
addq %r13, %rbx
leaq 0x8(%r12), %r14
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax,%r13,8), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4b01
movl %eax, %eax
leaq (%rbp,%rax,8), %rbp
movq %rbp, (%r14)
addq $0x3, %r13
addq $0x8, %r14
cmpq %r13, %rbx
jne 0x3398
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
callq 0x2fa0
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, (%rbx)
je 0x35e8
movsd 0x202ba(%rip), %xmm2    # 0x236b0
mulsd 0x8(%rsp), %xmm2
xorl %r13d, %r13d
movsd 0x1f259(%rip), %xmm3    # 0x22660
movsd %xmm2, 0x28(%rsp)
movq 0x8(%rbx), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rsi
testq %rsi, %rsi
movq %rcx, 0x30(%rsp)
je 0x34fb
movq 0x10(%rax,%rcx), %rax
movq %rax, 0x18(%rsp)
xorl %r8d, %r8d
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x18(%rsp), %rdi
movq (%rdi,%r8,8), %r14
movq %r14, %r11
shrq $0x20, %r11
movl %r11d, %r10d
subl %eax, %r10d
jbe 0x3475
movl $0x3e8, %edi            # imm = 0x3E8
subl %edx, %edi
movslq %r9d, %rbx
cmpl %edi, %r10d
jae 0x347a
movl %r10d, %eax
imulq %rbx, %rax
addq %rax, %rcx
addl %r10d, %edx
jmp 0x34e1
movl %edx, %r10d
jmp 0x34e7
movq %rcx, %r10
movl %r11d, %edi
subl %eax, %edi
movl $0x3e8, %ecx            # imm = 0x3E8
subl %edx, %ecx
cmpl %ecx, %edi
jb 0x34d6
movl %ecx, %edx
imulq %rbx, %rdx
addq %r10, %rdx
movapd %xmm3, %xmm0
testq %rdx, %rdx
jle 0x34b4
cvtsi2sd %rdx, %xmm0
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movapd %xmm3, %xmm0
minsd %xmm1, %xmm0
movq (%r12,%r13,8), %rdx
movsd %xmm0, (%rdx,%rbp,8)
incq %rbp
addl %ecx, %eax
xorl %r10d, %r10d
movl $0x0, %edx
movl $0x0, %ecx
cmpl %r11d, %eax
jb 0x347d
jmp 0x34e7
movl %edi, %ecx
imulq %rbx, %rcx
addq %r10, %rcx
addl %edi, %edx
movl %edx, %r10d
movl %r11d, %eax
addl %r14d, %r9d
incq %r8
movl %r10d, %edx
cmpq %rsi, %r8
jne 0x343f
jmp 0x3504
xorl %r10d, %r10d
xorl %ecx, %ecx
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x30(%rsp), %rsi
movl 0x10(%rdx,%rsi), %edi
cmpl %eax, %edi
jbe 0x3564
movl %edi, %esi
subl %eax, %esi
movl $0x3e8, %edx            # imm = 0x3E8
subl %r10d, %edx
cmpl %edx, %esi
jb 0x357c
movapd %xmm3, %xmm0
testq %rcx, %rcx
jle 0x3548
cvtsi2sd %rcx, %xmm0
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movapd %xmm3, %xmm0
minsd %xmm1, %xmm0
movq (%r12,%r13,8), %rcx
movsd %xmm0, (%rcx,%rbp,8)
addl %edx, %eax
incq %rbp
xorl %ecx, %ecx
movl $0x0, %r10d
cmpl %eax, %edi
ja 0x351a
jmp 0x35b6
testl %r10d, %r10d
je 0x35b6
movapd %xmm3, %xmm1
testq %rcx, %rcx
jle 0x35aa
movl %r10d, %eax
cvtsi2sd %rax, %xmm0
jmp 0x358d
movapd %xmm3, %xmm1
testq %rcx, %rcx
jle 0x35aa
addl %r10d, %esi
cvtsi2sd %rsi, %xmm0
mulsd 0x8(%rsp), %xmm0
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
ucomisd %xmm3, %xmm0
movapd %xmm3, %xmm1
ja 0x35aa
movapd %xmm0, %xmm1
movq (%r12,%r13,8), %rax
movsd %xmm1, (%rax,%rbp,8)
incq %rbp
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4b01
movl %eax, %eax
cmpq %rax, %rbp
jne 0x360d
incq %r13
movq 0x20(%rsp), %rbx
movl (%rbx), %eax
cmpq %rax, %r13
movsd 0x28(%rsp), %xmm2
movsd 0x1f07e(%rip), %xmm3    # 0x22660
jb 0x340d
pushq $0x18
popq %rdi
callq 0x2330
movq %r15, (%rax)
movl $0x3e8, 0x8(%rax)       # imm = 0x3E8
movq %r12, 0x10(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2022d(%rip), %rdi     # 0x23841
leaq 0x20249(%rip), %rsi     # 0x23864
leaq 0x20248(%rip), %rcx     # 0x2386a
movl $0x1da, %edx            # imm = 0x1DA
callq 0x21c0