// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncasm.c
// Function: scg_ra_utg_coverage
void scg_ra_utg_coverage(scg_t *g, sr_db_t *sr_db, scg_ra_v *ra_v, int verbose)
{
    if (ra_v->n == 0) {
        fprintf(stderr, "[W::%s] no read alignment, unitig coverage estimation skipped\n", __func__);
        return;
    }

    uint64_t i, j, k, s, n, m, n_vtx, n_scm, m_scm, uid, sid, *a;
    asmg_t *utg_g;
    syncmer_t *scm;

    utg_g = g->utg_asmg;
    n_vtx = utg_g->n_vtx;
    scm = scg_a_scm(g);
    n_scm = scg_n_scm(g);

    /***
    // first round estimation with single copy syncmers
    double *avg_covs;
    kvec_t(double) kcovs;
    
    // FIXME utgs with no single copy mers
    MYCALLOC(avg_covs, n_vtx);
    kv_init(kcovs);
    for (i = 0; i < n_vtx; ++i) {
        a = utg_g->vtx[i].a;
        n = utg_g->vtx[i].n;
        for (j = 0; j < n; ++j) {
            s = a[j] >> 1;
            if (scm_utg_n(g, s) == 1)
                kv_push(double, kcovs, (double) scm[s].cov);
        }
        avg_covs[i] = average_IQR(kcovs.a, kcovs.n, 0);
        avg_covs[i] = MAX(1., avg_covs[i]);
        kcovs.n = 0;
    }
    kv_destroy(kcovs);
    **/
    
    // first round estimation with uniquely mapped reads
    double *avg_covs, *covs, **C, intpart;
    ra_frg_t *ra;
    m_scm = 0;
    for (i = 0; i < n_vtx; ++i)
        m_scm += utg_g->vtx[i].n;
    MYCALLOC(covs, m_scm);
    MYMALLOC(C, n_vtx);
    C[0] = covs;
    for (i = 1; i < n_vtx; ++i)
        C[i] = C[i - 1] + utg_g->vtx[i - 1].n;
    for (i = 0, n = ra_v->n; i < n; ++i) {
        if (modf(ra_v->a[i].s, &intpart) > DBL_EPSILON)
            continue;
        ra = ra_v->a[i].a;
        m = ra_v->a[i].n;
        for (j = 0; j < m; ++j) {
            uid = ra[j].uid >> 1;
            for (k = ra[j].u_beg, s = ra[j].u_end; k <= s; ++k)
                C[uid][k] += 1.;
        }
    }
    MYCALLOC(avg_covs, n_vtx);
    for (i = 0; i < n_vtx; ++i) {
        n = utg_g->vtx[i].n;
        qsort(C[i], n, sizeof(double), dbl_cmpfunc);
        // do not use syncmers not covered
        m = 0;
        while (m < n && C[i][m] < DBL_EPSILON) ++m;
        avg_covs[i] = average_IQR(&C[i][m], n - m, 1);
        avg_covs[i] = MAX(1., avg_covs[i]);
    }
    free(C);
    free(covs);

#ifdef DEBUG_UTG_RA_COV
    fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] unitig coverage estimation first round -\n", __func__);
    for (i = 0; i < n_vtx; ++i)
        fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] u%lu: %u\n", __func__, i, (uint32_t) avg_covs[i]);
#endif

    // find alignment blocks for multiple alignments
    // here assume alignment records were sorted by reads
    kvec_t(ma_t) mas = {0, 0, 0};
    ma_t *ma;
    sid = ra_v->a[0].sid;
    for (i = j = 0; i < ra_v->n; ++i) {
        if (sid != ra_v->a[i].sid) {
            // new read
            kv_pushp(ma_t, mas, &ma);
            make_ma_block(g, &sr_db->a[ra_v->a[j].sid], &ra_v->a[j], i - j, ma);
            j = i;
            sid = ra_v->a[j].sid;
        }
    }
    kv_pushp(ma_t, mas, &ma);
    make_ma_block(g, &sr_db->a[ra_v->a[j].sid], &ra_v->a[j], i - j, ma);

    // second round estimation with mapped reads
    double diff, covt;
    // this is an interface for better EM algorithm
    // currently it converges quickly
    MYMALLOC(covs, n_vtx);
    for (i = 0; i < EM_MAX_ITER; ++i) {
        MYBZERO(covs, n_vtx);
        for (j = 0; j < mas.n; ++j) {
            ma = &mas.a[j];
            for (k = 0; k < ma->b; ++k) {
                a = &ma->u[k * (ma->a)];
                covt = 0.;
                for (n = 0; n < ma->a; ++n)
                    covt += avg_covs[a[n]];
                if (covt == 0.) continue;
                for (n = 0; n < ma->a; ++n)
                    covs[a[n]] += avg_covs[a[n]] / covt * ma->n[k];
            }
        }
        diff = 0.;
        for (j = 0; j < n_vtx; ++j) {
            covt = covs[j] / utg_g->vtx[j].n;
            diff += fabs(covt - avg_covs[j]);
            avg_covs[j] = covt;
        }
        if (verbose > 2)
            fprintf(stderr, "[M::%s] unitig coverage estimation iteration %lu: diff = %.6f\n",
                    __func__, i, diff);
        if (diff < DBL_EPSILON)
            break;
    }
    if (verbose > 2)
        fprintf(stderr, "[M::%s] unitig coverage estimation ended at iteration %lu\n", __func__, i);
    free(covs);

#ifdef DEBUG_UTG_RA_COV
    fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] unitig coverage estimation second round -\n", __func__);
    for (i = 0; i < n_vtx; ++i)
        fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] u%lu: %u\n", __func__, i, (uint32_t) avg_covs[i]);
#endif

    // third round estimation with all syncmers weighted by utg coverage estimated in last round
    uint128_t **idx_u;
    idx_u = g->idx_u;
    MYCALLOC(covs, m_scm);
    MYMALLOC(C, n_vtx);
    C[0] = covs;
    for (i = 1; i < n_vtx; ++i)
        C[i] = C[i - 1] + utg_g->vtx[i - 1].n;
    for (i = 0; i < n_scm; ++i) {
        m = scm_utg_n(g, i);
        if (m == 0) continue;
        covt = 0.;
        for (j = 0; j < m; ++j)
            covt += avg_covs[scm_utg_uid(idx_u[i][j])];
        if (covt < DBL_EPSILON) continue;
        for (j = 0; j < m; ++j) {
            uid = scm_utg_uid(idx_u[i][j]);
            C[uid][scm_utg_pos(idx_u[i][j])] = avg_covs[uid] / covt * scm[i].cov;
        }
    }
    for (i = 0; i < n_vtx; ++i) {
        n = utg_g->vtx[i].n;
        avg_covs[i] = average_IQR(C[i], n, 0);
        avg_covs[i] = MAX(1., avg_covs[i]);
    }
    free(C);
    free(covs);

#ifdef DEBUG_UTG_RA_COV
    fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] unitig coverage estimation third round -\n", __func__);
    for (i = 0; i < n_vtx; ++i)
        fprintf(stderr, "[DEBUG_UTG_RA_COV::%s] u%lu: %u\n", __func__, i, (uint32_t) avg_covs[i]);
#endif

    // update unitig coverage on graph
    for (i = 0; i < n_vtx; ++i)
        utg_g->vtx[i].cov = (uint32_t) avg_covs[i]; 

    for (i = 0; i < mas.n; ++i) {
        free(mas.a[i].n);
        free(mas.a[i].u);
    }
    kv_destroy(mas);

    free(avg_covs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq (%rdx), %rbp
testq %rbp, %rbp
je 0xb185
movq %rdx, %r15
movq (%rdi), %rax
movq 0x8(%rdi), %rbx
movq (%rbx), %rdx
testq %rdx, %rdx
movq %rdx, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
je 0xaa2f
movq 0x10(%rbx), %rcx
xorl %edi, %edi
addq (%rcx), %rdi
addq $0x28, %rcx
decq %rdx
jne 0xaa21
jmp 0xaa31
xorl %edi, %edi
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x58(%rsp)
movl $0x8, %esi
movq %rdi, 0x50(%rsp)
callq 0x2250
movq %rax, %r12
movq 0x8(%rsp), %r14
leaq (,%r14,8), %rdi
movq %rdi, 0x18(%rsp)
callq 0x2310
movq %rax, %r13
movq %r12, (%rax)
cmpq $0x2, %r14
jb 0xaa9e
movq %r14, %rdi
movq 0x10(%rbx), %rax
movl $0x1, %ecx
movq %r12, %rdx
movq (%rax), %rsi
leaq (%rdx,%rsi,8), %rdx
movq %rdx, (%r13,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %rdi
jne 0xaa86
movq %r12, 0x48(%rsp)
movq %rbx, 0x20(%rsp)
movq %r15, 0x10(%rsp)
movq 0x10(%r15), %rbx
xorl %r15d, %r15d
leaq 0x60(%rsp), %r14
movq %r15, %r12
shlq $0x5, %r12
movsd 0x18(%rbx,%r12), %xmm0
movq %r14, %rdi
callq 0x21a0
ucomisd 0x14d69(%rip), %xmm0    # 0x1f840
movsd 0x14d69(%rip), %xmm1    # 0x1f848
ja 0xab37
movl 0x8(%rbx,%r12), %eax
testq %rax, %rax
je 0xab37
movq 0x10(%rbx,%r12), %rcx
xorl %edx, %edx
movq %rdx, %r8
shlq $0x5, %r8
movq 0x8(%rcx,%r8), %rsi
movq 0x10(%rcx,%r8), %rdi
cmpq %rdi, %rsi
ja 0xab2f
addq %rcx, %r8
movq (%r8), %r8
andq $-0x2, %r8
movq (%r13,%r8,4), %r8
movsd (%r8,%rsi,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r8,%rsi,8)
incq %rsi
cmpq %rdi, %rsi
jbe 0xab17
incq %rdx
cmpq %rax, %rdx
jne 0xaaf2
incq %r15
cmpq %rbp, %r15
jne 0xaab9
movl $0x8, %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2250
movq %rax, %r14
testq %rbx, %rbx
movq 0x20(%rsp), %r12
je 0xabe9
xorl %ebx, %ebx
movq 0x10(%r12), %rax
leaq (%rbx,%rbx,4), %rcx
movq (%rax,%rcx,8), %rbp
movq (%r13,%rbx,8), %r15
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
leaq 0x627(%rip), %rcx       # 0xb1b3
callq 0x20d0
xorl %eax, %eax
testq %rbp, %rbp
je 0xabb3
movsd 0x14ca0(%rip), %xmm0    # 0x1f840
ucomisd (%r15,%rax,8), %xmm0
jbe 0xabb3
incq %rax
cmpq %rax, %rbp
jne 0xaba0
movq %rbp, %rax
leaq (%r15,%rax,8), %rdi
subl %eax, %ebp
movl %ebp, %esi
movl $0x1, %edx
callq 0xb1d0
movsd 0x14c7b(%rip), %xmm1    # 0x1f848
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbx,8)
incq %rbx
cmpq 0x8(%rsp), %rbx
jne 0xab68
movq 0x10(%rsp), %rax
movq (%rax), %rbp
movq %r13, %rdi
callq 0x2060
movq 0x48(%rsp), %rdi
callq 0x2060
testq %rbp, %rbp
je 0xacf9
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rcx
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %edx, %edx
xorl %ebx, %ebx
xorl %r10d, %r10d
cmpq (%rax,%r12), %rcx
je 0xacc3
cmpq %rdx, %rbx
jne 0xac67
leaq (%rdx,%rdx), %rbp
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rax
leaq (%rax,%rax,2), %rsi
movq %r13, %rdi
movq %r10, %r13
callq 0x2350
movq %r13, %r10
movq %rax, %r13
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
jmp 0xac6a
movq %rdx, %rbp
movq 0x28(%rsp), %rdi
leaq (%rbx,%rbx,2), %rcx
incq %rbx
leaq (,%rcx,8), %r8
addq %r13, %r8
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rsi
movq %r10, %r9
shlq $0x5, %r9
leaq (%rax,%r9), %rdx
movl %r15d, %ecx
subl %r10d, %ecx
imulq $0x58, (%rax,%r9), %rax
movq 0x50(%rsi,%rax), %rsi
callq 0xb2b2
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x10(%rax), %rax
movq (%rax,%r12), %rcx
movq %r15, %r10
movq %rbp, %rdx
movq %rsi, %rbp
incq %r15
addq $0x20, %r12
cmpq %rbp, %r15
jb 0xac20
cmpq %rdx, %rbx
jne 0xad1d
movq %r10, %rbp
movq %rbx, %rax
shlq $0x4, %rax
testq %rbx, %rbx
leaq (%rax,%rax,2), %rax
movl $0x30, %esi
cmovneq %rax, %rsi
movq 0x20(%rsp), %r12
jmp 0xad08
movl $0x30, %esi
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x2350
movq %rax, %r13
movq 0x28(%rsp), %rdi
movq %rbp, %r10
jmp 0xad27
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rdi
leaq (%rbx,%rbx,2), %rax
leaq (,%rax,8), %r8
addq %r13, %r8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq %r10, %rsi
shlq $0x5, %rsi
leaq (%rcx,%rsi), %rdx
subl %r10d, %r15d
imulq $0x58, (%rcx,%rsi), %rcx
movq 0x50(%rax,%rcx), %rsi
movl %r15d, %ecx
callq 0xb2b2
movq 0x18(%rsp), %rdi
callq 0x2310
movq %rax, %rbp
xorl %r15d, %r15d
movq %rbp, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x21e0
cmpq $-0x1, %rbx
xorpd %xmm3, %xmm3
je 0xae45
xorl %eax, %eax
leaq (%rax,%rax,2), %rdx
movl 0x4(%r13,%rdx,8), %ecx
testq %rcx, %rcx
je 0xae35
leaq (,%rdx,8), %rdx
addq %r13, %rdx
movq 0x10(%rdx), %rsi
movl (%rdx), %edi
leaq (,%rdi,8), %r8
xorl %r9d, %r9d
testq %rdi, %rdi
je 0xae2a
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
movq (%rsi,%r10,8), %r11
addsd (%r14,%r11,8), %xmm0
incq %r10
cmpq %r10, %rdi
jne 0xadd1
ucomisd %xmm3, %xmm0
jne 0xadeb
jnp 0xae2a
testl %edi, %edi
je 0xae2a
movq 0x8(%rdx), %r10
movl (%r10,%r9,4), %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
xorl %r10d, %r10d
movq (%rsi,%r10,8), %r11
movsd (%r14,%r11,8), %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd (%rbp,%r11,8), %xmm2
movsd %xmm2, (%rbp,%r11,8)
incq %r10
cmpq %r10, %rdi
jne 0xae02
incq %r9
addq %r8, %rsi
cmpq %rcx, %r9
jne 0xadc5
leaq 0x1(%rax), %rcx
cmpq %rbx, %rax
movq %rcx, %rax
jne 0xad97
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xaec1
movq 0x10(%r12), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd 0x149de(%rip), %xmm3    # 0x1f840
movsd 0x14996(%rip), %xmm4    # 0x1f800
movapd 0x1499e(%rip), %xmm5    # 0x1f810
movapd 0x149b6(%rip), %xmm6    # 0x1f830
movsd (%rbp,%rcx,8), %xmm7
movsd (%rax), %xmm1
unpcklps %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm5, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm7
movapd %xmm7, %xmm1
subsd (%r14,%rcx,8), %xmm1
andpd %xmm6, %xmm1
addsd %xmm1, %xmm0
movsd %xmm7, (%r14,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %rdx
jne 0xae7a
jmp 0xaecd
xorpd %xmm0, %xmm0
movsd 0x14973(%rip), %xmm3    # 0x1f840
cmpl $0x3, 0x30(%rsp)
jge 0xaeec
ucomisd %xmm0, %xmm3
ja 0xaf2a
incq %r15
cmpq $0x3e8, %r15            # imm = 0x3E8
jne 0xad78
jmp 0xaf24
movq 0x1b0fd(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
leaq 0x14cd9(%rip), %rsi     # 0x1fbd6
leaq 0x14cbe(%rip), %rdx     # 0x1fbc2
movq %r15, %rcx
movb $0x1, %al
movsd %xmm0, 0x10(%rsp)
callq 0x2280
movsd 0x10(%rsp), %xmm0
movsd 0x1491e(%rip), %xmm3    # 0x1f840
jmp 0xaed4
movl $0x3e8, %r15d           # imm = 0x3E8
cmpl $0x3, 0x30(%rsp)
jge 0xb18c
movq %rbp, %rdi
callq 0x2060
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
movl $0x8, %esi
movq 0x50(%rsp), %rdi
callq 0x2250
movq %rax, %r15
movq 0x18(%rsp), %rdi
callq 0x2310
movq %rax, %r12
movq %r15, (%rax)
movq 0x8(%rsp), %rdi
cmpq $0x2, %rdi
jb 0xafa0
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movl $0x1, %ecx
movq %r15, %rdx
movq (%rax), %rsi
leaq (%rdx,%rsi,8), %rdx
movq %rdx, (%r12,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %rdi
jne 0xaf89
movq %r15, 0x18(%rsp)
cmpq $0x0, 0x40(%rsp)
movsd 0x1488d(%rip), %xmm3    # 0x1f840
je 0xb08f
movabsq $0x3ffffffffff, %rax    # imm = 0x3FFFFFFFFFF
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movabsq $0xfffffffff, %rdi      # imm = 0xFFFFFFFFF
movq %rcx, %r8
movq 0x30(%rsp), %rcx
movq 0x8(%rcx,%rdx,8), %rcx
movq %rcx, %r9
subq %r8, %r9
je 0xb081
sarq $0x4, %r9
cmpq $0x1, %r9
adcq $0x0, %r9
xorpd %xmm0, %xmm0
movq %r8, %r10
movq %r9, %r11
movq (%r10), %r15
movq 0x8(%r10), %rbp
shldq $0x1c, %r15, %rbp
andq %rax, %rbp
addsd (%r14,%rbp,8), %xmm0
addq $0x10, %r10
decq %r11
jne 0xb00b
ucomisd %xmm0, %xmm3
ja 0xb081
movq %rdx, %r10
shlq $0x5, %r10
movq 0x58(%rsp), %r11
addq %r11, %r10
addq $0x10, %r10
movq (%r8), %r11
movq 0x8(%r8), %r15
shldq $0x1c, %r11, %r15
andq %rax, %r15
movsd (%r14,%r15,8), %xmm1
divsd %xmm0, %xmm1
movl (%r10), %ebp
andl %esi, %ebp
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
mulsd %xmm1, %xmm2
movq (%r12,%r15,8), %r15
andq %rdi, %r11
movsd %xmm2, (%r15,%r11,8)
addq $0x10, %r8
decq %r9
jne 0xb042
incq %rdx
cmpq 0x40(%rsp), %rdx
jne 0xafdc
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0xb12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%rbp), %esi
movq (%r12,%r15,8), %rdi
xorl %edx, %edx
callq 0xb1d0
movsd 0x14787(%rip), %xmm1    # 0x1f848
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r15,8)
incq %r15
addq $0x28, %rbp
cmpq %r15, 0x8(%rsp)
jne 0xb0a2
movq %r12, %rdi
callq 0x2060
movq 0x18(%rsp), %rdi
callq 0x2060
movl $0x4, %eax
xorl %ecx, %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r10
cvttsd2si (%r14,%rcx,8), %rsi
movq 0x10(%r9), %rdi
andl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl (%rdi,%rax,8), %r8d
andl %edx, %r8d
orl %esi, %r8d
movl %r8d, (%rdi,%rax,8)
incq %rcx
addq $0x5, %rax
cmpq %rcx, %r10
jne 0xb101
jmp 0xb13f
movq %r12, %rdi
callq 0x2060
movq 0x18(%rsp), %rdi
callq 0x2060
cmpq $-0x1, %rbx
je 0xb166
incq %rbx
leaq 0x10(%r13), %r15
movq -0x8(%r15), %rdi
callq 0x2060
movq (%r15), %rdi
callq 0x2060
addq $0x18, %r15
decq %rbx
jne 0xb14c
movq %r13, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x2060
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2853
jmp 0xb176
movq 0x1ae5d(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
leaq 0x14a78(%rip), %rsi     # 0x1fc15
leaq 0x14a1e(%rip), %rdx     # 0x1fbc2
movq %r15, %rcx
xorl %eax, %eax
callq 0x2280
jmp 0xaf35