// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %rsi, %r14
movq 0x18(%rsi), %rax
movl 0x14(%rax), %esi
addl %esi, %esi
leaq 0x30(%rsp), %rdi
callq 0x1431c3
movq %r14, 0x18(%rsp)
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 0x14573c
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x145829
movq (%rdi), %rax
testq %rax, %rax
movq 0x8(%rsp), %r8
je 0x145915
negq %rbp
movl $0x8, %r12d
xorl %r15d, %r15d
movq 0x10(%rdi), %rdx
movq -0x8(%rdx,%r12), %r14
addq %r14, %r14
movl %ebp, %ecx
shrq %cl, %r14
movq (%rdx,%r12), %r13
addq %r13, %r13
shrq %cl, %r13
cmpq %r13, %r14
je 0x145814
movq (%r8), %rdx
movq 0x80(%r8), %rcx
movl (%rdx,%r14,4), %esi
incq %rcx
cmpq %rsi, %rcx
jb 0x145814
movl (%rdx,%r13,4), %edx
cmpq %rdx, %rcx
jb 0x145814
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 0x1416e0
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 0x1416e0
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %r8
movq (%rdi), %rax
incq %r15
addq $0x20, %r12
cmpq %rax, %r15
jb 0x14577c
jmp 0x145915
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r8
je 0x145915
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbp
movq (%r8), %rdx
movq 0x80(%r8), %rsi
movl (%rdx,%r13,4), %edx
incq %rsi
cmpq %rdx, %rsi
jb 0x1458fe
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x1458fe
leaq (,%r13,2), %r15
addq %r13, %r15
shlq $0x4, %r15
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpq %r14, %r13
je 0x1458e9
movq (%r8), %rcx
movq 0x80(%r8), %rdx
movl (%rcx,%r14,4), %ecx
incq %rdx
cmpq %rcx, %rdx
jb 0x1458e9
movq $-0x1, 0x10(%rsp)
movq 0x10(%rbx), %rdi
addq %r12, %rdi
xorps %xmm0, %xmm0
movq %r13, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x1416e0
movq 0x10(%rbx), %rdi
addq %r15, %rdi
xorps %xmm0, %xmm0
movq %r14, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x1416e0
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
incq %r14
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq $0x30, %r12
cmpq %rcx, %r14
jb 0x145889
incq %r13
movl 0x14(%rax), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %r13
jb 0x14584e
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r8, %r13
callq 0xc8dad
movq %rax, %r14
movq %rax, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x1433a3
leaq 0x30(%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x141dcb
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x142ce3
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x144248
movq %r14, %rdi
callq 0xa0494
movq %r12, %rdi
callq 0x143241
movq (%rbx), %rsi
testq %rsi, %rsi
je 0x1459ed
movq 0x10(%rbx), %rcx
xorl %eax, %eax
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
cmpq $0x0, (%rcx,%rdx)
je 0x1459e5
movl $0x18, %esi
movq %rax, %rdi
shlq $0x4, %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
movq 0x10(%rcx,%rdi), %rcx
andb $-0x80, (%rcx,%rsi)
movq 0x10(%rbx), %rcx
movq 0x10(%rcx,%rdi), %r9
cmpb $0x0, (%r9,%rsi)
js 0x1459d5
cmpq $-0x1, -0x8(%r9,%rsi)
je 0x1459d5
movb $0x1, (%r9,%rsi)
movq 0x10(%rbx), %rcx
incq %r8
addq $0x28, %rsi
cmpq (%rcx,%rdx), %r8
jb 0x1459ab
movq (%rbx), %rsi
incq %rax
cmpq %rsi, %rax
jb 0x145989
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq