// File: chhylp123[P]hifiasm/hic.cpp
// Function: resolve_tangles_hic(ha_ug_index*, bubble_type*, kvec_pe_hit*, kv_u_trans_t*)
void resolve_tangles_hic(ha_ug_index *idx, bubble_type *bub, kvec_pe_hit *hits, kv_u_trans_t *ta)
{
    uint32_t i, k, l, m, h_occ;
    uint64_t shif = 64 - idx->uID_bits, qn, tn;
    pe_hit *h_a = NULL;
    u_trans_t *p = NULL;
    identify_bubbles(idx->ug, bub, idx->t_ch->ir_het, &(idx->t_ch->k_trans));
    ta->idx.n = ta->n = 0;
    if(hits->idx.n == 0) idx_hc_links(hits, idx, NULL);
    for (qn = 0; qn < hits->idx.n; qn++)
    {
        h_a = hits->a.a + (hits->idx.a[qn]>>32);
        h_occ = (uint32_t)(hits->idx.a[qn]);

        for (k = 1, l = 0; k <= h_occ; ++k) ///same qn
        {
            if (k == h_occ || ((h_a[k].e<<1)>>shif) != ((h_a[l].e<<1)>>shif)) //same qn and tn
            {
                tn = ((h_a[l].e<<1)>>shif);
                if(tn != qn)
                {
                    kv_pushp(u_trans_t, *ta, &p);
                    p->qn = qn; p->tn = tn; p->occ = (k-l);
                    kv_pushp(u_trans_t, *ta, &p);
                    p->qn = tn; p->tn = qn; p->occ = (k-l);
                }
                l = k;
            }
        }
    }
    radix_sort_u_trans_m(ta->a, ta->a + ta->n);
    for (k = 1, l = 0, m = 0; k <= ta->n; ++k)
    {
        if (k == ta->n || ta->a[l].qn != ta->a[k].qn || ta->a[l].tn != ta->a[k].tn) //same qn and tn
        {
            for (i = l, h_occ = 0; i < k; i++)
            {
                h_occ += ta->a[i].occ;
            }

            qn = ta->a[l].qn;
            tn = ta->a[l].tn;
            ta->a[m] = ta->a[l];
            ta->a[m].occ = h_occ;
            ta->a[m].nw = (h_occ*SCALL)/(MIN(hits->occ.a[qn], hits->occ.a[tn]));
            m++;
            l = k;
        }
    }
    ta->n = m;
    kt_u_trans_t_idx(ta, idx->ug->g->n_seq);
    
    resolve_bubble_chain_by_hic(idx, ta, bub);
    ta->idx.n = ta->n = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rcx
movl $0x40, %eax
subq 0x18(%rcx), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x138, %rcx            # imm = 0x138
callq 0x1c5bb0
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x2057ba
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1d2bc0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x205a2d
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x34(%rbp)
movl $0x1, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x34(%rbp), %eax
ja 0x205a1a
movl -0x28(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x205874
movq -0x58(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x40(%rbp), %rcx
shrq %cl, %rax
movq -0x58(%rbp), %rcx
movl -0x2c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rdx
shlq %rdx
movq -0x40(%rbp), %rcx
shrq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
je 0x205a0a
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x40(%rbp), %rcx
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x205a04
jmp 0x2058a4
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x205909
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2058d1
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x2058dc
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x2058dc
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
imulq $0x30, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x28(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x2059b9
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x205981
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x20598c
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x20598c
movq -0x70(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
imulq $0x30, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x28(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x205a0c
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x20581c
jmp 0x205a1c
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2057c2
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rax
imulq $0x30, (%rax), %rax
addq %rax, %rsi
callq 0x1b8260
movl $0x1, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x28(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
ja 0x205c50
movl -0x28(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
je 0x205ada
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x28(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x205ada
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x28(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
je 0x205c40
movl -0x2c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x205b15
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x205ae7
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x2c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x30(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x30, %edx
callq 0x2180
movl -0x34(%rbp), %ecx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
imull $0x2710, -0x34(%rbp), %eax # imm = 0x2710
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq 0x40(%rcx), %rcx
movq -0x50(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
ja 0x205bd2
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
jmp 0x205be6
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %xmm0
movaps 0x17c411(%rip), %xmm1   # 0x382010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x17c415(%rip), %xmm1   # 0x382020
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x20(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x205c42
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x205a62
movl -0x30(%rbp), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x104540
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0x1fb5c0
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)