// File: /workspace/github_c_makefile_star2_O1/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 0xb260
movq %rax, %r9
cmpl $0x0, (%rbx)
movq %rax, (%rsp)
je 0x30c1
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 0x3088
movq 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx), %rbx
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %r15d, %eax
movl 0x4(%rbx,%r13,8), %r15d
movl %r15d, %edx
subl %eax, %edx
jbe 0x307c
movq (%rsp), %rdi
movl %ebp, %esi
callq 0x16c69
movq (%rsp), %r9
addl (%rbx,%r13,8), %ebp
incq %r13
cmpq %r13, %r14
jne 0x305e
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rax,%rcx), %edx
subl %r15d, %edx
jbe 0x30ae
movq (%rsp), %rdi
movl %ebp, %esi
callq 0x16c69
movq (%rsp), %r9
incq %r12
movq 0x20(%rsp), %rbx
movl (%rbx), %eax
cmpq %rax, %r12
jb 0x302b
xorl %r14d, %r14d
cmpl $0x0, (%r9)
movl $0x0, %edi
movl $0x0, %r13d
movl $0x0, %r8d
movl $0x0, %ebx
je 0x3187
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %eax, %eax
xorl %edi, %edi
movq 0x10(%r9), %rcx
movl %ebp, %edx
shrl $0x4, %edx
movl (%rcx,%rdx,4), %edx
movl %r14d, %ecx
andb $0x1e, %cl
shrl %cl, %edx
testb $0x3, %dl
jne 0x3178
movl %ebp, %ecx
movq 0x18(%r9), %rdx
movq 0x20(%r9), %rsi
movq (%rdx,%rcx,8), %r12
movq (%rsi,%rcx,8), %r15
cmpq %rax, %r13
jne 0x315a
movq %rbx, 0x8(%rsp)
leaq (%rax,%rax), %rbx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movq %r8, 0x10(%rsp)
callq 0x2390
movq 0x10(%rsp), %r8
movq (%rsp), %r9
movq %rax, %rdi
movq %rbx, %rax
movq 0x8(%rsp), %rbx
leaq 0x1(%r13), %rcx
shlq $0x4, %r13
movq %r12, (%rdi,%r13)
movq %r15, 0x8(%rdi,%r13)
addq %r15, %r8
imulq %r12, %r15
addq %r15, %rbx
movq %rcx, %r13
incl %ebp
addl $0x2, %r14d
cmpl (%r9), %ebp
jne 0x30f2
leaq 0x13c62(%rip), %rcx     # 0x16df0
pushq $0x10
popq %rdx
movq %r13, %rsi
movq %r8, %r12
movq %rdi, %r15
callq 0x20e0
movq %r15, %r8
cvtsi2sd %r12, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rbp
movq %rbx, %r14
testq %r13, %r13
je 0x31f4
leaq 0x8(%r8), %rcx
xorl %eax, %eax
movq %r13, %rdx
movq %rbx, %r14
movq -0x8(%rcx), %rsi
movq (%rcx), %rdi
addq %rdi, %rax
cmpq %rbp, %rax
jge 0x31e7
imulq %rdi, %rsi
subq %rsi, %r14
addq $0x10, %rcx
decq %rdx
jne 0x31c6
jmp 0x31f4
addq %rbp, %rdi
subq %rdi, %rax
imulq %rsi, %rax
addq %rax, %r14
movq %rbx, 0x8(%rsp)
testq %r13, %r13
jne 0x3204
movq (%rsp), %rbx
jmp 0x324a
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%r8), %rcx
addq $-0x8, %rcx
xorl %eax, %eax
movq (%rsp), %rbx
movq -0x8(%rcx), %rdx
movq (%rcx), %rsi
addq %rsi, %rax
cmpq %rbp, %rax
jge 0x323d
decq %r13
imulq %rsi, %rdx
subq %rdx, %r14
addq $-0x10, %rcx
testq %r13, %r13
jne 0x3219
jmp 0x324a
addq %rbp, %rsi
subq %rsi, %rax
imulq %rdx, %rax
addq %rax, %r14
addq %rbp, %rbp
movq 0x18(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x10(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
movq 0x20(%rbx), %rsi
xorl %edi, %edi
callq 0xb302
xorl %edi, %edi
movq %rbx, %rsi
callq 0xb302
movq %r15, %rdi
callq 0x2060
movq %r12, %rax
xorpd %xmm1, %xmm1
subq %rbp, %rax
je 0x329a
cvtsi2sd %r14, %xmm1
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq 0x27d52(%rip), %r14     # 0x2aff8
movq (%r14), %rdi
leaq 0x1f4c1(%rip), %rsi     # 0x22771
leaq 0x1f4dc(%rip), %rbx     # 0x22793
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x1f4d5(%rip), %rsi     # 0x227a0
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x1f4db(%rip), %rsi     # 0x227bd
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x1f4db(%rip), %rsi     # 0x227d6
movq %rbx, %rdx
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x22c0
movq (%r14), %rdi
leaq 0x1f4d7(%rip), %rsi     # 0x227ec
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