// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rdi, %rax
pushq $0x40
popq %rcx
subq 0x18(%rdi), %rcx
movq %rcx, 0x10(%rsp)
movq (%rdi), %rdi
movq %rax, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq 0x10(%rcx), %rdx
addq $0x138, %rcx            # imm = 0x138
movq %rsi, 0x20(%rsp)
callq 0xf7387
andq $0x0, (%rbx)
andq $0x0, 0x18(%rbx)
xorl %eax, %eax
cmpq %rax, 0x18(%r15)
jne 0x119c76
movq %r15, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0xfdcd5
xorl %edi, %edi
movq 0x10(%rsp), %rcx
movq %r15, 0x28(%rsp)
cmpq 0x18(%r15), %rdi
jae 0x119dd1
movq 0x28(%r15), %rax
movq (%rax,%rdi,8), %rsi
movq %rsi, %r14
shrq $0x1b, %r14
andq $-0x20, %r14
addq 0x10(%r15), %r14
xorl %r12d, %r12d
pushq $0x1
popq %rbp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
cmpl %esi, %ebp
ja 0x119dc4
movl %r12d, %eax
jne 0x119cd1
shlq $0x5, %rax
movq 0x8(%r14,%rax), %r13
addq %r13, %r13
shrq %cl, %r13
jmp 0x119cfe
movl %ebp, %edx
shlq $0x5, %rdx
movq 0x8(%r14,%rdx), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
shlq $0x5, %rax
movq 0x8(%r14,%rax), %r13
addq %r13, %r13
shrq %cl, %r13
cmpq %r13, %rdx
jne 0x119cfe
movl %r12d, %eax
jmp 0x119dba
movl %ebp, %eax
cmpq %rdi, %r13
je 0x119dba
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq %rcx, %rdx
jne 0x119d47
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbx)
imulq $0x30, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbx)
imulq $0x30, %rdx, %rdx
movl %edi, 0x8(%rax,%rdx)
movl %r13d, 0x14(%rax,%rdx)
movl %ebp, %r15d
subl %r12d, %r15d
movl %r15d, 0x18(%rax,%rdx)
cmpq %rcx, %rsi
jne 0x119d95
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
imulq $0x30, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%rbx)
imulq $0x30, %rsi, %rcx
movl %r13d, 0x8(%rax,%rcx)
movl %edi, 0x14(%rax,%rcx)
movl %r15d, 0x18(%rax,%rcx)
movl %ebp, %eax
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rsi
incl %ebp
movl %eax, %r12d
jmp 0x119cb3
incq %rdi
movq 0x28(%rsp), %r15
jmp 0x119c82
movq 0x10(%rbx), %rdi
imulq $0x30, (%rbx), %rsi
addq %rdi, %rsi
callq 0xf0272
xorl %r8d, %r8d
pushq $0x1
popq %rcx
xorl %esi, %esi
movl %ecx, %edx
movl %esi, %eax
cmpq %rdx, (%rbx)
jb 0x119ebb
movl %r8d, %edi
je 0x119e27
movq 0x10(%rbx), %r9
imulq $0x30, %rdi, %r10
movl 0x8(%r9,%r10), %ebp
imulq $0x30, %rdx, %r11
cmpl 0x8(%r9,%r11), %ebp
jne 0x119e27
addq %r9, %r10
addq %r9, %r11
movl 0x14(%r10), %r9d
cmpl 0x14(%r11), %r9d
je 0x119eb4
movq 0x10(%rbx), %r8
imulq $0x30, %rdi, %r10
leaq (%r8,%r10), %r11
addq $0x18, %r11
xorl %r9d, %r9d
cmpq %rdx, %rdi
jae 0x119e4b
addl (%r11), %r9d
incq %rdi
addq $0x30, %r11
jmp 0x119e3a
movl 0x8(%r8,%r10), %edx
movl 0x14(%r8,%r10), %edi
imulq $0x30, %rax, %r11
movups 0x20(%r8,%r10), %xmm0
movups %xmm0, 0x20(%r8,%r11)
movups 0x10(%r8,%r10), %xmm0
movups %xmm0, 0x10(%r8,%r11)
movups (%r8,%r10), %xmm0
movups %xmm0, (%r8,%r11)
movq 0x10(%rbx), %r8
movl %r9d, 0x18(%r8,%r11)
imull $0x2710, %r9d, %eax     # imm = 0x2710
movq 0x40(%r15), %r9
movq (%r9,%rdx,8), %rdx
movq (%r9,%rdi,8), %rdi
cmpq %rdi, %rdx
cmovbq %rdx, %rdi
xorl %edx, %edx
divq %rdi
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%r8,%r11)
incl %esi
movl %ecx, %r8d
incl %ecx
jmp 0x119de9
movq %rax, (%rbx)
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rbx, %rdi
callq 0x88a31
movq %r14, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x113e73
andq $0x0, (%rbx)
andq $0x0, 0x18(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq