// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %r15
movq %rsi, 0x8(%rsp)
movl (%rsi), %r13d
imulq $0x18, %r13, %r14
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %rbx, %r14
je 0x132f1
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax,%rbx), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x28ff
movl %eax, %eax
addq %rax, %rbp
addq $0x18, %rbx
jmp 0x132ca
movq %r15, 0x18(%rsp)
pushq $0x8
popq %r12
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2290
movq %rax, %rbp
movq %r13, %rdi
movq %r12, %rsi
callq 0x2290
movq %rax, %r12
movq %rbp, (%rax)
pushq $0x1
popq %r14
pushq $0x2
popq %r15
cmpq %r13, %r14
jae 0x13356
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movl (%rax,%r15,8), %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x28ff
movl %eax, %eax
leaq (%rbp,%rax,8), %rbp
movq %rbp, (%r12,%r14,8)
incq %r14
addq $0x3, %r15
jmp 0x13326
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
callq 0x12dea
movsd 0xc202(%rip), %xmm3     # 0x1f578
mulsd %xmm0, %xmm3
xorl %r13d, %r13d
movsd 0xb293(%rip), %xmm4     # 0x1e618
movsd %xmm0, 0x30(%rsp)
movsd %xmm3, 0x28(%rsp)
movl (%rbx), %eax
cmpq %rax, %r13
jae 0x13580
movq 0x8(%rbx), %rax
imulq $0x18, %r13, %rcx
movq (%rax,%rcx), %rdi
movq %rcx, 0x10(%rsp)
movq 0x10(%rax,%rcx), %r8
xorl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
xorl %r9d, %r9d
xorl %ebp, %ebp
xorl %r10d, %r10d
cmpq %rdi, %r10
je 0x13464
movq (%r8,%r10,8), %r14
movq %r14, %r11
shrq $0x20, %r11
movl %r11d, %ebx
subl %edx, %ebx
jbe 0x13453
movl $0x3e8, %esi            # imm = 0x3E8
subl %ecx, %esi
movslq %r9d, %r15
cmpl %esi, %ebx
jae 0x133f6
movl %ebx, %edx
imulq %r15, %rdx
addq %rdx, %rax
addl %ebx, %ecx
jmp 0x13456
movl %r11d, %esi
subl %edx, %esi
jbe 0x13453
movl $0x3e8, %ebx            # imm = 0x3E8
subl %ecx, %ebx
cmpl %ebx, %esi
jae 0x13418
movl %esi, %edx
imulq %r15, %rdx
addq %rdx, %rax
addl %esi, %ecx
movl %r11d, %edx
jmp 0x133f6
movl %ebx, %ecx
imulq %r15, %rcx
addq %rax, %rcx
movapd %xmm4, %xmm2
testq %rcx, %rcx
jle 0x1343f
cvtsi2sd %rcx, %xmm2
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd %xmm4, %xmm2
minsd %xmm1, %xmm2
movq (%r12,%r13,8), %rax
movsd %xmm2, (%rax,%rbp,8)
incq %rbp
addl %ebx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
jmp 0x133f6
movl %edx, %r11d
addl %r14d, %r9d
incq %r10
movl %r11d, %edx
jmp 0x133c0
movq 0x8(%rsp), %rsi
movq 0x8(%rsi), %rsi
movq 0x10(%rsp), %rdi
movl 0x10(%rsi,%rdi), %edi
movl %edi, %r8d
subl %edx, %r8d
jbe 0x134f5
movl $0x3e8, %esi            # imm = 0x3E8
subl %ecx, %esi
cmpl %esi, %r8d
jae 0x134c3
movapd %xmm4, %xmm1
testq %rax, %rax
jle 0x134b6
addl %ecx, %r8d
cvtsi2sd %r8, %xmm2
mulsd %xmm0, %xmm2
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm2
ucomisd %xmm4, %xmm2
movapd %xmm4, %xmm1
ja 0x134b6
movapd %xmm2, %xmm1
movq (%r12,%r13,8), %rax
movsd %xmm1, (%rax,%rbp,8)
movl %edi, %edx
jmp 0x134ec
movapd %xmm4, %xmm2
testq %rax, %rax
jle 0x134e1
cvtsi2sd %rax, %xmm2
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd %xmm4, %xmm2
minsd %xmm1, %xmm2
movq (%r12,%r13,8), %rax
movsd %xmm2, (%rax,%rbp,8)
addl %esi, %edx
incq %rbp
xorl %ecx, %ecx
xorl %eax, %eax
jmp 0x13476
testl %ecx, %ecx
je 0x13530
movapd %xmm4, %xmm1
testq %rax, %rax
jle 0x13524
movl %ecx, %ecx
cvtsi2sd %rcx, %xmm2
mulsd %xmm0, %xmm2
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm2
ucomisd %xmm4, %xmm2
movapd %xmm4, %xmm1
ja 0x13524
movapd %xmm2, %xmm1
movq (%r12,%r13,8), %rax
movsd %xmm1, (%rax,%rbp,8)
incq %rbp
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x28ff
movl %eax, %eax
incq %r13
cmpq %rax, %rbp
movq 0x18(%rsp), %rbx
movsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm3
movsd 0xb0bd(%rip), %xmm4     # 0x1e618
je 0x13391
leaq 0xc1a1(%rip), %rdi      # 0x1f709
leaq 0xc1bd(%rip), %rsi      # 0x1f72c
leaq 0xc1bc(%rip), %rcx      # 0x1f732
movl $0x1da, %edx            # imm = 0x1DA
callq 0x21c0
pushq $0x18
popq %rdi
callq 0x2330
movq 0x20(%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