// File: /workspace/github_cpp_makefile_star2_O2/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 0x8c99a
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rcx
movl 0x14(%rcx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jae 0x105138
leaq (%r12,%r12), %rdx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq %r12, %rsi
shlq $0x4, %rsi
movq (%rcx,%rsi), %rsi
movl %esi, %edi
shrq $0x20, %rsi
imulq $0x18, %rdi, %r8
imulq $0x18, %rsi, %rsi
leaq (%rax,%rsi), %r9
addq $0xc, %r9
xorl %r10d, %r10d
xorl %esi, %esi
pushq $-0x1
popq %rdi
cmpq %r10, %r8
je 0x104e60
cmpl $0x0, (%r9,%r10)
js 0x104e49
incl %esi
movl -0x4(%r9,%r10), %r11d
movl %r11d, %r15d
shrl %r15d
cmpq %r15, %r12
jne 0x104e4e
movl %edi, %r11d
jmp 0x104e57
cmpl $-0x1, %edi
jne 0x104ee2
addq $0x18, %r10
movl %r11d, %edi
jmp 0x104e2b
movq %r13, %rbp
movq 0x8(%rcx,%rdx,8), %r8
movl %r8d, %r9d
shrq $0x20, %r8
imulq $0x18, %r9, %r9
imulq $0x18, %r8, %r8
leaq (%rax,%r8), %r10
addq $0xc, %r10
xorl %r15d, %r15d
xorl %r11d, %r11d
pushq $-0x1
popq %r8
cmpq %r15, %r9
je 0x104ebb
cmpl $0x0, (%r10,%r15)
js 0x104ea7
incl %r11d
movl -0x4(%r10,%r15), %r13d
movl %r13d, %ebx
shrl %ebx
cmpq %rbx, %r12
jne 0x104eac
movl %r8d, %r13d
jmp 0x104eb2
cmpl $-0x1, %r8d
jne 0x104ecf
addq $0x18, %r15
movl %r13d, %r8d
jmp 0x104e89
cmpl $0x2, %esi
setb %sil
cmpl $0x2, %r11d
setb %r9b
testb %r9b, %sil
je 0x104ed4
movq %rbp, %r13
jmp 0x104ee2
cmpl $-0x1, %edi
movq %rbp, %r13
jne 0x104eea
cmpl $-0x1, %r8d
jne 0x104eea
incq %r12
jmp 0x104ddc
movl %edi, %esi
xorl $0x1, %esi
cmpl $-0x1, %edi
cmovel %edi, %esi
movl %r8d, %r15d
xorl $0x1, %r15d
cmpl $-0x1, %r8d
cmovel %r8d, %r15d
cmpl $-0x1, %esi
je 0x104f60
movl %r15d, %edi
xorl $0x1, %edi
cmpl $-0x1, %r15d
cmovel %r15d, %edi
movl %esi, %r8d
movq (%rcx,%r8,8), %r8
movl %r8d, %r9d
movq %r8, %r10
shrq $0x20, %r10
imulq $0x18, %r9, %r8
imulq $0x18, %r10, %r9
addq %rax, %r9
addq $0xc, %r9
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x104f60
cmpl $0x0, (%r9,%r10)
js 0x104f5a
movl -0x4(%r9,%r10), %r11d
movl %r11d, %ebx
shrl %ebx
cmpq %rbx, %r12
je 0x104f5a
cmpl %edi, %r11d
jne 0x104ee2
addq $0x18, %r10
jmp 0x104f3a
cmpl $-0x1, %r15d
je 0x104fc8
movl %esi, %edi
xorl $0x1, %edi
cmpl $-0x1, %esi
cmovel %esi, %edi
movl %r15d, %r8d
movq (%rcx,%r8,8), %rcx
movl %ecx, %r8d
movq %rcx, %r9
shrq $0x20, %r9
imulq $0x18, %r8, %rcx
imulq $0x18, %r9, %r8
addq %r8, %rax
addq $0xc, %rax
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x104fc8
cmpl $0x0, (%rax,%r8)
js 0x104fc2
movl -0x4(%rax,%r8), %r9d
movl %r9d, %r10d
shrl %r10d
cmpq %r10, %r12
setne %r10b
cmpl %edi, %r9d
setne %r9b
testb %r10b, %r9b
jne 0x104ee2
addq $0x18, %r8
jmp 0x104f94
pushq $-0x1
popq %rbx
cmpl %ebx, %r15d
movl %esi, %r13d
cmovnel %r15d, %r13d
cmpl %ebx, %esi
cmovnel %esi, %r13d
cmpl %r15d, %r13d
cmovel %ebx, %r15d
orl $-0x1, 0x10(%rsp)
orl $-0x1, 0x14(%rsp)
orl $-0x1, 0xc(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0xc(%rsp), %r9
callq 0x10519d
cmpl %ebx, %r13d
je 0x10502e
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 0x10519d
cmpl $-0x1, %r15d
je 0x105053
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 0x10519d
movq 0x14df86(%rip), %rax    # 0x252fe0
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 0xe8c5e(%rip), %rsi     # 0x1edce1
movl %r13d, %r8d
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x22b0
popq %rcx
popq %rdx
movl 0xc(%rsp), %eax
leal (%rax,%rax,4), %esi
movl 0x14(%rsp), %edx
cmpl %edx, %esi
jae 0x104ecf
movl 0x10(%rsp), %ecx
cmpl %ecx, %esi
movq %rbp, %r13
jae 0x104ee2
imull $0xa, %eax, %esi
addl %edx, %ecx
cmpl %ecx, %esi
jae 0x104ee2
cmpq %r13, 0x18(%rsp)
jne 0x1050fd
leaq (,%r13,2), %rbx
testq %r13, %r13
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movl 0x10(%rsp), %ecx
movl 0xc(%rsp), %eax
addl 0x14(%rsp), %ecx
movq %rbx, %r13
subl %eax, %ecx
shlq $0x20, %rcx
orq %r12, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%r14,%rax,8)
incq %rax
movq %rax, 0x18(%rsp)
movq 0x14dec2(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xe80b2(%rip), %rdi     # 0x1ed1da
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0x104ee2
movq 0x18(%rsp), %rbx
leaq (%r14,%rbx,8), %rsi
movq %r14, %rdi
callq 0xf116c
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %rbx
jbe 0x10516f
movq (%r14,%rax,8), %rcx
movq %rax, %rdx
notq %rdx
addq %rbx, %rdx
movq (%r14,%rdx,8), %rsi
movq %rsi, (%r14,%rax,8)
movq %rcx, (%r14,%rdx,8)
incl %eax
jmp 0x10514b
movq 0x78(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x28(%rsp), %rdi
callq 0x6921d
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq