// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl $0x40, %eax
subq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
movq (%rdi), %rdi
movq 0x10(%r14), %rcx
movq 0x10(%rcx), %rdx
addq $0x138, %rcx            # imm = 0x138
movq %rsi, 0x30(%rsp)
callq 0x138ab3
movq %rbx, %r8
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, 0x18(%rbx)
cmpq %rax, 0x18(%r15)
movq %rbx, 0x8(%rsp)
jne 0x1598a7
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x13f81d
movq 0x8(%rsp), %r8
movq %r14, 0x10(%rsp)
cmpq $0x0, 0x18(%r15)
movq 0x20(%rsp), %rcx
je 0x159a34
xorl %r9d, %r9d
movq %r15, 0x18(%rsp)
movq 0x28(%r15), %rax
movq (%rax,%r9,8), %r15
testl %r15d, %r15d
je 0x159a22
movq %r15, %r12
shrq $0x1b, %r12
andq $-0x20, %r12
movq 0x18(%rsp), %rax
addq 0x10(%rax), %r12
movl $0x1, %r14d
xorl %r13d, %r13d
movq %r9, 0x28(%rsp)
movl %r13d, %eax
cmpl %r14d, %r15d
je 0x159930
movl %r14d, %edx
shlq $0x5, %rdx
movq 0x8(%r12,%rdx), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
movq %rax, %rsi
shlq $0x5, %rsi
movq 0x8(%r12,%rsi), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
cmpq %rsi, %rdx
jne 0x159930
movl %r13d, %eax
jmp 0x159a13
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rbp
addq %rbp, %rbp
shrq %cl, %rbp
movl %r14d, %eax
cmpq %r9, %rbp
je 0x159a13
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x159987
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl %r9d, 0x8(%rdi,%rax)
movl %ebp, 0x14(%rdi,%rax)
movl %r14d, %ebx
subl %r13d, %ebx
movl %ebx, 0x18(%rdi,%rax)
movq 0x8(%r8), %rax
cmpq %rax, %rcx
jne 0x1599e8
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r8)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x28(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl %ebp, 0x8(%rcx,%rax)
movl %r9d, 0x14(%rcx,%rax)
movl %ebx, 0x18(%rcx,%rax)
movl %r14d, %eax
movq 0x20(%rsp), %rcx
incl %r14d
movl %eax, %r13d
cmpl %r15d, %r14d
jbe 0x1598f7
incq %r9
movq 0x18(%rsp), %r15
cmpq 0x18(%r15), %r9
jb 0x1598c4
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0x130888
movq 0x8(%rsp), %rdi
movq (%rdi), %r8
xorl %ecx, %ecx
testq %r8, %r8
je 0x159b70
movl $0x1, %esi
movl $0x1, %r11d
xorl %edx, %edx
movq 0x10(%rsp), %r14
movl %edx, %eax
cmpq %r11, %r8
je 0x159aab
movq 0x10(%rdi), %r8
leaq (%rax,%rax,2), %r9
shlq $0x4, %r9
movl 0x8(%r8,%r9), %r10d
leaq (%r11,%r11,2), %r11
shlq $0x4, %r11
cmpl 0x8(%r8,%r11), %r10d
jne 0x159aab
addq %r8, %r9
addq %r8, %r11
movl 0x14(%r9), %r8d
cmpl 0x14(%r11), %r8d
je 0x159b5b
xorl %ebx, %ebx
cmpl %esi, %edx
jae 0x159ad7
movl %esi, %edx
movq 0x10(%rdi), %r9
leaq (%rax,%rax,2), %r8
shlq $0x4, %r8
addq %r9, %r8
addq $0x18, %r8
subq %rax, %rdx
xorl %ebx, %ebx
addl (%r8), %ebx
addq $0x30, %r8
decq %rdx
jne 0x159acb
movq 0x10(%rdi), %rdx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl 0x8(%rdx,%rax), %r8d
movl 0x14(%rdx,%rax), %r9d
movl %ecx, %r10d
shlq $0x4, %r10
leaq (%r10,%r10,2), %r10
movups 0x20(%rdx,%rax), %xmm0
movups %xmm0, 0x20(%rdx,%r10)
movups 0x10(%rdx,%rax), %xmm0
movups %xmm0, 0x10(%rdx,%r10)
movups (%rdx,%rax), %xmm0
movups %xmm0, (%rdx,%r10)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r11
movl %ebx, 0x18(%r11,%r10)
imull $0x2710, %ebx, %eax     # imm = 0x2710
movq 0x40(%r15), %rdx
movq (%rdx,%r8,8), %rdi
movq (%rdx,%r9,8), %r8
cmpq %r8, %rdi
cmovbq %rdi, %r8
xorl %edx, %edx
divq %r8
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%r11,%r10)
movq 0x8(%rsp), %rdi
incl %ecx
movl %esi, %edx
incl %esi
movq (%rdi), %r8
movq %rsi, %r11
cmpq %rsi, %r8
jae 0x159a70
movl %ecx, %ecx
jmp 0x159b75
movq 0x10(%rsp), %r14
movq %rcx, (%rdi)
movq (%r14), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rdi, %rbx
callq 0xc405a
movq %r14, %rdi
movq %rbx, %rsi
movq 0x30(%rsp), %rdx
callq 0x1535c0
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, 0x18(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq