// File: /workspace/github_c_makefile_star2_O1/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
movq %rsi, %r15
movq %rdi, 0x10(%rsp)
movl (%rsi), %r13d
testq %r13, %r13
movsd %xmm0, 0x8(%rsp)
jne 0x3361
xorl %r12d, %r12d
jmp 0x338c
imulq $0x18, %r13, %rbx
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x8(%r15), %rax
movl 0x10(%rax,%r14), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4bf1
movl %eax, %eax
addq %rax, %r12
addq $0x18, %r14
cmpq %r14, %rbx
jne 0x336b
movq %r12, %rdi
pushq $0x8
popq %r12
movq %rdi, 0x18(%rsp)
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 0x33f9
leaq -0x3(,%r13,2), %r14
addq %r13, %r14
leaq 0x8(%r12), %r13
xorl %ebp, %ebp
movq -0x8(%r13), %rbx
movq 0x8(%r15), %rax
movl 0x10(%rax,%rbp,8), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4bf1
movl %eax, %eax
leaq (%rbx,%rax,8), %rax
movq %rax, (%r13)
addq $0x3, %rbp
addq $0x8, %r13
cmpq %rbp, %r14
jne 0x33cc
movq 0x10(%rsp), %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
movq %rdi, %rbx
movsd 0x8(%rsp), %xmm0
callq 0x2fea
cmpl $0x0, (%rbx)
je 0x3659
movsd 0x1f24b(%rip), %xmm3    # 0x22670
mulsd %xmm0, %xmm3
xorl %r13d, %r13d
movsd 0x1e1fc(%rip), %xmm4    # 0x21630
movsd %xmm0, 0x28(%rsp)
movsd %xmm3, 0x20(%rsp)
movq 0x8(%rbx), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rdi
testq %rdi, %rdi
je 0x351f
movq %rcx, %r15
movq 0x10(%rax,%rcx), %rax
movq %rax, 0x8(%rsp)
xorl %r9d, %r9d
xorl %ebp, %ebp
xorl %r10d, %r10d
xorl %eax, %eax
xorl %edx, %edx
xorl %esi, %esi
movq 0x8(%rsp), %rcx
movq (%rcx,%r9,8), %r14
movq %r14, %r11
shrq $0x20, %r11
movl %r11d, %ebx
subl %eax, %ebx
jbe 0x350e
movl $0x3e8, %r8d            # imm = 0x3E8
subl %esi, %r8d
movslq %r10d, %rcx
cmpl %r8d, %ebx
jae 0x34ac
movl %ebx, %eax
imulq %rcx, %rax
addq %rax, %rdx
addl %ebx, %esi
movl %r11d, %eax
jmp 0x350e
movl %r11d, %r8d
subl %eax, %r8d
movl $0x3e8, %ebx            # imm = 0x3E8
subl %esi, %ebx
cmpl %ebx, %r8d
jae 0x34d0
movl %r8d, %eax
imulq %rcx, %rax
addq %rax, %rdx
addl %r8d, %esi
movl %r11d, %eax
jmp 0x3509
movl %ebx, %esi
imulq %rcx, %rsi
addq %rdx, %rsi
movapd %xmm4, %xmm2
testq %rsi, %rsi
jle 0x34f7
cvtsi2sd %rsi, %xmm2
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd %xmm4, %xmm2
minsd %xmm1, %xmm2
movq (%r12,%r13,8), %rdx
movsd %xmm2, (%rdx,%rbp,8)
incq %rbp
addl %ebx, %eax
xorl %esi, %esi
xorl %edx, %edx
cmpl %r11d, %eax
jb 0x34ac
addl %r14d, %r10d
incq %r9
cmpq %rdi, %r9
jne 0x3470
jmp 0x352a
movq %rcx, %r15
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x30(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0x10(%rcx,%r15), %edi
cmpl %eax, %edi
jbe 0x35d8
movq %rdx, %rcx
leaq 0x1f12e(%rip), %r9      # 0x22678
movl %edi, %r8d
subl %eax, %r8d
movl $0x3e8, %edx            # imm = 0x3E8
subl %esi, %edx
movapd %xmm4, %xmm2
cmpl %edx, %r8d
jae 0x359a
testq %rcx, %rcx
jle 0x358d
addl %esi, %r8d
cvtsi2sd %r8, %xmm2
mulsd %xmm0, %xmm2
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm2
xorl %eax, %eax
ucomisd %xmm4, %xmm2
seta %cl
jbe 0x358d
movb %cl, %al
movsd (%r9,%rax,8), %xmm2
movq (%r12,%r13,8), %rax
movsd %xmm2, (%rax,%rbp,8)
movl %edi, %eax
jmp 0x35bf
testq %rcx, %rcx
jle 0x35b4
cvtsi2sd %rcx, %xmm2
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd %xmm4, %xmm2
minsd %xmm1, %xmm2
movq (%r12,%r13,8), %rcx
movsd %xmm2, (%rcx,%rbp,8)
addl %edx, %eax
incq %rbp
xorl %ecx, %ecx
movl $0x0, %esi
movl $0x0, %edx
cmpl %eax, %edi
ja 0x354a
jmp 0x35e1
movl %esi, %ecx
leaq 0x1f097(%rip), %r9      # 0x22678
testl %ecx, %ecx
je 0x3621
movapd %xmm4, %xmm2
testq %rdx, %rdx
jle 0x3615
movl %ecx, %eax
cvtsi2sd %rax, %xmm2
mulsd %xmm0, %xmm2
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm2
xorl %eax, %eax
ucomisd %xmm4, %xmm2
seta %cl
jbe 0x3615
movb %cl, %al
movsd (%r9,%rax,8), %xmm2
movq (%r12,%r13,8), %rax
movsd %xmm2, (%rax,%rbp,8)
incq %rbp
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4bf1
movl %eax, %eax
cmpq %rax, %rbp
jne 0x3683
incq %r13
movq 0x10(%rsp), %rbx
movl (%rbx), %eax
cmpq %rax, %r13
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm3
movsd 0x1dfdd(%rip), %xmm4    # 0x21630
jb 0x3440
pushq $0x18
popq %rdi
callq 0x2330
movq 0x18(%rsp), %rcx
movq %rcx, (%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 0x1f187(%rip), %rdi     # 0x22811
leaq 0x1f1a3(%rip), %rsi     # 0x22834
leaq 0x1f1a2(%rip), %rcx     # 0x2283a
movl $0x1da, %edx            # imm = 0x1DA
callq 0x21c0