// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: measure_distance(ha_ug_index*, ma_ug_t const*, kvec_pe_hit*, hc_links*, bubble_type*, kv_u_trans_t*)
void measure_distance(ha_ug_index* idx, const ma_ug_t* ug, kvec_pe_hit* hits, hc_links* link, bubble_type* bub, kv_u_trans_t *ta)
{
    // double index_time = yak_realtime();
    MT M;
    init_MT(&M, ug->g->n_seq<<1);
    uint64_t uID_bits;
    for (uID_bits=1; (uint64_t)(1<<uID_bits)<(uint64_t)ug->u.n; uID_bits++);
    uint64_t k, i, shif = 64 - uID_bits, beg, end, t_d;
    if(hits)
    {
        for (k = 0; k < hits->a.n; ++k) 
        {
            beg = ((hits->a.a[k].s<<1)>>shif);
            end = ((hits->a.a[k].e<<1)>>shif);

            if(beg == end) continue;
            if(IF_HOM(beg, *bub)) continue;
            if(IF_HOM(end, *bub)) continue;

            t_d = (uint64_t)-1;
            push_hc_edge(&(link->a.a[beg]), end, 0, 0, &t_d);
            push_hc_edge(&(link->a.a[end]), beg, 0, 0, &t_d);
        }
    }
    else
    {
        for (k = 0; k < ug->g->n_seq; ++k)
        {
            if(IF_HOM(k, *bub)) continue;
            for (i = 0; i < ug->g->n_seq; ++i)
            {
                if(i == k || IF_HOM(i, *bub)) continue;
                t_d = (uint64_t)-1;
                push_hc_edge(&(link->a.a[i]), k, 0, 0, &t_d);
                push_hc_edge(&(link->a.a[k]), i, 0, 0, &t_d);
            }
        }
    }

    asg_t *copy_sg = copy_read_graph(ug->g);

    update_ug_by_trans(copy_sg, ta);
    // update_ug_by_tigs(copy_sg, link);
    all_pair_shortest_path(copy_sg, link, &M);
    fill_utg_distance_multi(copy_sg, link, &M, bub);
    update_containment_distance(copy_sg, ta, link);
    // update_dis_connected_gfa(copy_sg, link, &M);
    asg_destroy(copy_sg);
    destory_MT(&M);

    for (i = 0; i < link->a.n; i++)
    {
        for (k = 0; k < link->a.a[i].e.n; k++)
        {
            link->a.a[i].e.a[k].is_cc = 0;
            if(link->a.a[i].e.a[k].del) continue;
            if(link->a.a[i].e.a[k].dis == (uint64_t)-1) continue;
            link->a.a[i].e.a[k].is_cc = 1;
        }
    }

    // filter_disconnect_edges(idx, hits, link, bub, 1, 0.01);
    // fprintf(stderr, "[M::%s::%.3f] ==> Hi-C linkages have been counted\n", __func__, yak_realtime()-index_time);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %rbx
movq %rdx, 0x18(%rsp)
movq %rsi, %r14
movq 0x18(%rsi), %rax
movl 0x14(%rax), %esi
addl %esi, %esi
leaq 0x30(%rsp), %rdi
callq 0x13d35e
movq %r14, %rdi
movq (%r14), %rax
movq $-0x40, %rbp
leal 0x41(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movslq %edx, %rcx
incq %rbp
cmpq %rcx, %rax
ja 0x13fade
movq 0x18(%rsp), %r8
testq %r8, %r8
movq %rdi, 0x20(%rsp)
je 0x13fbd0
cmpq $0x0, (%r8)
movq 0x8(%rsp), %rsi
je 0x13fcba
negq %rbp
movl $0x8, %r12d
xorl %r15d, %r15d
movq 0x10(%r8), %rax
movq -0x8(%rax,%r12), %r14
addq %r14, %r14
movl %ebp, %ecx
shrq %cl, %r14
movq (%rax,%r12), %r13
addq %r13, %r13
shrq %cl, %r13
cmpq %r13, %r14
je 0x13fbbb
movq (%rsi), %rcx
movq 0x80(%rsi), %rax
movl (%rcx,%r14,4), %edx
incq %rax
cmpq %rdx, %rax
jb 0x13fbbb
movl (%rcx,%r13,4), %ecx
cmpq %rcx, %rax
jb 0x13fbbb
movq $-0x1, 0x10(%rsp)
leaq (%r14,%r14,2), %rdi
shlq $0x4, %rdi
addq 0x10(%rbx), %rdi
xorps %xmm0, %xmm0
movq %r13, %rsi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
callq 0x13b8bb
leaq (,%r13,2), %rdi
addq %r13, %rdi
shlq $0x4, %rdi
addq 0x10(%rbx), %rdi
xorps %xmm0, %xmm0
movq %r14, %rsi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
callq 0x13b8bb
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
incq %r15
addq $0x20, %r12
cmpq (%r8), %r15
jb 0x13fb21
jmp 0x13fcba
movq 0x18(%rdi), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rsi
je 0x13fcba
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbp
movq (%rsi), %rax
movq 0x80(%rsi), %rcx
movl (%rax,%r13,4), %eax
incq %rcx
cmpq %rax, %rcx
jb 0x13fca2
movq 0x18(%rdi), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x13fca2
leaq (,%r13,2), %r15
addq %r13, %r15
shlq $0x4, %r15
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x13fc8a
movq (%rsi), %rax
movq 0x80(%rsi), %rcx
movl (%rax,%r14,4), %eax
incq %rcx
cmpq %rax, %rcx
jb 0x13fc8a
movq $-0x1, 0x10(%rsp)
movq 0x10(%rbx), %rdi
addq %r12, %rdi
xorps %xmm0, %xmm0
movq %r13, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x13b8bb
movq 0x10(%rbx), %rdi
addq %r15, %rdi
xorps %xmm0, %xmm0
movq %r14, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x13b8bb
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
incq %r14
movq 0x18(%rdi), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x30, %r12
cmpq %rax, %r14
jb 0x13fc2e
incq %r13
movq 0x18(%rdi), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jb 0x13fbee
movq 0x18(%rdi), %rdi
movq %rsi, %r13
callq 0xc802c
movq %rax, %r14
movq %rax, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x13d54c
leaq 0x30(%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x13bfb1
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x13ce8f
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x13e644
movq %r14, %rdi
callq 0x9fcdb
movq %r12, %rdi
callq 0x13d3ea
cmpq $0x0, (%rbx)
je 0x13fd88
xorl %eax, %eax
movq 0x10(%rbx), %r8
leaq (%rax,%rax,2), %rcx
shlq $0x4, %rcx
cmpq $0x0, (%r8,%rcx)
je 0x13fd80
movl $0x18, %edx
movq %rax, %rsi
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
movq 0x10(%r8,%rsi), %r8
andb $-0x80, (%r8,%rdx)
movq 0x10(%rbx), %r8
movq 0x10(%r8,%rsi), %r8
cmpb $0x0, (%r8,%rdx)
js 0x13fd6f
cmpq $-0x1, -0x8(%r8,%rdx)
je 0x13fd6f
movb $0x1, (%r8,%rdx)
incq %rdi
movq 0x10(%rbx), %r8
addq $0x28, %rdx
cmpq (%r8,%rcx), %rdi
jb 0x13fd48
incq %rax
cmpq (%rbx), %rax
jb 0x13fd23
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq