// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: resolve_bubble_chain_tangle_back(ma_ug_t*, bubble_type*, hc_links*)
void resolve_bubble_chain_tangle_back(ma_ug_t* ug, bubble_type* bub, hc_links* link)
{
    ma_ug_t *copy_ug = copy_untig_graph(bub->b_ug);
    asg_arc_t *av = NULL;
    uint32_t i, j, k, v, w, w1, w2, nw1, nw2, nv, occ_e_1, occ_e_2, occ_c;
    ma_ug_t *bub_ug = copy_ug;
    ///ma_utg_t *u = NULL;
    buf_t b; memset(&b, 0, sizeof(buf_t));
    kvec_t_u32_warp stack, result;
    kv_init(stack.a); kv_init(result.a);
    uint8_t *vis = NULL; CALLOC(vis, ug->g->n_seq<<1);
    uint8_t *is_vis = NULL; CALLOC(is_vis, ug->g->n_seq<<1);
    kvec_t(uint64_t) occ_idx; kv_init(occ_idx); uint64_t tmp, *p = NULL;

    for (k = occ_idx.n = 0; k < bub_ug->g->n_seq; k++)
    {
        v = (k<<1);
        av = asg_arc_a(bub_ug->g, v);
        nv = asg_arc_n(bub_ug->g, v);
        for (i = 0, w = (uint32_t)-1, nw1 = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nw1++;
            if((av[i].v>>1) == (v>>1)) continue;
            if(w != (uint32_t)-1) break;
            w = av[i].v;
        }
        if(i < nv) continue;
        w1 = w;


        v = (k<<1)+1;
        av = asg_arc_a(bub_ug->g, v);
        nv = asg_arc_n(bub_ug->g, v);
        for (i = 0, w = (uint32_t)-1, nw2 = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nw2++;
            if((av[i].v>>1) == (v>>1)) continue;
            if(w != (uint32_t)-1) break;
            w = av[i].v;
        }
        if(i < nv) continue;
        w2 = w;

        if(nw1 <= 1 && nw2 <= 1) continue;
        if(w1 == (uint32_t)-1 && w2 == (uint32_t)-1) continue;
        if(w1 != (uint32_t)-1) w1 ^=1;
        if(w2 != (uint32_t)-1) w2 ^=1;

        if(w1 != (uint32_t)-1)
        {   
            w = (uint32_t)-1;
            if(w2 != (uint32_t)-1) w = w2^1;

            av = asg_arc_a(bub_ug->g, w1);
            nv = asg_arc_n(bub_ug->g, w1);
            for (i = 0; i < nv; i++)
            {
                if(av[i].del) continue;
                if((av[i].v>>1) == k) continue;
                if(av[i].v == w) continue;
                break;
            }
            if(i < nv) continue;
        }


        if(w2 != (uint32_t)-1)
        {   
            w = (uint32_t)-1;
            if(w1 != (uint32_t)-1) w = w1^1;

            av = asg_arc_a(bub_ug->g, w2);
            nv = asg_arc_n(bub_ug->g, w2);
            for (i = 0; i < nv; i++)
            {
                if(av[i].del) continue;
                if((av[i].v>>1) == k) continue;
                if(av[i].v == w) continue;
                break;
            }
            if(i < nv) continue;
        }

        
        if(w1 == (uint32_t)-1 && w2 != (uint32_t)-1) w1 = w2;
        if(w1 == w2) w2 = (uint32_t)-1;

        occ_c = occ_e_1 = occ_e_2 = (uint32_t)-1;
        set_b_utg_weight_flag(bub, &b, k<<1, NULL, 0, &occ_c);
        if(w1 != (uint32_t)-1) set_b_utg_weight_flag(bub, &b, w1^1, NULL, 0, &occ_e_1);
        if(w2 != (uint32_t)-1) set_b_utg_weight_flag(bub, &b, w2^1, NULL, 0, &occ_e_2);

        fprintf(stderr, "\n>>>>>>k=btg%.6ul (n=%u), w1=btg%.6ul (n=%u), w2=utg%.6ul (n=%u)\n", k+1, occ_c, 
                (w1>>1)+1, occ_e_1, (w2>>1)+1, occ_e_2);

        if(occ_c*5 >= occ_e_1) continue;
        if(occ_c*5 >= occ_e_2) continue;
        if(occ_c*10 >= (occ_e_1 + occ_e_2)) continue;
        kv_pushp(uint64_t, occ_idx, &p);
        (*p) = occ_e_1 + occ_e_2 - occ_c;
        (*p) <<= 32; (*p) += k;
        fprintf(stderr, "passed\n");
    }

    radix_sort_hc64(occ_idx.a, occ_idx.a + occ_idx.n);
    
    for (k = 0; k < occ_idx.n; ++k) 
    {
        tmp = occ_idx.a[k];
        occ_idx.a[k] = occ_idx.a[occ_idx.n - k - 1];
        occ_idx.a[occ_idx.n - k - 1] = tmp;
    }

    for (j = 0; j < occ_idx.n; j++)
    {
        k = (uint32_t)occ_idx.a[j];

        v = (k<<1);
        av = asg_arc_a(bub_ug->g, v);
        nv = asg_arc_n(bub_ug->g, v);
        for (i = 0, w = (uint32_t)-1, nw1 = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nw1++;
            if((av[i].v>>1) == (v>>1)) continue;
            if(w != (uint32_t)-1) break;
            w = av[i].v;
        }
        if(i < nv) continue;
        w1 = w;


        v = (k<<1)+1;
        av = asg_arc_a(bub_ug->g, v);
        nv = asg_arc_n(bub_ug->g, v);
        for (i = 0, w = (uint32_t)-1, nw2 = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nw2++;
            if((av[i].v>>1) == (v>>1)) continue;
            if(w != (uint32_t)-1) break;
            w = av[i].v;
        }
        if(i < nv) continue;
        w2 = w;

        if(nw1 <= 1 && nw2 <= 1) continue;
        if(w1 == (uint32_t)-1 && w2 == (uint32_t)-1) continue;
        if(w1 != (uint32_t)-1) w1 ^=1;
        if(w2 != (uint32_t)-1) w2 ^=1;



    }

    free(vis); free(is_vis); free(b.b.a); kv_destroy(occ_idx); kv_destroy(stack.a); kv_destroy(result.a);
    ma_ug_destroy(copy_ug);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, 0x20(%rsp)
movq 0xc0(%rsi), %rdi
callq 0xc8eab
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x18(%rax), %rcx
testl $0x7fffffff, 0x14(%rcx) # imm = 0x7FFFFFFF
je 0x14d209
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r14d, %r14d
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq %r12, %rdx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rdx
movb $0x1, %dil
movb $0x1, %r8b
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testl %edx, %edx
je 0x14cec8
movq %rdx, %rsi
shrq $0x20, %rsi
movl %edx, %r8d
leaq (%rsi,%rsi,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
xorl %r9d, %r9d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
cmpl $0x0, (%rdx,%r9)
js 0x14ceb7
incl %r10d
movl -0x4(%rdx,%r9), %r11d
movl %r11d, %r15d
shrl %r15d
cmpq %r15, %r12
je 0x14ceb7
cmpl $-0x1, %esi
movl %r11d, %esi
jne 0x14cf4b
addq $0x18, %r9
cmpq %r9, %r8
jne 0x14ce91
cmpl $0x2, %r10d
setb %r8b
leaq (%r12,%r12), %rdx
movq 0x8(%rcx,%rdx,8), %r9
testl %r9d, %r9d
je 0x14cf35
movq %r9, %rdi
shrq $0x20, %rdi
movl %r9d, %r9d
leaq (%rdi,%rdi,2), %rdi
leaq (%rax,%rdi,8), %rdi
addq $0xc, %rdi
shlq $0x3, %r9
leaq (%r9,%r9,2), %r10
xorl %r11d, %r11d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
cmpl $0x0, (%rdi,%r11)
js 0x14cf22
incl %r15d
movl -0x4(%rdi,%r11), %r13d
movl %r13d, %ebp
shrl %ebp
cmpq %rbp, %r12
je 0x14cf22
cmpl $-0x1, %r9d
movl %r13d, %r9d
jne 0x14cf4b
addq $0x18, %r11
cmpq %r11, %r10
jne 0x14cf00
cmpl $0x2, %r15d
setb %dil
jmp 0x14cf3b
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
testb %dil, %r8b
jne 0x14cf4b
cmpl $-0x1, %esi
jne 0x14cf68
cmpl $-0x1, %r9d
jne 0x14cf68
incq %r12
movq 0x18(%rbx), %rcx
movl 0x14(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x14ce46
jmp 0x14d1ce
movl %esi, %edi
xorl $0x1, %edi
cmpl $-0x1, %esi
cmovel %esi, %edi
movl %r9d, %r15d
xorl $0x1, %r15d
cmpl $-0x1, %r9d
cmovel %r9d, %r15d
cmpl $-0x1, %edi
je 0x14cfeb
movl %r15d, %esi
xorl $0x1, %esi
cmpl $-0x1, %r15d
cmovel %r15d, %esi
movl %edi, %r8d
movq (%rcx,%r8,8), %r8
testl %r8d, %r8d
je 0x14cfeb
movq %r8, %r9
shrq $0x20, %r9
movl %r8d, %r10d
leaq (%r9,%r9,2), %r8
leaq (%rax,%r8,8), %r8
addq $0xc, %r8
shlq $0x3, %r10
leaq (%r10,%r10,2), %r9
xorl %r10d, %r10d
cmpl $0x0, (%r8,%r10)
js 0x14cfe2
movl -0x4(%r8,%r10), %r11d
movl %r11d, %r13d
shrl %r13d
cmpq %r13, %r12
je 0x14cfe2
cmpl %esi, %r11d
jne 0x14cf4b
addq $0x18, %r10
cmpq %r10, %r9
jne 0x14cfc2
cmpl $-0x1, %r15d
je 0x14d059
movl %edi, %esi
xorl $0x1, %esi
cmpl $-0x1, %edi
cmovel %edi, %esi
movl %r15d, %r8d
movq (%rcx,%r8,8), %rcx
testl %ecx, %ecx
je 0x14d059
movq %rcx, %r8
shrq $0x20, %r8
movl %ecx, %ecx
leaq (%r8,%r8,2), %r8
leaq (%rax,%r8,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r8d, %r8d
cmpl $0x0, (%rax,%r8)
js 0x14d050
movl -0x4(%rax,%r8), %r9d
movl %r9d, %r10d
shrl %r10d
cmpq %r10, %r12
setne %r10b
cmpl %esi, %r9d
setne %r9b
testb %r10b, %r9b
jne 0x14cf4b
addq $0x18, %r8
cmpq %r8, %rcx
jne 0x14d027
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl %ebp, %r15d
movl %edi, %r13d
cmovnel %r15d, %r13d
cmpl %ebp, %edi
cmovnel %edi, %r13d
cmpl %r15d, %r13d
cmovel %ebp, %r15d
movl %ebp, 0x10(%rsp)
movl %ebp, 0x14(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0xc(%rsp), %r9
callq 0x14d241
cmpl %ebp, %r13d
je 0x14d0be
movl %r13d, %edx
xorl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x14(%rsp), %r9
callq 0x14d241
cmpl $-0x1, %r15d
je 0x14d0e3
movl %r15d, %edx
xorl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x10(%rsp), %r9
callq 0x14d241
movq 0x147ef6(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movl 0xc(%rsp), %ecx
shrl %r13d
incl %r13d
movl 0x14(%rsp), %r9d
shrl %r15d
incl %r15d
movl 0x10(%rsp), %r10d
leal 0x1(%r12), %edx
leaq 0xeecde(%rip), %rsi     # 0x23bdf1
movl %r13d, %r8d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x22b0
addq $0x10, %rsp
movl 0xc(%rsp), %ecx
leal (%rcx,%rcx,4), %esi
movl 0x14(%rsp), %edx
cmpl %edx, %esi
jae 0x14cf4b
movl 0x10(%rsp), %eax
cmpl %eax, %esi
jae 0x14cf4b
leal (%rcx,%rcx), %esi
leal (%rsi,%rsi,4), %esi
addl %edx, %eax
cmpl %eax, %esi
jae 0x14cf4b
movq 0x18(%rsp), %r15
cmpq 0x28(%rsp), %r15
jne 0x14d194
leaq (%r15,%r15), %rcx
testq %r15, %r15
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rsp)
leaq (,%rcx,8), %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
addl 0x14(%rsp), %eax
subl %ecx, %eax
shlq $0x20, %rax
orq %r12, %rax
movq %rax, (%r14,%r15,8)
incq %r15
movq %r15, 0x18(%rsp)
movq 0x147e30(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0x7, %esi
movl $0x1, %edx
leaq 0xee126(%rip), %rdi     # 0x23b2ea
callq 0x2410
jmp 0x14cf4b
movq 0x18(%rsp), %r15
leaq (%r14,%r15,8), %rsi
movq %r14, %rdi
callq 0x1379f9
testq %r15, %r15
je 0x14d215
leaq -0x1(%r15), %rax
xorl %ecx, %ecx
movq (%r14,%rcx,8), %rdx
movq (%r14,%rax,8), %rsi
movq %rsi, (%r14,%rcx,8)
movq %rdx, (%r14,%rax,8)
incq %rcx
movl %ecx, %edx
decq %rax
cmpq %rdx, %r15
ja 0x14d1ea
jmp 0x14d215
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x1379f9
movq 0x78(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0761
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq