// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, %rax
movl $0x40, %ebp
subq 0x18(%rdi), %rbp
movq (%rdi), %rdi
movq %rax, 0x10(%rsp)
movq 0x10(%rax), %rcx
movq 0x10(%rcx), %rdx
addq $0x138, %rcx            # imm = 0x138
movq %rsi, 0x20(%rsp)
callq 0x13e8a1
movq %rbx, %rdi
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, 0x18(%rbx)
movq 0x18(%r14), %rax
testq %rax, %rax
movq %rbx, 0x8(%rsp)
jne 0x16022d
movq %r14, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x14547c
movq 0x8(%rsp), %rdi
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x1603ac
xorl %r8d, %r8d
movq %rbp, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq 0x28(%r14), %rcx
movq (%rcx,%r8,8), %r15
testl %r15d, %r15d
je 0x1603a0
movq %r15, %r12
shrq $0x1b, %r12
andq $-0x20, %r12
addq 0x10(%r14), %r12
movl $0x1, %r14d
xorl %r13d, %r13d
movq %r8, 0x18(%rsp)
movl %r13d, %eax
cmpl %r14d, %r15d
jne 0x160283
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rbx
addq %rbx, %rbx
movl %ebp, %ecx
shrq %cl, %rbx
jmp 0x1602b3
movl %r14d, %ecx
shlq $0x5, %rcx
movq 0x8(%r12,%rcx), %rdx
addq %rdx, %rdx
movl %ebp, %ecx
shrq %cl, %rdx
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rbx
addq %rbx, %rbx
shrq %cl, %rbx
cmpq %rbx, %rdx
jne 0x1602b3
movl %r13d, %eax
jmp 0x160388
movl %r14d, %eax
cmpq %r8, %rbx
je 0x160388
movq (%rdi), %rsi
movq 0x8(%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq %rcx, %rsi
jne 0x160308
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rdi)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rsi
movq 0x8(%rdi), %rcx
leaq 0x1(%rsi), %rdx
movq %rdx, (%rdi)
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movl %r8d, 0x8(%rax,%rsi)
movl %ebx, 0x14(%rax,%rsi)
movl %r14d, %ebp
subl %r13d, %ebp
movl %ebp, 0x18(%rax,%rsi)
cmpq %rcx, %rdx
jne 0x160364
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rdi)
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%rdi)
leaq (%rdx,%rdx,2), %rcx
shlq $0x4, %rcx
movl %ebx, 0x8(%rax,%rcx)
movl %r8d, 0x14(%rax,%rcx)
movl %ebp, 0x18(%rax,%rcx)
movl %r14d, %eax
movq 0x30(%rsp), %rbp
incl %r14d
movl %eax, %r13d
cmpl %r15d, %r14d
jbe 0x160268
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rax
incq %r8
cmpq %rax, %r8
jb 0x16023a
movq (%rdi), %rax
movq 0x10(%rdi), %rdi
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0x136a91
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
xorl %ecx, %ecx
testq %rax, %rax
je 0x1604d7
movq 0x10(%rdi), %rbx
movl $0x1, %esi
movl $0x1, %r8d
xorl %edx, %edx
movl %edx, %r9d
cmpq %r8, %rax
je 0x160424
leaq (%r9,%r9,2), %r10
shlq $0x4, %r10
movl 0x8(%rbx,%r10), %r11d
leaq (%r8,%r8,2), %r8
shlq $0x4, %r8
cmpl 0x8(%rbx,%r8), %r11d
movq 0x8(%rsp), %rdi
jne 0x160424
addq %rbx, %r10
addq %rbx, %r8
movl 0x14(%r10), %r10d
cmpl 0x14(%r8), %r10d
je 0x1604c7
xorl %r8d, %r8d
leaq (%r9,%r9,2), %rax
cmpl %esi, %edx
jae 0x160452
movl %esi, %edx
movq %rax, %r8
shlq $0x4, %r8
leaq (%rbx,%r8), %r10
addq $0x18, %r10
subq %r9, %rdx
xorl %r8d, %r8d
addl (%r10), %r8d
addq $0x30, %r10
decq %rdx
jne 0x160446
shlq $0x4, %rax
movl 0x8(%rbx,%rax), %edx
movl 0x14(%rbx,%rax), %r9d
movl %ecx, %r10d
shlq $0x4, %r10
leaq (%r10,%r10,2), %r10
movups 0x20(%rbx,%rax), %xmm0
movups %xmm0, 0x20(%rbx,%r10)
movups 0x10(%rbx,%rax), %xmm0
movups %xmm0, 0x10(%rbx,%r10)
movups (%rbx,%rax), %xmm0
movups %xmm0, (%rbx,%r10)
movq 0x10(%rdi), %rbx
movl %r8d, 0x18(%rbx,%r10)
imull $0x2710, %r8d, %eax     # imm = 0x2710
movq 0x40(%r14), %r8
movq (%r8,%rdx,8), %rdx
movq (%r8,%r9,8), %r8
cmpq %r8, %rdx
cmovbq %rdx, %r8
xorl %edx, %edx
divq %r8
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rbx,%r10)
incl %ecx
movq (%rdi), %rax
movl %esi, %edx
incl %esi
movq %rsi, %r8
cmpq %rsi, %rax
jae 0x1603e7
movl %ecx, %ecx
movq %rcx, (%rdi)
movq 0x10(%rsp), %r14
movq (%r14), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %rdi, %rbx
callq 0xc4edd
movq %r14, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x159df5
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