// File: /workspace/github_c_makefile_star2_O2/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), %rbx
testq %rbx, %rbx
je 0x84b8
movq %rdx, 0x38(%rsp)
movq (%rdi), %rax
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %rcx
movq %rcx, 0x18(%rsp)
movq (%rcx), %rdi
movq (%rax), %rcx
movq %rcx, 0x58(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movq %rdi, %rcx
xorl %r8d, %r8d
subq $0x1, %rcx
jb 0x7e47
movq 0x18(%rsp), %rdx
movq 0x10(%rdx), %rdx
addq (%rdx,%rax), %r8
addq $0x28, %rax
jmp 0x7e2e
pushq $0x8
popq %rsi
movq %rdi, 0x10(%rsp)
movq %r8, 0x48(%rsp)
movq %r8, %rdi
callq 0x2250
movq %rax, %r14
movq 0x10(%rsp), %rax
leaq (,%rax,8), %rdi
movq %rdi, 0x28(%rsp)
callq 0x2310
movq 0x10(%rsp), %rdi
movq %rax, %r12
movq %r14, (%rax)
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq %r14, 0x8(%rsp)
cmpq %rdi, %rax
jae 0x7eae
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rsi
movq (%rsi,%rcx), %rsi
leaq (%r14,%rsi,8), %r14
movq %r14, (%r12,%rax,8)
incq %rax
addq $0x28, %rcx
jmp 0x7e8b
movq 0x38(%rsp), %rax
movq 0x10(%rax), %r15
xorl %r13d, %r13d
leaq 0x60(%rsp), %r14
cmpq %rbx, %r13
je 0x7f4a
movq %r13, %rbp
shlq $0x5, %rbp
movsd 0x18(%r15,%rbp), %xmm0
movq %r14, %rdi
callq 0x21a0
movsd 0x118e2(%rip), %xmm1    # 0x197c8
ucomisd %xmm1, %xmm0
movsd 0x118de(%rip), %xmm1    # 0x197d0
jbe 0x7efe
incq %r13
movq 0x10(%rsp), %rdi
jmp 0x7ebf
movq 0x10(%r15,%rbp), %rax
movl 0x8(%r15,%rbp), %ecx
xorl %edx, %edx
cmpq %rcx, %rdx
je 0x7ef4
movq %rdx, %r8
shlq $0x5, %r8
movq (%rax,%r8), %rsi
movq 0x8(%rax,%r8), %rdi
shrq %rsi
movq 0x10(%rax,%r8), %r8
cmpq %r8, %rdi
ja 0x7f45
movq (%r12,%rsi,8), %r9
movsd (%r9,%rdi,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r9,%rdi,8)
incq %rdi
jmp 0x7f27
incq %rdx
jmp 0x7f0a
pushq $0x8
popq %rbp
movq %rbp, %rsi
callq 0x2250
movq %rax, %r14
xorl %ebx, %ebx
cmpq 0x10(%rsp), %rbx
je 0x7fde
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbx, %rcx
movq (%rax,%rcx), %r15
movq (%r12,%rbx,8), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
leaq 0x586(%rip), %rcx       # 0x850c
callq 0x20d0
xorl %eax, %eax
movsd 0x11833(%rip), %xmm0    # 0x197c8
cmpq %rax, %r15
je 0x7fa8
ucomisd (%r13,%rax,8), %xmm0
jbe 0x7fab
incq %rax
jmp 0x7f95
movq %r15, %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
subl %eax, %r15d
movl %r15d, %esi
pushq $0x1
popq %rdx
callq 0x8529
movsd 0x11804(%rip), %xmm1    # 0x197d0
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbx,8)
incq %rbx
jmp 0x7f5a
movq %r12, %rdi
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2060
movq 0x38(%rsp), %rdx
movq 0x10(%rdx), %rax
movq (%rax), %rcx
xorl %r13d, %r13d
xorl %r9d, %r9d
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpq (%rdx), %r15
jae 0x80b3
cmpq (%rax,%r13), %rcx
je 0x80a7
cmpq 0x8(%rsp), %rbx
jne 0x8063
leaq (%rbx,%rbx), %rcx
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsp)
imulq $0x18, %rcx, %rsi
movq %r12, %rdi
movq %rdx, %rbp
movq %r9, %r12
callq 0x2350
movq %r12, %r9
movq %rax, %r12
movq 0x10(%rbp), %rax
movq 0x20(%rsp), %rdi
jmp 0x806b
movq 0x20(%rsp), %rdi
movq %rdx, %rbp
imulq $0x18, %rbx, %r8
incq %rbx
addq %r12, %r8
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 0x8609
movq 0x10(%rbp), %rax
movq (%rax,%r13), %rcx
movq %r15, %r9
movq %rbp, %rdx
incq %r15
addq $0x20, %r13
jmp 0x8013
cmpq 0x8(%rsp), %rbx
jne 0x80e0
imulq $0x30, %rbx, %rax
testq %rbx, %rbx
pushq $0x30
popq %rsi
cmovneq %rax, %rsi
movq %r12, %rdi
movq %rdx, %r13
movq %r9, %r12
callq 0x2350
movq %r12, %r9
movq %rax, %r12
movq 0x10(%r13), %rax
movq 0x20(%rsp), %rdi
imulq $0x18, %rbx, %r8
incq %rbx
addq %r12, %r8
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 0x8609
movq 0x28(%rsp), %rdi
callq 0x2310
movq %rax, %r15
xorl %r13d, %r13d
cmpq $0x3e8, %r13            # imm = 0x3E8
je 0x82a9
movq %r15, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x21e0
xorpd %xmm3, %xmm3
movapd 0x116f5(%rip), %xmm6    # 0x19840
movapd 0x116cd(%rip), %xmm5    # 0x19820
movsd 0x116b5(%rip), %xmm4    # 0x19810
xorl %eax, %eax
cmpq %rbx, %rax
je 0x81f6
imulq $0x18, %rax, %rdx
leaq (%r12,%rdx), %rcx
movl 0x4(%r12,%rdx), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rdx, %rdi
je 0x81ee
movl (%rcx), %r8d
movq %rsi, %r9
imulq %r8, %r9
addq 0x10(%rcx), %r9
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x81a5
movq (%r9,%r10,8), %r11
addsd (%r14,%r11,8), %xmm0
incq %r10
jmp 0x8191
ucomisd %xmm3, %xmm0
jne 0x81b6
jp 0x81b6
incq %rdi
addq $0x8, %rsi
jmp 0x8177
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x81ad
movq (%r9,%r10,8), %r11
movsd (%r14,%r11,8), %xmm1
divsd %xmm0, %xmm1
movq 0x8(%rcx), %rbp
movl (%rbp,%rdi,4), %ebp
cvtsi2sd %rbp, %xmm2
mulsd %xmm1, %xmm2
addsd (%r15,%r11,8), %xmm2
movsd %xmm2, (%r15,%r11,8)
incq %r10
jmp 0x81b9
incq %rax
jmp 0x815d
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x10(%rsp), %rsi
movsd 0x115bd(%rip), %xmm3    # 0x197c8
cmpq %rcx, %rsi
je 0x825c
movsd (%r15,%rcx,8), %xmm7
movq 0x18(%rsp), %rdx
movq 0x10(%rdx), %rdx
movsd (%rdx,%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
jmp 0x820b
cmpl $0x3, 0x34(%rsp)
jge 0x8271
ucomisd %xmm0, %xmm3
ja 0x82a9
incq %r13
jmp 0x8123
movq 0x18d78(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
leaq 0x1191c(%rip), %rsi     # 0x19b9e
leaq 0x11901(%rip), %rdx     # 0x19b8a
movq %r13, %rcx
movb $0x1, %al
movsd %xmm0, 0x8(%rsp)
callq 0x2280
movsd 0x8(%rsp), %xmm0
movsd 0x11521(%rip), %xmm3    # 0x197c8
jmp 0x8263
cmpl $0x3, 0x34(%rsp)
jge 0x84e5
movq %r15, %rdi
callq 0x2060
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x8(%rsp)
pushq $0x8
popq %rsi
movq 0x48(%rsp), %rdi
callq 0x2250
movq %rax, %r15
movq 0x28(%rsp), %rdi
callq 0x2310
movq %rax, %r13
movq %r15, (%rax)
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq %r15, 0x28(%rsp)
movq 0x10(%rsp), %rdi
movsd 0x114c7(%rip), %xmm3    # 0x197c8
cmpq %rdi, %rax
jae 0x8325
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rsi
movq (%rsi,%rcx), %rsi
leaq (%r15,%rsi,8), %r15
movq %r15, (%r13,%rax,8)
incq %rax
addq $0x28, %rcx
jmp 0x8301
movabsq $0x3ffffffffff, %rax    # imm = 0x3FFFFFFFFFF
xorl %esi, %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movabsq $0xfffffffff, %rdx      # imm = 0xFFFFFFFFF
cmpq 0x58(%rsp), %rsi
je 0x83e9
movq %rsi, %r9
incq %rsi
movq 0x8(%rsp), %r8
movq (%r8,%r9,8), %rdi
movq 0x8(%r8,%r9,8), %r8
subq %rdi, %r8
je 0x8340
sarq $0x4, %r8
xorpd %xmm0, %xmm0
movq %rdi, %r10
movq %r8, %r11
subq $0x1, %r11
jb 0x8393
movq (%r10), %rbp
movq 0x8(%r10), %r15
shldq $0x1c, %rbp, %r15
andq %rax, %r15
addsd (%r14,%r15,8), %xmm0
addq $0x10, %r10
jmp 0x8372
ucomisd %xmm0, %xmm3
ja 0x8340
shlq $0x5, %r9
movq 0x50(%rsp), %r10
addq %r10, %r9
addq $0x10, %r9
subq $0x1, %r8
jb 0x8340
movq (%rdi), %r10
movq 0x8(%rdi), %r11
shldq $0x1c, %r10, %r11
andq %rax, %r11
movsd (%r14,%r11,8), %xmm1
divsd %xmm0, %xmm1
movl (%r9), %ebp
andl %ecx, %ebp
cvtsi2sd %ebp, %xmm2
mulsd %xmm1, %xmm2
movq (%r13,%r11,8), %r11
andq %rdx, %r10
movsd %xmm2, (%r11,%r10,8)
addq $0x10, %rdi
jmp 0x83a9
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %rbp, 0x10(%rsp)
je 0x8429
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r15), %esi
movq (%r13,%rbp,8), %rdi
xorl %edx, %edx
callq 0x8529
movsd 0x113ba(%rip), %xmm1    # 0x197d0
maxsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbp,8)
incq %rbp
addq $0x28, %r15
jmp 0x83ee
movq %r13, %rdi
callq 0x2060
movq 0x28(%rsp), %rdi
callq 0x2060
pushq $0x4
popq %rax
xorl %ecx, %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
cmpq %rcx, 0x10(%rsp)
je 0x8478
cvttsd2si (%r14,%rcx,8), %rsi
movq 0x18(%rsp), %rdi
movq 0x10(%rdi), %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
jmp 0x8445
leaq 0x10(%r12), %r15
subq $0x1, %rbx
jb 0x849a
movq -0x8(%r15), %rdi
callq 0x2060
movq (%r15), %rdi
callq 0x2060
addq $0x18, %r15
jmp 0x847d
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060
movq 0x18b31(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
leaq 0x11682(%rip), %rsi     # 0x19b4b
leaq 0x116ba(%rip), %rdx     # 0x19b8a
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
movq 0x18b04(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
leaq 0x116e7(%rip), %rsi     # 0x19bdd
leaq 0x1168d(%rip), %rdx     # 0x19b8a
movq %r13, %rcx
xorl %eax, %eax
callq 0x2280
jmp 0x82b4