// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/syncasm.c
// Function: scg_refine_arc_coverage
void scg_refine_arc_coverage(scg_t *g, int verbose)
{
    // update arc coverage on graph
    // the arc coverage on the input graph is the number of syncmer pair links in the read set
    // need to check if there are other pair links on the graph
    // if yes, calculate weighted arc coverage by utg coverge
    
    uint64_t i, j, n, m, p, v, w, c, nl, ld, *a;
    asmg_t *utg_g;
    asmg_arc_t *arc;
    u64_v_t *link_pairs, *pair;
    khint_t k;
    kh_u128_t *h_arc;
    int absent;

    utg_g = g->utg_asmg;
    h_arc = kh_u128_init();
    nl = asmg_max_link_id(utg_g) + 1; // number of links
    MYCALLOC(link_pairs, nl);
    // add arcs
    for (i = 0, n = utg_g->n_arc; i < n; ++i) {
        arc = &utg_g->arc[i];
        if (arc->del || arc->comp) continue;
        v = asmg_arc_head_e(utg_g, arc);
        w = asmg_arc_tail_e(utg_g, arc);
        if (v > w) {
            v ^= 1;
            w ^= 1;
            SWAP(v, w);
        }
        ld = arc->link_id;
        // add arc
        k = kh_u128_put(h_arc, (uint128_t) v << 64 | w, &absent);
        // only update if absent
        // for (v,w) with multiple link ids
        // all pack in the first link
        if (absent)
            kh_val(h_arc, k) = ld;
        else
            ld = kh_val(h_arc, k);
        kv_push(uint64_t, link_pairs[ld], arc->link_id);
        kv_push(uint64_t, link_pairs[ld], ((uint64_t) 
                    utg_g->vtx[arc->v >> 1].cov + 
                    utg_g->vtx[arc->w >> 1].cov) / 2);
    }
    // check utgs
    for (i = 0, n = utg_g->n_vtx; i < n; ++i) {
        a = utg_g->vtx[i].a;
        for (j = 1, m = utg_g->vtx[i].n; j < m; ++j) {
            v = a[j - 1];
            w = a[j];
            if (v > w) {
                v ^= 1;
                w ^= 1;
                SWAP(v, w);
            }
            k = kh_u128_get(h_arc, (uint128_t) v << 64 | w);
            if (k == kh_end(h_arc)) continue;
            ld = kh_val(h_arc, k);
            kv_push(uint64_t, link_pairs[ld], UINT64_MAX);
            kv_push(uint64_t, link_pairs[ld], utg_g->vtx[i].cov);
        } 
    }
    // update arc covs on graph
    for (i = 0, n = utg_g->n_arc; i < n; ++i) {
        arc = &utg_g->arc[i];
        if (arc->del || arc->comp) continue;
        v = asmg_arc_head_e(utg_g, arc);
        w = asmg_arc_tail_e(utg_g, arc);
        if (v > w) {
            v ^= 1;
            w ^= 1;
            SWAP(v, w);
        }
        k = kh_u128_get(h_arc, (uint128_t) v << 64 | w);
        ld = kh_val(h_arc, k);
        pair = &link_pairs[ld];
        if (pair->n == 2) {
            assert(pair->a[0] == ld);
            continue; // no need to update
        }
        // find the pair and total cov
        c = 0;
        p = UINT64_MAX;
        for (j = 1, m = pair->n; j < m; j += 2) {
            if (pair->a[j - 1] == arc->link_id)
                p = j;
            c += pair->a[j];
        }
        if (c == 0) continue;
        assert(p < m);
        c = lround((double) arc->cov / c * pair->a[p]);
        // c = MIN(c, utg_g->vtx[arc->v >> 1].cov);
        // c = MIN(c, utg_g->vtx[arc->w >> 1].cov);
        if (verbose > 2)
            fprintf(stderr, "[M::%s] arc u%lu%c -> u%lu%c coverage updated: %u -> %lu\n", 
                    __func__, arc->v>>1, "+-"[arc->v&1], arc->w>>1, "+-"[arc->w&1], arc->cov, c);
        arc->cov = c;

        // for reverse complementary arcs
        arc = asmg_comp_arc1(utg_g, arc);
        if (arc) arc->cov = c;
    }

    // fix arc coverage
    // bounded by the minimum utg cov
    asmg_arc_fix_cov(utg_g);
    
    for (i = 0; i < nl; ++i)
        free(link_pairs[i].a);
    free(link_pairs);
    kh_u128_destroy(h_arc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0x30(%rsp)
movq 0x8(%rdi), %rbx
callq 0x3cde
movq %rax, %r15
movq %rbx, %rdi
callq 0x11fef
incq %rax
pushq $0x18
popq %rsi
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x2250
movq %rax, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %r14
xorl %ebx, %ebx
movq %r15, 0x28(%rsp)
subq $0x1, %r14
jb 0x9665
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
cmpl $0x3fffffff, 0x20(%rax,%rbx) # imm = 0x3FFFFFFF
ja 0x965c
leaq (%rax,%rbx), %rbp
movq %rax, 0x10(%rsp)
movq %rbx, %r12
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9a48
movq %rax, %r13
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x9a78
movq %r13, %rsi
xorq $0x1, %rsi
movq %rax, %rdx
xorq $0x1, %rdx
cmpq %rax, %r13
cmovbeq %rax, %rsi
cmovbeq %r13, %rdx
movq %r12, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq 0x28(%rax,%r12), %rbx
movq 0x10(%rsp), %r12
movq %r15, %rdi
leaq 0x34(%rsp), %rcx
callq 0x9aa9
movl %eax, %eax
imulq $0x18, %rax, %rax
cmpl $0x0, 0x34(%rsp)
movq 0x10(%r15), %rcx
leaq 0x10(%rcx,%rax), %rax
je 0x958d
movq %rbx, (%rax)
jmp 0x9590
movq (%rax), %rbx
imulq $0x18, %rbx, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r13
movq (%rcx,%rax), %rbp
movq 0x8(%rcx,%rax), %r15
cmpq %r15, %rbp
jne 0x95d8
leaq (,%rbp,2), %r15
testq %rbp, %rbp
pushq $0x2
popq %rax
cmoveq %rax, %r15
movq %r15, 0x8(%r13)
movq 0x10(%r13), %rdi
leaq (,%r15,8), %rsi
callq 0x2350
movq %rax, 0x10(%r13)
jmp 0x95dc
movq 0x10(%r13), %rax
movq 0x20(%rsp), %rbx
movq 0x28(%r12,%rbx), %rcx
leaq 0x1(%rbp), %rdx
movq %rcx, (%rax,%rbp,8)
cmpq %r15, %rdx
jne 0x9615
leaq (%r15,%r15), %rsi
testq %r15, %r15
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2350
movq %rax, 0x10(%r13)
movq 0x28(%rsp), %r15
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq (%r12,%rbx), %rdx
movq 0x8(%r12,%rbx), %rsi
shrq %rdx
imulq $0x28, %rdx, %rdx
movl 0x20(%rcx,%rdx), %edx
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
andl %edi, %edx
shrq %rsi
imulq $0x28, %rsi, %rsi
movl 0x20(%rcx,%rsi), %ecx
andl %edi, %ecx
addl %edx, %ecx
shrl %ecx
leaq 0x2(%rbp), %rdx
movq %rdx, (%r13)
movq %rcx, 0x8(%rax,%rbp,8)
addq $0x30, %rbx
jmp 0x94ed
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
cmpq 0x40(%rsp), %rcx
je 0x97b0
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20(%rsp)
imulq $0x28, %rcx, %rcx
movq (%rax,%rcx), %r13
movq %rcx, 0x10(%rsp)
movq 0x8(%rax,%rcx), %r12
pushq $0x1
popq %rbp
cmpq %r13, %rbp
jae 0x97a3
movq -0x8(%r12,%rbp,8), %rax
movq (%r12,%rbp,8), %rcx
movq %rax, %rsi
xorq $0x1, %rsi
movq %rcx, %rdx
xorq $0x1, %rdx
cmpq %rcx, %rax
cmovbeq %rcx, %rsi
cmovbeq %rax, %rdx
movq %r15, %rdi
callq 0x41fd
movq 0x10(%r15), %rdx
testq %rdx, %rdx
je 0x96e8
movb (%r15), %cl
pushq $0x1
popq %rsi
shll %cl, %esi
jmp 0x96ea
xorl %esi, %esi
cmpl %esi, %eax
je 0x979b
movl %eax, %eax
imulq $0x18, %rax, %rax
imulq $0x18, 0x10(%rdx,%rax), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r15
movq (%rcx,%rax), %r14
movq 0x8(%rcx,%rax), %rbx
cmpq %rbx, %r14
jne 0x973e
leaq (%r14,%r14), %rbx
testq %r14, %r14
pushq $0x2
popq %rax
cmoveq %rax, %rbx
movq %rbx, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq (,%rbx,8), %rsi
callq 0x2350
movq %rax, 0x10(%r15)
jmp 0x9742
movq 0x10(%r15), %rax
leaq 0x1(%r14), %rcx
orq $-0x1, (%rax,%r14,8)
cmpq %rbx, %rcx
jne 0x9772
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2350
movq %rax, 0x10(%r15)
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rsp), %rdx
movl 0x20(%rcx,%rdx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
leaq 0x2(%r14), %rdx
movq %rdx, (%r15)
movq %rcx, 0x8(%rax,%r14,8)
movq 0x28(%rsp), %r15
incq %rbp
jmp 0x96a2
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x9674
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rbx
xorl %r14d, %r14d
movq %rbx, 0x10(%rsp)
cmpq %rbx, %r14
je 0x99be
movq 0x8(%rsp), %rax
movq 0x28(%rax), %r13
imulq $0x30, %r14, %rax
movl 0x20(%r13,%rax), %r12d
cmpl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
ja 0x994e
addq %rax, %r13
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9a48
movq %rax, %rbp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x9a78
movq %rbp, %rsi
xorq $0x1, %rsi
movq %rax, %rdx
xorq $0x1, %rdx
cmpq %rax, %rbp
cmovbeq %rax, %rsi
cmovbeq %rbp, %rdx
movq %r15, %rdi
callq 0x41fd
movq 0x10(%r15), %rcx
movl %eax, %eax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %rcx
imulq $0x18, %rcx, %rdx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rdx), %rax
movq (%rsi,%rdx), %rdx
cmpq $0x2, %rdx
jne 0x9868
movq 0x10(%rax), %rax
cmpq %rcx, (%rax)
movq 0x10(%rsp), %rbx
je 0x994e
jmp 0x9a0a
xorl %esi, %esi
pushq $-0x1
popq %rcx
pushq $0x1
popq %rdi
movq 0x10(%rsp), %rbx
cmpq %rdx, %rdi
jae 0x9895
movq 0x10(%rax), %r8
movq -0x8(%r8,%rdi,8), %r9
cmpq 0x28(%r13), %r9
cmoveq %rdi, %rcx
addq (%r8,%rdi,8), %rsi
addq $0x2, %rdi
jmp 0x9875
testq %rsi, %rsi
je 0x994e
cmpq %rdx, %rcx
jae 0x9a29
cvtsi2sd %r12d, %xmm1
movq %rsi, %xmm0
movq 0xff57(%rip), %xmm3     # 0x19810
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0xff5b(%rip), %xmm4     # 0x19820
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movq 0x10(%rax), %rax
movq (%rax,%rcx,8), %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm4, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
callq 0x2110
movq %rax, %rbp
cmpl $0x3, 0x30(%rsp)
jge 0x9956
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
movl 0x20(%r13), %eax
movl $0xc0000000, %ecx       # imm = 0xC0000000
andl %ecx, %eax
orl %ebp, %eax
movl %eax, 0x20(%r13)
movq (%r13), %rdx
movq 0x8(%r13), %rsi
xorq $0x1, %rsi
xorq $0x1, %rdx
movq 0x8(%rsp), %rdi
callq 0x6a92
testq %rax, %rax
je 0x994e
movl 0x20(%rax), %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
andl %edx, %ecx
orl %ebp, %ecx
movl %ecx, 0x20(%rax)
incq %r14
jmp 0x97c1
movq 0x17693(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
movq (%r13), %rax
movq 0x8(%r13), %rdx
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
leaq 0xfffe(%rip), %rsi      # 0x19976
movsbl (%rax,%rsi), %r8d
movq %rdx, %r9
shrq %r9
andl $0x1, %edx
movsbl (%rdx,%rsi), %r10d
movl 0x20(%r13), %r11d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r11d
subq $0x8, %rsp
leaq 0x102b8(%rip), %rsi     # 0x19c5a
leaq 0x102eb(%rip), %rdx     # 0x19c94
xorl %eax, %eax
pushq %rbp
pushq %r11
pushq %r10
callq 0x2280
addq $0x20, %rsp
jmp 0x9909
movq 0x8(%rsp), %rdi
callq 0x120cb
movq 0x18(%rsp), %rbx
addq $0x10, %rbx
movq 0x38(%rsp), %r14
subq $0x1, %r14
jb 0x99ea
movq (%rbx), %rdi
callq 0x2060
addq $0x18, %rbx
jmp 0x99d6
movq 0x18(%rsp), %rdi
callq 0x2060
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3d41
leaq 0x10207(%rip), %rdi     # 0x19c18
leaq 0xfffb(%rip), %rsi      # 0x19a13
leaq 0x1020a(%rip), %rcx     # 0x19c29
movl $0x8b3, %edx            # imm = 0x8B3
callq 0x21d0
leaq 0x10224(%rip), %rdi     # 0x19c54
leaq 0xffdc(%rip), %rsi      # 0x19a13
leaq 0x101eb(%rip), %rcx     # 0x19c29
movl $0x8bf, %edx            # imm = 0x8BF
callq 0x21d0