// File: /workspace/github_c_makefile_star2_O1/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, 0x34(%rsp)
movq %rsi, 0x40(%rsp)
movq (%rdx), %r15
testq %r15, %r15
je 0xa881
movq %rdx, 0x10(%rsp)
movq (%rdi), %rax
movq 0x8(%rdi), %rbx
movq (%rbx), %rdx
testq %rdx, %rdx
movq %rdx, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
je 0xa151
movq 0x10(%rbx), %rcx
xorl %edi, %edi
addq (%rcx), %rdi
addq $0x28, %rcx
decq %rdx
jne 0xa143
jmp 0xa153
xorl %edi, %edi
movq (%rax), %rcx
movq %rcx, 0x48(%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, 0x28(%rsp)
callq 0x2320
movq %rax, %r13
movq %r12, 0x18(%rsp)
movq %r12, (%rax)
cmpq $0x2, %r14
jb 0xa1c6
movq %r14, %rdi
movq 0x10(%rbx), %rax
movq (%r13), %rcx
movl $0x1, %edx
movq (%rax), %rsi
leaq (%rcx,%rsi,8), %rcx
movq %rcx, (%r13,%rdx,8)
incq %rdx
addq $0x28, %rax
cmpq %rdx, %rdi
jne 0xa1ae
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r12
cmpq $0x1, %r15
adcq $0x0, %r15
xorl %r14d, %r14d
movq %r14, %rbp
shlq $0x5, %rbp
movsd 0x18(%r12,%rbp), %xmm0
leaq 0x60(%rsp), %rdi
callq 0x21a0
ucomisd 0x135ce(%rip), %xmm0    # 0x1d7c8
movsd 0x135ce(%rip), %xmm1    # 0x1d7d0
ja 0xa25a
movl 0x8(%r12,%rbp), %eax
testq %rax, %rax
je 0xa25a
movq 0x10(%r12,%rbp), %rcx
xorl %edx, %edx
movq %rdx, %r8
shlq $0x5, %r8
movq 0x8(%rcx,%r8), %rsi
movq 0x10(%rcx,%r8), %rdi
cmpq %rdi, %rsi
ja 0xa252
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 0xa23a
incq %rdx
cmpq %rax, %rdx
jne 0xa215
incq %r14
cmpq %r15, %r14
jne 0xa1da
movl $0x8, %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x2250
movq %rax, %r14
testq %r15, %r15
je 0xa30c
xorl %r12d, %r12d
movq 0x10(%rbx), %rax
leaq (%r12,%r12,4), %rcx
movq (%rax,%rcx,8), %r15
movq (%r13,%r12,8), %rbp
movl $0x8, %edx
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x605(%rip), %rcx       # 0xa8af
callq 0x20d0
xorl %eax, %eax
testq %r15, %r15
je 0xa2d1
movsd 0x1350a(%rip), %xmm0    # 0x1d7c8
ucomisd (%rbp,%rax,8), %xmm0
jbe 0xa2d1
incq %rax
cmpq %rax, %r15
jne 0xa2be
movq %r15, %rax
leaq (,%rax,8), %rdi
addq %rbp, %rdi
subl %eax, %r15d
movl %r15d, %esi
movl $0x1, %edx
callq 0xa8cc
movsd 0x134dc(%rip), %xmm1    # 0x1d7d0
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r12,8)
incq %r12
cmpq 0x8(%rsp), %r12
jne 0xa287
movq %r13, %rdi
callq 0x2060
movq 0x18(%rsp), %rdi
callq 0x2060
movq 0x10(%rsp), %rdx
cmpq $0x0, (%rdx)
movq %rbx, 0x38(%rsp)
je 0xa405
movq 0x10(%rdx), %rax
movq (%rax), %rax
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %esi, %esi
xorl %r12d, %r12d
xorl %r9d, %r9d
movq 0x10(%rdx), %rcx
cmpq (%rcx,%rbp), %rax
je 0xa3f3
cmpq %rsi, %r12
jne 0xa39a
leaq (%rsi,%rsi), %rbx
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
movq %r13, %rdi
movq %r9, %r13
callq 0x2360
movq %r13, %r9
movq 0x10(%rsp), %rdx
movq %rax, %r13
movq %rbx, 0x18(%rsp)
movq 0x38(%rsp), %rbx
jmp 0xa39f
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
leaq (%r12,%r12,2), %rax
incq %r12
leaq (,%rax,8), %r8
addq %r13, %r8
movq 0x10(%rdx), %rax
movq %r9, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdx
imulq $0x58, (%rax,%rcx), %rsi
movq 0x40(%rsp), %rax
addq 0x10(%rax), %rsi
movl %r15d, %ecx
subl %r9d, %ecx
callq 0xa9ba
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %rax
movq (%rax,%rbp), %rax
movq %r15, %r9
movq 0x18(%rsp), %rsi
incq %r15
addq $0x20, %rbp
cmpq (%rdx), %r15
jb 0xa349
jmp 0xa413
xorl %r9d, %r9d
xorl %r12d, %r12d
xorl %esi, %esi
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq %rsi, %r12
jne 0xa445
movq %rsi, %rax
shlq $0x4, %rax
testq %rsi, %rsi
leaq (%rax,%rax,2), %rax
movl $0x30, %esi
cmovneq %rax, %rsi
movq %r13, %rdi
movq %r9, %r13
callq 0x2360
movq %r13, %r9
movq 0x10(%rsp), %rdx
movq %rax, %r13
movq 0x20(%rsp), %rdi
leaq 0x1(%r12), %rax
movq %rax, 0x18(%rsp)
leaq (%r12,%r12,2), %rax
leaq (,%rax,8), %r8
addq %r13, %r8
movq 0x10(%rdx), %rax
movq %r9, %rcx
shlq $0x5, %rcx
imulq $0x58, (%rax,%rcx), %rsi
leaq (%rax,%rcx), %rdx
movq 0x40(%rsp), %rax
addq 0x10(%rax), %rsi
subl %r9d, %r15d
movl %r15d, %ecx
callq 0xa9ba
movq 0x28(%rsp), %rdi
callq 0x2320
movq %rax, %rbp
xorl %r15d, %r15d
movq %rbp, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x21e0
cmpq $0x0, 0x18(%rsp)
xorpd %xmm3, %xmm3
je 0xa56a
xorl %eax, %eax
leaq (%rax,%rax,2), %rdx
movl 0x4(%r13,%rdx,8), %ecx
testq %rcx, %rcx
je 0xa55a
leaq (,%rdx,8), %rdx
addq %r13, %rdx
movq 0x10(%rdx), %rsi
movl (%rdx), %edi
leaq (,%rdi,8), %r8
xorl %r9d, %r9d
xorpd %xmm0, %xmm0
testq %rdi, %rdi
je 0xa508
xorl %r10d, %r10d
movq (%rsi,%r10,8), %r11
addsd (%r14,%r11,8), %xmm0
incq %r10
cmpq %r10, %rdi
jne 0xa4f6
ucomisd %xmm3, %xmm0
jne 0xa510
jnp 0xa54f
testl %edi, %edi
je 0xa54f
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 0xa527
incq %r9
addq %r8, %rsi
cmpq %rcx, %r9
jne 0xa4ea
leaq 0x1(%rax), %rcx
cmpq %r12, %rax
movq %rcx, %rax
jne 0xa4bc
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0xa5e5
movq 0x10(%rbx), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd 0x13242(%rip), %xmm3    # 0x1d7c8
movsd 0x13282(%rip), %xmm4    # 0x1d810
movapd 0x1328a(%rip), %xmm5    # 0x1d820
movapd 0x132a2(%rip), %xmm6    # 0x1d840
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 0xa59e
jmp 0xa5f1
xorpd %xmm0, %xmm0
movsd 0x131d7(%rip), %xmm3    # 0x1d7c8
cmpl $0x3, 0x34(%rsp)
jge 0xa610
ucomisd %xmm0, %xmm3
ja 0xa64e
incq %r15
cmpq $0x3e8, %r15            # imm = 0x3E8
jne 0xa49b
jmp 0xa648
movq 0x199d9(%rip), %rax     # 0x23ff0
movq (%rax), %rdi
leaq 0x1357d(%rip), %rsi     # 0x1db9e
leaq 0x13562(%rip), %rdx     # 0x1db8a
movq %r15, %rcx
movb $0x1, %al
movsd %xmm0, 0x10(%rsp)
callq 0x2290
movsd 0x10(%rsp), %xmm0
movsd 0x13182(%rip), %xmm3    # 0x1d7c8
jmp 0xa5f8
movl $0x3e8, %r15d           # imm = 0x3E8
cmpl $0x3, 0x34(%rsp)
jge 0xa888
movq %rbp, %rdi
callq 0x2060
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rbp
movl $0x8, %esi
movq 0x50(%rsp), %rdi
callq 0x2250
movq %rax, %r15
movq 0x28(%rsp), %rdi
callq 0x2320
movq %rax, %r12
movq %r15, 0x28(%rsp)
movq %r15, (%rax)
movq 0x8(%rsp), %rdi
cmpq $0x2, %rdi
jb 0xa6c0
movq 0x10(%rbx), %rax
movq (%r12), %rcx
movl $0x1, %edx
movq (%rax), %rsi
leaq (%rcx,%rsi,8), %rcx
movq %rcx, (%r12,%rdx,8)
incq %rdx
addq $0x28, %rax
cmpq %rdx, %rdi
jne 0xa6a9
cmpq $0x0, 0x48(%rsp)
movsd 0x130fa(%rip), %xmm3    # 0x1d7c8
je 0xa7a4
movabsq $0x3ffffffffff, %rax    # imm = 0x3FFFFFFFFFF
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movabsq $0xfffffffff, %rsi      # imm = 0xFFFFFFFFF
movq (%rbp,%rcx,8), %rdi
movq 0x8(%rbp,%rcx,8), %r9
movq %r9, %r8
subq %rdi, %r8
je 0xa796
sarq $0x4, %r8
cmpq $0x1, %r8
adcq $0x0, %r8
xorpd %xmm0, %xmm0
movq %rdi, %r10
movq %r8, %r11
movq (%r10), %rbx
movq 0x8(%r10), %r15
shldq $0x1c, %rbx, %r15
andq %rax, %r15
addsd (%r14,%r15,8), %xmm0
addq $0x10, %r10
decq %r11
jne 0xa71b
ucomisd %xmm0, %xmm3
ja 0xa796
cmpq %rdi, %r9
je 0xa796
movq %rcx, %r9
shlq $0x5, %r9
movq 0x58(%rsp), %r10
addq %r10, %r9
addq $0x10, %r9
movq (%rdi), %r10
movq 0x8(%rdi), %r11
shldq $0x1c, %r10, %r11
andq %rax, %r11
movsd (%r14,%r11,8), %xmm1
divsd %xmm0, %xmm1
movl (%r9), %ebx
andl %edx, %ebx
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
mulsd %xmm1, %xmm2
movq (%r12,%r11,8), %r11
andq %rsi, %r10
movsd %xmm2, (%r11,%r10,8)
addq $0x10, %rdi
decq %r8
jne 0xa757
incq %rcx
cmpq 0x48(%rsp), %rcx
jne 0xa6ef
cmpq $0x0, 0x8(%rsp)
movq 0x38(%rsp), %rbx
je 0xa7e8
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
movl (%rax,%rbp), %esi
movq (%r12,%r15,8), %rdi
xorl %edx, %edx
callq 0xa8cc
movsd 0x13000(%rip), %xmm1    # 0x1d7d0
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%r15,8)
incq %r15
addq $0x28, %rbp
cmpq %r15, 0x8(%rsp)
jne 0xa7b6
movq %r12, %rdi
callq 0x2060
movq 0x28(%rsp), %rdi
callq 0x2060
movq 0x8(%rsp), %r9
testq %r9, %r9
je 0xa83a
movl $0x4, %eax
xorl %ecx, %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
cvttsd2si (%r14,%rcx,8), %rsi
movq 0x10(%rbx), %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, %r9
jne 0xa810
movq 0x18(%rsp), %r15
testq %r15, %r15
je 0xa862
leaq 0x10(%r13), %rbx
movq -0x8(%rbx), %rdi
callq 0x2060
movq (%rbx), %rdi
callq 0x2060
addq $0x18, %rbx
decq %r15
jne 0xa848
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 0x2863
jmp 0xa872
movq 0x19761(%rip), %rax     # 0x23ff0
movq (%rax), %rdi
leaq 0x13344(%rip), %rsi     # 0x1dbdd
leaq 0x132ea(%rip), %rdx     # 0x1db8a
movq %r15, %rcx
xorl %eax, %eax
callq 0x2290
jmp 0xa659