// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: append_boundary_chain(ma_ug_t*, hc_links*, bubble_type*)
void append_boundary_chain(ma_ug_t* ug, hc_links* link, bubble_type* bub)
{
    double index_time = yak_realtime();
    ma_ug_t *bs_ug = bub->b_ug;
    uint32_t v, u, i, k, beg_idx, m, nv, n_vx, flag_pri = 1, flag_aux = 2, flag_ava = 4;
    uint32_t root, rId_0, ori_0, root_0, new_bub;
    asg_arc_t *av = NULL;
    n_vx = bs_ug->g->n_seq << 1;
    uint8_t *vis = NULL; CALLOC(vis, ug->g->n_seq<<1);
    uint8_t *is_vis = NULL; CALLOC(is_vis, ug->g->n_seq<<1);
    uint8_t *is_used = NULL; CALLOC(is_used, n_vx);
    uint8_t *dedup = NULL; CALLOC(dedup, ug->g->n_seq<<1);
    buf_t b; memset(&b, 0, sizeof(buf_t));
    kvec_t_u32_warp stack, result, res_utg;
    kv_init(stack.a); kv_init(result.a); kv_init(res_utg.a);
    kvec_asg_arc_t_warp edges; kv_init(edges.a);

    for (i = 0; i < bs_ug->g->n_seq; i++)
    {
        set_b_utg_weight_flag(bub, &b, i<<1, vis, flag_aux, NULL);
    }

    if(bub->num.n > 0) bub->num.n--;
    new_bub = bub->b_g->n_seq;
    for (v = 0; v < n_vx; v++)
    {
        av = asg_arc_a(bs_ug->g, v);
        nv = asg_arc_n(bs_ug->g, v);
        if(nv == 0 || get_real_length(bs_ug->g, v, NULL) == 0) continue;
        res_utg.a.n = 0;
        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            u = av[i].v^1;
            /**
            beg_idx = res_utg.a.n;
            set_b_utg_weight_flag_xor(bub, bs_ug, &b, v^1, vis, flag_pri, NULL);
            get_chain_weight(bub, bs_ug, &b, u^1, v, link, vis, is_vis, ug, &stack, &result, flag_pri, flag_ava, &res_utg, NULL);
            set_b_utg_weight_flag_xor(bub, bs_ug, &b, v^1, vis, flag_pri, NULL);
            for (k = m = beg_idx; k < res_utg.a.n; k++)
            {
                if(dedup[res_utg.a.a[k]>>1] != 0) continue;
                dedup[res_utg.a.a[k]>>1] = 1;
                res_utg.a.a[m] = res_utg.a.a[k];
                m++;
            }
            res_utg.a.n = m;
            **/
            beg_idx = res_utg.a.n;
            set_b_utg_weight_flag_xor(bub, bs_ug, &b, u^1, vis, flag_pri, NULL);
            get_chain_weight(bub, bs_ug, &b, v^1, u, link, vis, is_vis, ug, &stack, &result, flag_pri, flag_ava, &res_utg, NULL);
            set_b_utg_weight_flag_xor(bub, bs_ug, &b, u^1, vis, flag_pri, NULL);
            for (k = m = beg_idx; k < res_utg.a.n; k++)
            {
                if(dedup[res_utg.a.a[k]>>1] != 0) continue;
                dedup[res_utg.a.a[k]>>1] = 1;
                res_utg.a.a[m] = res_utg.a.a[k];
                m++;
            }
            res_utg.a.n = m;
        }

        for (k = 0; k < res_utg.a.n; k++) dedup[res_utg.a.a[k]>>1] = 0;
        /*******************************for debug************************************/
        // for (i = 0; i < res_utg.a.n; i++)
        // {
        //     for (k = 0; k < res_utg.a.n; k++)
        //     {
        //         if(k == i) continue;
        //         if((res_utg.a.a[i]>>1) == (res_utg.a.a[k]>>1)) fprintf(stderr, "ERROR\n");
        //     }
        // }
        /*******************************for debug************************************/

        root = get_utg_end_from_btg(bub, bs_ug, v);
        rId_0 = root>>1;
        ori_0 = root&1;
        get_bubbles(bub, rId_0, ori_0 == 1?&root_0:NULL, ori_0 == 0?&root_0:NULL, NULL, NULL, NULL);

        if(root_0 != (uint32_t)-1 && (!IF_HOM(root_0>>1, *bub))) kv_push(uint32_t, res_utg.a, root_0);

        if(v&1)
        {
            update_bubble_graph(&res_utg, root_0^1, rId_0, (uint32_t)-1, (uint32_t)-1, bub, &edges, bub->b_g, NULL, NULL, ug, NULL, 0);
        }
        else
        {
            update_bubble_graph(&res_utg, (uint32_t)-1, (uint32_t)-1, root_0^1, rId_0, bub, &edges, bub->b_g, NULL, NULL, ug, NULL, 0);
        }
    }
    kv_push(uint32_t, bub->num, bub->list.n);
    new_bub = bub->b_g->n_seq - new_bub;
    bub->mess_bub += new_bub;
    ///actually not useful, and may have bug when one bubble at multipe chains
    if(new_bub) update_bub_b_s_idx(bub);


    for (v = 0; v < n_vx; v++)
    {
        av = asg_arc_a(bs_ug->g, v);
        nv = asg_arc_n(bs_ug->g, v);
        if(nv == 0 || get_real_length(bs_ug->g, v, NULL) == 0) continue;
        drop_g_edges_by_utg(bub, bub->b_g, bs_ug, NULL, v, (uint32_t)-1);
    }

    update_bsg(bub->b_g, &edges);

    ma_ug_destroy(bs_ug);
    bs_ug = ma_ug_gen(bub->b_g);
    bub->b_ug = bs_ug;
    kv_destroy(bub->chain_weight);
    ma_utg_t *u_x = NULL;
    bs_ug = bub->b_ug;
    kv_malloc(bub->chain_weight, bs_ug->u.n); bub->chain_weight.n = bs_ug->u.n;
    for (i = 0; i < bs_ug->u.n; i++)
    {
        u_x = &(bs_ug->u.a[i]);
        bub->chain_weight.a[i].id = i;
        // if(u->n <= 1) ///not a chain
        // {
        //     bub->chain_weight.a[i].b_occ = bub->chain_weight.a[i].g_occ = 0;
        //     bub->chain_weight.a[i].del = 1;
        // } 
        // else
        {
            bub->chain_weight.a[i].del = 0;
            calculate_chain_weight(u_x, bub, ug, &(bub->chain_weight.a[i]));
        }
    }
    qsort(bub->chain_weight.a, bub->chain_weight.n, sizeof(chain_w_type), cmp_chain_weight);



    free(vis); free(is_vis); free(is_used); free(dedup); free(b.b.a);
    kv_destroy(stack.a); kv_destroy(result.a); kv_destroy(res_utg.a);
    kv_destroy(edges.a);


    /*******************************for debug************************************/
    // for (v = 0; v < (uint32_t)(bs_ug->g->n_seq<<1); v++)
    // {
    //     if(asg_arc_n(bs_ug->g, v) > 0) fprintf(stderr, "ERROR, btg%.6ul\n", (v>>1)+1);
    //     ma_utg_t *utg = &(bs_ug->u.a[v>>1]);
    //     for (i = 0; i < utg->n; i++)
    //     {
    //         if((utg->a[i]>>33) >= 
    //                 (bub->f_bub + bub->b_bub + bub->b_end_bub + bub->tangle_bub + bub->cross_bub))
    //         {
    //             if(i != 0 && i != utg->n - 1) fprintf(stderr, "ERROR, btg%.6ul, i: %u\n", (v>>1)+1, i);
    //         }
    //     }
    // }
    /*******************************for debug************************************/
    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %r14
movq %rsi, 0x90(%rsp)
movq %rdi, %rbx
callq 0x1365ef
movsd %xmm0, 0x88(%rsp)
movq %r14, 0x18(%rsp)
movq 0xc0(%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rax), %rax
movl 0x14(%rax), %ebp
movq %rbx, 0x58(%rsp)
movq 0x18(%rbx), %rax
movl 0x14(%rax), %eax
addl %eax, %eax
movslq %eax, %r15
pushq $0x1
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, 0x78(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x150(%rsp)
movapd %xmm0, 0x140(%rsp)
movapd %xmm0, 0x130(%rsp)
andq $0x0, 0x160(%rsp)
movapd %xmm0, 0x120(%rsp)
andq $0x0, 0xf0(%rsp)
movapd %xmm0, 0x110(%rsp)
andq $0x0, 0xd0(%rsp)
movapd %xmm0, 0x100(%rsp)
andq $0x0, 0x40(%rsp)
movapd %xmm0, 0xe0(%rsp)
andq $0x0, 0xb0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0xa0(%rsp)
movl %ebp, 0xc(%rsp)
testl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
je 0x1775a
xorl %r15d, %r15d
pushq $0x2
popq %r13
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movl %r15d, %edx
movq %r14, %rcx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x14d241
incl %ebp
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl %ebx, %eax
addl $0x2, %r15d
cmpl %eax, %ebp
jb 0x17724
movq %r14, %rax
movl 0xc(%rsp), %edx
addl %edx, %edx
movq 0x18(%rsp), %rbx
movq 0x30(%rbx), %rcx
testq %rcx, %rcx
jne 0x17775
xorl %ecx, %ecx
jmp 0x1777c
decq %rcx
movq %rcx, 0x30(%rbx)
movq 0x10(%rsp), %r14
movq %rax, %rdi
movq 0xb8(%rbx), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movl %esi, 0x24(%rsp)
movl %edx, %eax
movq %rax, 0x68(%rsp)
movl %edx, 0xc(%rsp)
testl %edx, %edx
movq %rdi, 0x50(%rsp)
movq 0x58(%rsp), %r13
je 0x17b19
xorl %r9d, %r9d
movq %r12, 0x70(%rsp)
movq 0x18(%r14), %rcx
movq 0x28(%rcx), %rax
movq (%rax,%r9,8), %rax
testl %eax, %eax
je 0x17b07
movq %rax, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rdx
je 0x17b07
movq 0x8(%rcx), %rcx
movq %rax, %rsi
shrq $0x20, %rsi
imulq $0x18, %rsi, %rsi
leaq (%rcx,%rsi), %r10
addq %rsi, %rcx
addq $0xc, %rcx
imulq $0x18, %rdx, %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x17803
testl %edi, %edi
je 0x17b07
andq $0x0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movl %r9d, %ecx
xorl $0x1, %ecx
movl %ecx, 0x60(%rsp)
movl %eax, %eax
movq %rax, 0x98(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r10, 0x80(%rsp)
imulq $0x18, %r15, %rax
cmpl $0x0, 0xc(%r10,%rax)
js 0x1797b
addq %r10, %rax
movl 0x8(%rax), %ecx
movl %ecx, 0x64(%rsp)
movl %ecx, %r13d
xorl $0x1, %r13d
subq $0x8, %rsp
movq 0x20(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
leaq 0x108(%rsp), %rdx
movq 0x58(%rsp), %rbp
movq %rbp, %r8
pushq $0x1
popq %r9
pushq $0x0
callq 0x1500a5
popq %rax
popq %rcx
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x108(%rsp), %rdx
movl 0x68(%rsp), %ecx
movl %r13d, %r8d
movq 0x60(%rsp), %r13
movq 0x98(%rsp), %r9
pushq $0x0
leaq 0x40(%rsp), %rax
pushq %rax
pushq $0x4
pushq $0x1
leaq 0xe8(%rsp), %rax
pushq %rax
leaq 0x110(%rsp), %rax
pushq %rax
pushq %r13
pushq 0xb8(%rsp)
pushq %rbp
callq 0x1505fe
addq $0x48, %rsp
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x108(%rsp), %rdx
movl 0x6c(%rsp), %ecx
movq %rbp, %r8
pushq $0x1
popq %r9
pushq $0x0
callq 0x1500a5
addq $0x10, %rsp
movq 0x30(%rsp), %rax
movl %r14d, %esi
cmpq %rsi, %rax
jbe 0x17966
movq 0x40(%rsp), %rcx
leal 0x1(%r14), %edx
movq 0x70(%rsp), %r12
movq 0x80(%rsp), %r10
movl (%rcx,%rsi,4), %esi
movl %esi, %edi
shrl %edi
cmpb $0x0, (%r12,%rdi)
jne 0x1795b
movb $0x1, (%r12,%rdi)
movl %r14d, %edi
movl %esi, (%rcx,%rdi,4)
incl %r14d
movl %edx, %esi
incl %edx
cmpq %rsi, %rax
ja 0x1793f
jmp 0x17973
movq 0x70(%rsp), %r12
movq 0x80(%rsp), %r10
movl %r14d, %r14d
movq %r14, 0x30(%rsp)
incq %r15
cmpq 0x98(%rsp), %r15
jne 0x1784f
testq %r14, %r14
je 0x179ac
movq 0x40(%rsp), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
shrl %edx
movb $0x0, (%r12,%rdx)
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r14
ja 0x17998
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rsp), %rsi
movl %esi, %ecx
shrl %ecx
imulq $0x28, %rcx, %rdx
movq 0x18(%rax,%rdx), %rcx
testb $0x1, %sil
movq 0x18(%rsp), %rbx
jne 0x179e0
addq %rdx, %rax
movl 0x10(%rax), %eax
decl %eax
movl 0x4(%rcx,%rax,8), %ebp
jmp 0x179e6
movl 0x4(%rcx), %ebp
xorl $0x1, %ebp
movl %ebp, %eax
notl %eax
shrl %ebp
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
andl $0x1, %eax
addl (%rdx,%rbp,4), %eax
movl (%rcx,%rax,4), %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r15
je 0x17a69
movl %r15d, %eax
shrl %eax
movq (%rbx), %rcx
movq 0x80(%rbx), %rdx
movl (%rcx,%rax,4), %eax
incq %rdx
cmpq %rax, %rdx
jb 0x17a69
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %r14
jne 0x17a57
leaq (%r14,%r14), %rsi
testq %r14, %r14
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x38(%rsp)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x40(%rsp)
movq 0x30(%rsp), %r14
leaq 0x1(%r14), %rcx
movq %rcx, 0x30(%rsp)
movl %r15d, (%rax,%r14,4)
movq 0x18(%rsp), %rbx
xorl $0x1, %r15d
movq 0xb8(%rbx), %rax
testb $0x1, 0x28(%rsp)
jne 0x17ab7
subq $0x40, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movupd %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x70(%rsp), %rdi
pushq $-0x1
popq %rdx
movl %edx, %esi
movl %r15d, %ecx
movl %ebp, %r8d
jmp 0x17af1
subq $0x40, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movupd %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x70(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
pushq $-0x1
popq %rcx
movl %ecx, %r8d
movq %rbx, %r9
callq 0x14c338
addq $0x40, %rsp
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r9
incq %r9
cmpq 0x68(%rsp), %r9
jne 0x177bc
movq 0x30(%rbx), %rcx
cmpq 0x38(%rbx), %rcx
jne 0x17b48
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x38(%rbx)
movq 0x40(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x40(%rbx)
movq 0x30(%rbx), %rcx
jmp 0x17b4c
movq 0x40(%rbx), %rax
movl 0xc(%rsp), %ebp
movl 0x24(%rsp), %edi
movl 0x18(%rbx), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x30(%rbx)
movl %edx, (%rax,%rcx,4)
movq 0xb8(%rbx), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %ecx
movl %ecx, %eax
subl %edi, %eax
addq %rax, 0xa8(%rbx)
cmpl %edi, %ecx
je 0x17b88
movq %rbx, %rdi
callq 0x13f2d9
testl %ebp, %ebp
je 0x17c04
xorl %r15d, %r15d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
pushq $-0x1
popq %r13
movq 0x18(%r14), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%r15,8), %rax
movq %rax, %rcx
andq %rbx, %rcx
je 0x17bfa
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
imulq $0x18, %rax, %rax
addq %rdx, %rax
addq $0xc, %rax
imulq $0x18, %rcx, %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x17bc7
testl %esi, %esi
je 0x17bfa
movq 0x18(%rsp), %rax
movq 0xb8(%rax), %rsi
movq %r14, %rdx
xorl %ecx, %ecx
movl %r15d, %r8d
movl %r13d, %r9d
callq 0x14e373
incq %r15
cmpq 0x68(%rsp), %r15
jne 0x17b98
movq 0x18(%rsp), %r15
movq 0xb8(%r15), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x14e73d
movq %r14, %rdi
callq 0xa0761
movq 0xb8(%r15), %rdi
callq 0xb10cd
movq %rax, 0xc0(%r15)
movq 0xd8(%r15), %rdi
callq 0x2220
movq 0xc0(%r15), %rbx
movq (%rbx), %r14
movq %r14, 0xd0(%r15)
movq %r14, %rdi
shlq $0x5, %rdi
callq 0x2280
movq %rax, 0xd8(%r15)
movq %r14, 0xc8(%r15)
testq %r14, %r14
jne 0x17c7d
xorl %esi, %esi
movq 0x50(%rsp), %r14
jmp 0x17cd2
pushq $0x1
popq %rbp
xorl %eax, %eax
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %r13
imulq $0x28, %rax, %rdi
addq 0x10(%rbx), %rdi
movq 0xd8(%r15), %rdx
movq %rax, %rsi
shlq $0x5, %rsi
leaq (%rdx,%rsi), %rcx
movq %rax, 0x10(%rdx,%rsi)
movb $0x0, 0x18(%rdx,%rsi)
movq %r15, %rsi
movq %r13, %rdx
callq 0x14cc0f
movl %ebp, %eax
incl %ebp
cmpq %rax, (%rbx)
ja 0x17c8c
movq 0xc8(%r15), %rsi
movq 0xd8(%r15), %rax
leaq 0x1350b9(%rip), %rcx    # 0x14cd92
pushq $0x20
popq %rdx
movq %rax, %rdi
callq 0x23f0
movq %r14, %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
movq 0x148(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
movq 0xd0(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x27d29d(%rip), %rax    # 0x294fe0
movq (%rax), %rbx
callq 0x1365ef
subsd 0x88(%rsp), %xmm0
leaq 0x223e90(%rip), %rsi    # 0x23bbeb
leaq 0x2241ab(%rip), %rdx    # 0x23bf0d
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq