// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
callq 0x1e960
movq %rax, -0x50(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jae 0x1e4cb
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x1e47e
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jbe 0x1e458
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x3c(%rbp), %edx
subl -0x38(%rbp), %edx
callq 0x1e980
movl -0x3c(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1e421
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
imulq $0x18, -0x20(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jae 0x1e4b8
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x20(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %edx
subl -0x38(%rbp), %edx
callq 0x1e980
jmp 0x1e4ba
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1e3c9
movq $0x0, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x1e612
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0x54(%rbp), %ecx
andl $0xf, %ecx
shll %ecx
shrl %cl, %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x1e602
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x1e5ad
cmpq $0x0, -0x68(%rbp)
je 0x1e57f
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0xc0(%rbp)
jmp 0x1e58d
movl $0x2, %eax
movq %rax, -0xc0(%rbp)
jmp 0x1e58d
movq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq $0x4, %rsi
callq 0x23a0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x70(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq -0xb0(%rbp), %rcx
movq %rcx, (%rax)
movq -0xa8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0xa8(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xb0(%rbp), %rax
imulq -0xa8(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x1e604
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1e4fa
movq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl $0x10, %edx
leaq 0x3ba(%rip), %rcx       # 0x1e9e0
callq 0x20e0
cvtsi2sdq -0x78(%rbp), %xmm0
mulsd -0x18(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
movq -0xa0(%rbp), %rcx
shlq %rcx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x1e74d
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jge 0x1e6f0
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
imulq 0x8(%rax), %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0x1e73a
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0xa0(%rbp), %rax
subq -0x98(%rbp), %rax
movq -0x60(%rbp), %rdx
movq -0x20(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
addq 0x8(%rdx), %rax
imulq %rax, %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0x1e74d
jmp 0x1e73c
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1e678
movq -0x70(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x98(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1e846
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jge 0x1e7e1
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rdx
subq $0x1, %rdx
shlq $0x4, %rdx
addq %rdx, %rax
imulq 0x8(%rax), %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0x1e833
movq -0x60(%rbp), %rax
movq -0x20(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0xa0(%rbp), %rax
subq -0x98(%rbp), %rax
movq -0x60(%rbp), %rdx
movq -0x20(%rbp), %rsi
subq $0x1, %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
addq 0x8(%rdx), %rax
imulq %rax, %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0x1e846
jmp 0x1e835
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1e760
movq -0x50(%rbp), %rdi
callq 0x1ea30
movq -0x60(%rbp), %rdi
callq 0x2050
cmpq $0x0, -0x88(%rbp)
jne 0x1e86f
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
jmp 0x1e88d
cvtsi2sdq -0x90(%rbp), %xmm0
cvtsi2sdq -0x88(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq 0x1a754(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x13dc3(%rip), %rsi     # 0x32671
leaq 0x13dde(%rip), %rdx     # 0x32693
movb $0x0, %al
callq 0x22c0
movq 0x1a735(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x78(%rbp), %rcx
leaq 0x13dcf(%rip), %rsi     # 0x326a0
leaq 0x13dbb(%rip), %rdx     # 0x32693
movb $0x0, %al
callq 0x22c0
movq 0x1a712(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x80(%rbp), %rcx
leaq 0x13dc9(%rip), %rsi     # 0x326bd
leaq 0x13d98(%rip), %rdx     # 0x32693
movb $0x0, %al
callq 0x22c0
movq 0x1a6ef(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movsd -0x18(%rbp), %xmm0
leaq 0x13dbe(%rip), %rsi     # 0x326d6
leaq 0x13d74(%rip), %rdx     # 0x32693
movb $0x1, %al
callq 0x22c0
movq 0x1a6cb(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movsd -0xb8(%rbp), %xmm0
leaq 0x13dad(%rip), %rsi     # 0x326ec
leaq 0x13d4d(%rip), %rdx     # 0x32693
movb $0x1, %al
callq 0x22c0
movsd -0xb8(%rbp), %xmm0
addq $0xd0, %rsp
popq %rbp
retq
nop