// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/cov.c
// Function: calc_avg_cov
double calc_avg_cov(cov_t *cov, sdict_t *sdict, double q_drop)
{
    size_t i, j, s;
    int32_t c;
    uint32_t p, p1;
    uint64_t *a;
    kh_ctab_t *ctab;
    khint_t k;

    // get base coverage table
    ctab = kh_init_ctab();
    for (i = 0; i < cov->n; ++i) {
        a = cov->p[i].a;
        s = cov->p[i].n;
        
        p = 0, c = 0;
        for (j = 0; j < s; ++j) {
            p1 = (uint32_t) (a[j] >> 32);
            if (p1 > p)
                kh_ctab_put1(ctab, c, p1 - p);
            p = p1;
            c += (int32_t) a[j];
        }

        if (p < sdict->s[i].len)
            kh_ctab_put1(ctab, c, sdict->s[i].len - p);
    }

    kvec_t(ctab_pair) cnts;
    int64_t bases, covs, qbases, qcovs, b, qb;

    bases = covs = 0;
    kv_init(cnts);
    for (k = kh_begin(ctab); k != kh_end(ctab); ++k) {
        if (kh_exist(ctab, k)) {
            ctab_pair cnt = {kh_key(ctab, k), kh_val(ctab, k)};
            kv_push(ctab_pair, cnts, cnt);
            bases += cnt.y;
            covs += cnt.x * cnt.y;
        }
    }
    qsort(cnts.a, cnts.n, sizeof(ctab_pair), ctab_cmpfunc);
    
    qb = bases * q_drop;
    qbases = bases - qb * 2;
    qcovs = covs;
    for (i = 0, b = 0; i < cnts.n; ++i) {
        b += cnts.a[i].y;
        if (b < qb) {
            qcovs -= cnts.a[i].x * cnts.a[i].y;
        } else {
            qcovs -= cnts.a[i].x * (qb - b + cnts.a[i].y);
            break;
        }
    }
    for (i = cnts.n, b = 0; i != 0 ; --i) {
        b += cnts.a[i-1].y;
        if (b < qb) {
            qcovs -= cnts.a[i-1].x * cnts.a[i-1].y;
        } else {
            qcovs -= cnts.a[i-1].x * (qb - b + cnts.a[i-1].y);
            break;
        }
    }
    
    kh_destroy(ctab, ctab);
    kv_destroy(cnts);

    double avg_cov;
    avg_cov = qbases == 0? .0 : (double) qcovs / qbases;
    
    fprintf(stderr, "[I::%s] sequence coverage stats:\n", __func__);
    fprintf(stderr, "[I::%s] sequence bases: %ld\n", __func__, bases);
    fprintf(stderr, "[I::%s] read bases: %ld\n", __func__, covs);
    fprintf(stderr, "[I::%s] q drop: %.3f\n", __func__, q_drop);
    fprintf(stderr, "[I::%s] average read coverage: %.3f\n", __func__, avg_cov);

    return avg_cov;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
xorl %edi, %edi
callq 0xbf84
movq %rax, %r8
cmpl $0x0, (%rbx)
movq %rax, (%rsp)
je 0x307f
xorl %r12d, %r12d
movq %rbx, 0x20(%rsp)
movq 0x8(%rbx), %rax
imulq $0x18, %r12, %rcx
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx), %r14
movl $0x0, %r15d
movl $0x0, %ebp
testq %r14, %r14
je 0x3046
movq 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx), %rbx
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %r15d, %ecx
movq (%rbx,%r13,8), %rax
movq %rax, %r15
shrq $0x20, %r15
movl %r15d, %edx
subl %ecx, %edx
jbe 0x303c
movq (%rsp), %rdi
movl %ebp, %esi
callq 0x17b37
movq (%rsp), %r8
movq (%rbx,%r13,8), %rax
addl %eax, %ebp
incq %r13
cmpq %r13, %r14
jne 0x3014
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rax,%rcx), %edx
subl %r15d, %edx
jbe 0x306c
movq (%rsp), %rdi
movl %ebp, %esi
callq 0x17b37
movq (%rsp), %r8
incq %r12
movq 0x20(%rsp), %rbx
movl (%rbx), %eax
cmpq %rax, %r12
jb 0x2fe1
movl (%r8), %eax
xorl %ebp, %ebp
movl $0x0, %edi
movl $0x0, %r13d
movl $0x0, %r9d
movl $0x0, %r10d
testl %eax, %eax
je 0x314e
xorl %r14d, %r14d
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %edi, %edi
movq 0x10(%r8), %rcx
movl %r14d, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
movl %ebp, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x313f
movl %r14d, %ecx
movq 0x18(%r8), %rdx
movq 0x20(%r8), %rsi
movq (%rdx,%rcx,8), %r15
movq (%rsi,%rcx,8), %rbx
cmpq %r12, %r13
jne 0x3121
leaq (,%r13,2), %r12
testq %r13, %r13
pushq $0x2
popq %rax
cmoveq %rax, %r12
movq %r12, %rsi
shlq $0x4, %rsi
movq %r10, 0x8(%rsp)
movq %r9, 0x10(%rsp)
callq 0x2390
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
movq (%rsp), %r8
movq %rax, %rdi
movl (%r8), %eax
leaq 0x1(%r13), %rcx
shlq $0x4, %r13
movq %r15, (%rdi,%r13)
movq %rbx, 0x8(%rdi,%r13)
addq %rbx, %r9
imulq %r15, %rbx
addq %rbx, %r10
movq %rcx, %r13
incl %r14d
addl $0x2, %ebp
cmpl %eax, %r14d
jne 0x30b4
movq %r10, 0x8(%rsp)
leaq 0x14b72(%rip), %rcx     # 0x17ccc
pushq $0x10
popq %rdx
movq %rdi, %r15
movq %r13, %rsi
movq %r9, %rbx
callq 0x20e0
movq 0x8(%rsp), %rsi
movq %rbx, %r12
cvtsi2sd %rbx, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rbp
movq %rsi, %r14
testq %r13, %r13
je 0x3210
leaq 0x8(%r15), %rcx
xorl %eax, %eax
movq %r13, %rdx
movq %rsi, %r14
movq -0x8(%rcx), %rsi
movq (%rcx), %rdi
addq %rdi, %rax
cmpq %rbp, %rax
jge 0x31bc
imulq %rdi, %rsi
subq %rsi, %r14
addq $0x10, %rcx
decq %rdx
jne 0x319b
jmp 0x31ce
addq %rbp, %rdi
subq %rdi, %rax
imulq %rsi, %rax
addq %rax, %r14
testq %r13, %r13
je 0x3210
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%r15), %rcx
addq $-0x8, %rcx
xorl %eax, %eax
movq -0x8(%rcx), %rdx
movq (%rcx), %rsi
addq %rsi, %rax
cmpq %rbp, %rax
jge 0x3203
decq %r13
imulq %rsi, %rdx
subq %rdx, %r14
addq $-0x10, %rcx
testq %r13, %r13
jne 0x31df
jmp 0x3210
addq %rbp, %rsi
subq %rsi, %rax
imulq %rdx, %rax
addq %rax, %r14
addq %rbp, %rbp
movq (%rsp), %rbx
movq 0x18(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x10(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
movq 0x20(%rbx), %rsi
xorl %edi, %edi
callq 0xc01f
xorl %edi, %edi
movq %rbx, %rsi
callq 0xc01f
movq %r15, %rdi
callq 0x2060
movq %r12, %rax
xorpd %xmm1, %xmm1
subq %rbp, %rax
je 0x3264
cvtsi2sd %r14, %xmm1
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq 0x28d88(%rip), %r14     # 0x2bff8
movq (%r14), %rdi
leaq 0x20527(%rip), %rsi     # 0x237a1
leaq 0x20542(%rip), %rbx     # 0x237c3
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x2053b(%rip), %rsi     # 0x237d0
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x20541(%rip), %rsi     # 0x237ed
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x20541(%rip), %rsi     # 0x23806
movq %rbx, %rdx
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x22c0
movq (%r14), %rdi
leaq 0x2053d(%rip), %rsi     # 0x2381c
movq %rbx, %rdx
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x22c0
movsd (%rsp), %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq