// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1eb04
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x2aa0
movl %eax, %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1eac1
movq -0x58(%rbp), %rdi
movl $0x8, %esi
callq 0x2290
movq %rax, -0x70(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq $0x1, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1ebaf
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x2aa0
movq -0x90(%rbp), %rdx
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rdx
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1eb3c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movsd -0x18(%rbp), %xmm0
callq 0x1e3a0
movsd %xmm0, -0x80(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jae 0x1f1df
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x1ee42
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jbe 0x1ee1f
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x1ecab
movslq -0x3c(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x44(%rbp), %ecx
movl %ecx, %ecx
imulq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1ee1d
jmp 0x1ecad
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jbe 0x1ee1b
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jb 0x1edec
movslq -0x3c(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
subl -0x4c(%rbp), %ecx
movl %ecx, %ecx
imulq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movsd 0x1a727(%rip), %xmm0    # 0x39418
movsd %xmm0, -0x98(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x1ed21
movsd 0x13878(%rip), %xmm0    # 0x32580
mulsd -0x80(%rbp), %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x1ed33
movsd 0x1384f(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xa0(%rbp)
jmp 0x1ed33
movsd -0x98(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1ed5b
movsd 0x1a6c7(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xa8(%rbp)
jmp 0x1eda5
cmpq $0x0, -0x60(%rbp)
jle 0x1ed83
movsd 0x13816(%rip), %xmm0    # 0x32580
mulsd -0x80(%rbp), %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x1ed95
movsd 0x137ed(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xb0(%rbp)
jmp 0x1ed95
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movsd %xmm0, (%rax,%rcx,8)
movl $0x3e8, %eax            # imm = 0x3E8
subl -0x4c(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x4c(%rbp)
jmp 0x1ee16
movslq -0x3c(%rbp), %rax
movl -0x48(%rbp), %ecx
subl -0x44(%rbp), %ecx
movl %ecx, %ecx
imulq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1ecad
jmp 0x1ee1d
jmp 0x1ee1f
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1ec3d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jbe 0x1f08b
movl -0x48(%rbp), %eax
subl -0x44(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jb 0x1ef71
movsd 0x1a596(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xb8(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x1eeb2
movsd 0x136e7(%rip), %xmm0    # 0x32580
mulsd -0x80(%rbp), %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0x1eec4
movsd 0x136be(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xc0(%rbp)
jmp 0x1eec4
movsd -0xb8(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1eeec
movsd 0x1a536(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xc8(%rbp)
jmp 0x1ef36
cmpq $0x0, -0x60(%rbp)
jle 0x1ef14
movsd 0x13685(%rip), %xmm0    # 0x32580
mulsd -0x80(%rbp), %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
jmp 0x1ef26
movsd 0x1365c(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xd0(%rbp)
jmp 0x1ef26
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movsd %xmm0, (%rax,%rcx,8)
movl $0x3e8, %eax            # imm = 0x3E8
subl -0x4c(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1f077
movsd 0x1a49f(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xd8(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x1efb7
movsd -0x80(%rbp), %xmm0
movl -0x48(%rbp), %eax
movl -0x44(%rbp), %ecx
subl %ecx, %eax
movl -0x4c(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xe0(%rbp)
jmp 0x1efc9
movsd 0x135b9(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xe0(%rbp)
jmp 0x1efc9
movsd -0xd8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1eff1
movsd 0x1a431(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xe8(%rbp)
jmp 0x1f049
cmpq $0x0, -0x60(%rbp)
jle 0x1f027
movsd -0x80(%rbp), %xmm0
movl -0x48(%rbp), %eax
movl -0x44(%rbp), %ecx
subl %ecx, %eax
movl -0x4c(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xf0(%rbp)
jmp 0x1f039
movsd 0x13549(%rip), %xmm0    # 0x32578
movsd %xmm0, -0xf0(%rbp)
jmp 0x1f039
movsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x4c(%rbp)
jmp 0x1ee58
cmpl $0x0, -0x4c(%rbp)
jbe 0x1f181
movsd 0x1a37b(%rip), %xmm0    # 0x39418
movsd %xmm0, -0xf8(%rbp)
cmpq $0x0, -0x60(%rbp)
jle 0x1f0d1
movsd -0x80(%rbp), %xmm0
movl -0x4c(%rbp), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
jmp 0x1f0e3
movsd 0x1349f(%rip), %xmm0    # 0x32578
movsd %xmm0, -0x100(%rbp)
jmp 0x1f0e3
movsd -0xf8(%rbp), %xmm1
movsd -0x100(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1f10b
movsd 0x1a317(%rip), %xmm0    # 0x39418
movsd %xmm0, -0x108(%rbp)
jmp 0x1f159
cmpq $0x0, -0x60(%rbp)
jle 0x1f137
movsd -0x80(%rbp), %xmm0
movl -0x4c(%rbp), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
jmp 0x1f149
movsd 0x13439(%rip), %xmm0    # 0x32578
movsd %xmm0, -0x110(%rbp)
jmp 0x1f149
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movq -0x78(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movsd %xmm0, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq %rax, -0x118(%rbp)
movl -0x48(%rbp), %eax
movl %eax, %edi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x2aa0
movl %eax, %ecx
movq -0x118(%rbp), %rax
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x1f1ad
jmp 0x1f1cc
leaq 0x1355d(%rip), %rdi     # 0x32711
leaq 0x13579(%rip), %rsi     # 0x32734
movl $0x1da, %edx            # imm = 0x1DA
leaq 0x13573(%rip), %rcx     # 0x3273a
callq 0x21c0
jmp 0x1f1ce
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1ebce
movl $0x18, %edi
callq 0x2340
movq %rax, -0x88(%rbp)
movq -0x58(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movl $0x3e8, 0x8(%rax)       # imm = 0x3E8
movq -0x78(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x88(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)