// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, 0x20(%rsp)
movq 0xc0(%rsi), %rdi
callq 0xc815c
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0x18(%rax), %rcx
testl $0x7fffffff, 0x14(%rcx) # imm = 0x7FFFFFFF
je 0x146b6c
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), %rsi
movq %rsi, %rdx
shlq $0x20, %rdx
setne %dl
je 0x146804
movq %rsi, %r8
shrq $0x20, %r8
movl %esi, %edi
leaq (%r8,%r8,2), %rsi
leaq (%rax,%rsi,8), %r9
addq $0xc, %r9
movq %rdi, %r10
negq %r10
xorl %r8d, %r8d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x1, %r11d
cmpl $0x0, (%r9)
js 0x1467d5
incl %r8d
movl -0x4(%r9), %ebx
movl %ebx, %r15d
shrl %r15d
cmpq %r15, %r12
jne 0x1467d9
movl %esi, %ebx
jmp 0x1467de
cmpl $-0x1, %esi
jne 0x1467fa
cmpq %rdi, %r11
setb %dl
addq $0x18, %r9
leaq (%r10,%r11), %r15
incq %r15
incq %r11
movl %ebx, %esi
cmpq $0x1, %r15
jne 0x1467bd
cmpl $0x2, %r8d
setb %dil
jmp 0x14680c
movb $0x1, %dil
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testb $0x1, %dl
je 0x14682e
incq %r12
movq 0x18(%r13), %rcx
movl 0x14(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x146775
jmp 0x146b78
leaq (%r12,%r12), %rdx
movq %rdx, 0x30(%rsp)
movq 0x8(%rcx,%rdx,8), %r8
movq %r8, %r9
shlq $0x20, %r9
setne %r9b
movq %rax, 0x38(%rsp)
je 0x1468c6
movq %r13, %rdx
movq %r8, %r11
shrq $0x20, %r11
movl %r8d, %r10d
leaq (%r11,%r11,2), %r8
leaq (%rax,%r8,8), %r15
addq $0xc, %r15
movq %r10, %r13
negq %r13
xorl %r11d, %r11d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x1, %ebx
cmpl $0x0, (%r15)
js 0x146891
incl %r11d
movl -0x4(%r15), %r8d
movl %r8d, %eax
shrl %eax
cmpq %rax, %r12
jne 0x146896
movl %ebp, %r8d
jmp 0x14689b
cmpl $-0x1, %ebp
jne 0x1468b9
cmpq %r10, %rbx
setb %r9b
addq $0x18, %r15
leaq (%rbx,%r13), %rax
incq %rax
incq %rbx
movl %r8d, %ebp
cmpq $0x1, %rax
jne 0x14687a
cmpl $0x2, %r11d
setb %r8b
movq %rdx, %r13
jmp 0x1468ce
movb $0x1, %r8b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb $0x1, %r9b
jne 0x146811
andb %r8b, %dil
jne 0x146811
cmpl $-0x1, %esi
jne 0x1468ef
cmpl $-0x1, %ebp
je 0x146811
movl %esi, %edi
xorl $0x1, %edi
cmpl $-0x1, %esi
cmovel %esi, %edi
movl %ebp, %r15d
xorl $0x1, %r15d
cmpl $-0x1, %ebp
cmovel %ebp, %r15d
cmpl $-0x1, %edi
movq 0x38(%rsp), %rdx
je 0x146974
movl %r15d, %esi
xorl $0x1, %esi
cmpl $-0x1, %r15d
cmovel %r15d, %esi
movl %edi, %eax
movq (%rcx,%rax,8), %r8
testl %r8d, %r8d
je 0x146974
movq %r8, %rax
shrq $0x20, %rax
movl %r8d, %r9d
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %r8
addq $0xc, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
cmpl $0x0, (%r8,%r10)
js 0x14696b
movl -0x4(%r8,%r10), %r11d
movl %r11d, %eax
shrl %eax
cmpq %rax, %r12
je 0x14696b
cmpl %esi, %r11d
jne 0x146811
addq $0x18, %r10
cmpq %r10, %r9
jne 0x14694c
cmpl $-0x1, %r15d
je 0x1469e2
movl %edi, %esi
xorl $0x1, %esi
cmpl $-0x1, %edi
cmovel %edi, %esi
movl %r15d, %eax
movq (%rcx,%rax,8), %rcx
testl %ecx, %ecx
je 0x1469e2
movq %rcx, %rax
shrq $0x20, %rax
movl %ecx, %ecx
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r8d, %r8d
cmpl $0x0, (%rax,%r8)
js 0x1469d9
movl -0x4(%rax,%r8), %r9d
movl %r9d, %r10d
shrl %r10d
cmpq %r10, %r12
setne %r10b
cmpl %esi, %r9d
setne %r9b
testb %r10b, %r9b
jne 0x146811
addq $0x18, %r8
cmpq %r8, %rcx
jne 0x1469b0
movq %r13, %rbx
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 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0xc(%rsp), %r9
callq 0x146be0
cmpl %ebp, %r13d
je 0x146a4f
movl %r13d, %edx
xorl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x14(%rsp), %r9
callq 0x146be0
cmpl $-0x1, %r15d
je 0x146a74
movl %r15d, %edx
xorl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x10(%rsp), %r9
callq 0x146be0
movq 0x143565(%rip), %rax    # 0x289fe0
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 0xe90fd(%rip), %rsi     # 0x22fba1
movl %r13d, %r8d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x22b0
addq $0x10, %rsp
movl 0xc(%rsp), %eax
leal (%rax,%rax,4), %esi
movl 0x14(%rsp), %ecx
cmpl %ecx, %esi
jae 0x146b64
movl 0x10(%rsp), %edx
cmpl %edx, %esi
movq %rbx, %r13
jae 0x146811
addl %eax, %eax
leal (%rax,%rax,4), %eax
addl %ecx, %edx
cmpl %edx, %eax
jae 0x146811
movq 0x28(%rsp), %rax
cmpq %rax, 0x18(%rsp)
jne 0x146b1b
leaq (%rax,%rax), %rbx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rbx, 0x28(%rsp)
movl 0x10(%rsp), %eax
addl 0x14(%rsp), %eax
subl 0xc(%rsp), %eax
shlq $0x20, %rax
orq %r12, %rax
movq 0x18(%rsp), %rcx
movq %rax, (%r14,%rcx,8)
incq %rcx
movq %rcx, 0x18(%rsp)
movq 0x14349a(%rip), %rax    # 0x289fe0
movq (%rax), %rcx
movl $0x7, %esi
movl $0x1, %edx
leaq 0xe8540(%rip), %rdi     # 0x22f09a
callq 0x2410
jmp 0x146811
movq %rbx, %r13
jmp 0x146811
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rbx
leaq (%r14,%rbx,8), %rsi
movq %r14, %rdi
callq 0x13180d
testq %rbx, %rbx
je 0x146bb1
leaq -0x1(%rbx), %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, %rbx
ja 0x146b94
movq 0x88(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq %r13, %rdi
callq 0xa0004
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq