// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
movq %rdi, %rbx
xorl %r14d, %r14d
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
xorl %edi, %edi
callq 0x83c0
movq %rax, %r9
movq %rax, %r13
movq %rbx, 0x20(%rsp)
movl (%rbx), %eax
cmpq %rax, %r14
jae 0x12eb1
movq 0x8(%rbx), %rax
movq %r14, (%rsp)
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %r14
movq %rcx, 0x10(%rsp)
movq 0x10(%rax,%rcx), %r15
xorl %eax, %eax
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x12e7d
movq (%r15,%r12,8), %rcx
movq %rcx, %rbx
shrq $0x20, %rbx
movl %ebx, %edx
subl %eax, %edx
jbe 0x12e74
movq %r13, %rdi
movl %ebp, %esi
callq 0x13101
movq %r13, %r9
movq (%r15,%r12,8), %rcx
addl %ecx, %ebp
incq %r12
movl %ebx, %eax
jmp 0x12e4d
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x10(%rsp), %rdx
movl 0x10(%rcx,%rdx), %edx
subl %eax, %edx
jbe 0x12ea0
movq %r13, %rdi
movl %ebp, %esi
callq 0x13101
movq %r13, %r9
movq (%rsp), %r14
incq %r14
movq 0x20(%rsp), %rbx
jmp 0x12e21
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpl (%r9), %ebp
je 0x12f64
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 0x12f59
movl %ebp, %ecx
movq 0x18(%r9), %rdx
movq 0x20(%r9), %rsi
movq (%rdx,%rcx,8), %rdx
movq (%rsi,%rcx,8), %r15
cmpq %rax, %r12
jne 0x12f3a
movq %rbx, (%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)
movq %rdx, 0x8(%rsp)
callq 0x2390
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %r8
movq %r13, %r9
movq %rax, %rdi
movq %rbx, %rax
movq (%rsp), %rbx
leaq 0x1(%r12), %rcx
shlq $0x4, %r12
movq %rdx, (%rdi,%r12)
movq %r15, 0x8(%rdi,%r12)
addq %r15, %r8
imulq %rdx, %r15
addq %r15, %rbx
movq %rcx, %r12
incl %ebp
addl $0x2, %r14d
jmp 0x12ec2
leaq 0x325(%rip), %rcx       # 0x13290
pushq $0x10
popq %rdx
movq %r12, %rsi
movq %r8, %r15
movq %rdi, (%rsp)
callq 0x20e0
movq (%rsp), %r8
cvtsi2sd %r15, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rbp
leaq 0x8(%r8), %rcx
xorl %eax, %eax
movq %r12, %rdx
movq %rbx, %r14
subq $0x1, %rdx
jb 0x12fcc
movq -0x8(%rcx), %rsi
movq (%rcx), %rdi
addq %rdi, %rax
cmpq %rbp, %rax
jge 0x12fbf
imulq %rdi, %rsi
subq %rsi, %r14
addq $0x10, %rcx
jmp 0x12f9d
addq %rbp, %rdi
subq %rdi, %rax
imulq %rsi, %rax
addq %rax, %r14
movq %r12, %rax
shlq $0x4, %rax
leaq (%r8,%rax), %rcx
addq $-0x8, %rcx
xorl %eax, %eax
subq $0x1, %r12
jb 0x12fff
movq -0x8(%rcx), %rdx
movq (%rcx), %rsi
addq %rsi, %rax
cmpq %rbp, %rax
jge 0x13004
imulq %rsi, %rdx
subq %rdx, %r14
addq $-0x10, %rcx
jmp 0x12fdd
movq %rbx, %r12
jmp 0x13014
movq %rbx, %r12
addq %rbp, %rsi
subq %rsi, %rax
imulq %rdx, %rax
addq %rax, %r14
addq %rbp, %rbp
movq 0x18(%r13), %rsi
xorl %edi, %edi
callq 0x8447
movq 0x10(%r13), %rsi
xorl %edi, %edi
callq 0x8447
movq 0x20(%r13), %rsi
xorl %edi, %edi
callq 0x8447
xorl %edi, %edi
movq %r13, %rsi
callq 0x8447
movq (%rsp), %rdi
callq 0x2060
movq %r15, %rax
xorpd %xmm1, %xmm1
subq %rbp, %rax
je 0x13065
cvtsi2sd %r14, %xmm1
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq 0x14f87(%rip), %r14     # 0x27ff8
movq (%r14), %rdi
leaq 0xc5ee(%rip), %rsi      # 0x1f669
leaq 0xc609(%rip), %rbx      # 0x1f68b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0xc602(%rip), %rsi      # 0x1f698
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0xc608(%rip), %rsi      # 0x1f6b5
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0xc60a(%rip), %rsi      # 0x1f6ce
movq %rbx, %rdx
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x22c0
movq (%r14), %rdi
leaq 0xc606(%rip), %rsi      # 0x1f6e4
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