// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: append_boundary_chain_hic(ma_ug_t*, kv_u_trans_t*, bubble_type*)
void append_boundary_chain_hic(ma_ug_t* ug, kv_u_trans_t *ta, 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, u^1, vis, flag_pri, NULL);
            get_chain_weight_hic(bub, bs_ug, &b, v^1, u, ta, 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;

        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);
    // 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 $0x158, %rsp            # imm = 0x158
movq %rdx, %r15
movq %rsi, 0x88(%rsp)
movq 0xc0(%rdx), %r12
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
movl %eax, 0xc(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x18(%rdi), %rax
movl 0x14(%rax), %eax
addl %eax, %eax
movslq %eax, %r14
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r13
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x70(%rsp)
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movq %r14, 0x150(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movq %r14, 0xe0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movq %r14, 0xc0(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movq %r14, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0x18(%r12), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x154977
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movq %r15, %rdi
leaq 0xf0(%rsp), %rsi
movl %r14d, %edx
movq %r13, %rcx
movl $0x2, %r8d
xorl %r9d, %r9d
callq 0x146be0
incl %ebp
movq 0x18(%r12), %rax
movl 0x14(%rax), %eax
andl %ebx, %eax
addl $0x2, %r14d
cmpl %eax, %ebp
jb 0x154944
movq %r12, (%rsp)
movl 0xc(%rsp), %ecx
addl %ecx, %ecx
movq 0x30(%r15), %rax
testq %rax, %rax
je 0x154991
decq %rax
movq %rax, 0x30(%r15)
movq 0xb8(%r15), %rax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %edx
movl %edx, 0x5c(%rsp)
movl %ecx, %eax
movq %rax, 0x68(%rsp)
movl %ecx, 0xc(%rsp)
testl %ecx, %ecx
movq 0x38(%rsp), %r14
movq (%rsp), %rdi
je 0x154d64
xorl %r8d, %r8d
movq %r15, 0x48(%rsp)
movq %r13, 0x50(%rsp)
movq 0x18(%rdi), %rax
movq 0x28(%rax), %rcx
movq (%rcx,%r8,8), %r9
testl %r9d, %r9d
je 0x154d56
movq %r9, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0x154d56
movq 0x8(%rax), %rax
movq %r9, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r10
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%r10,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x154a12
testl %edx, %edx
je 0x154d56
movq $0x0, 0x10(%rsp)
movq %r8, 0x30(%rsp)
movl %r8d, %eax
xorl $0x1, %eax
movl %eax, 0x60(%rsp)
cmpl $0x1, %r9d
adcl $0x0, %r9d
xorl %r12d, %r12d
movq %r9, 0x80(%rsp)
movq %r10, 0x78(%rsp)
leaq (%r12,%r12,2), %rax
cmpl $0x0, 0xc(%r10,%rax,8)
js 0x154ba3
leaq (%r10,%rax,8), %rax
movl 0x8(%rax), %eax
movl %eax, %r14d
movl %eax, %ecx
movl %eax, 0x64(%rsp)
xorl $0x1, %r14d
movq 0x10(%rsp), %rbp
movl %ebp, %ebx
subq $0x8, %rsp
movq 0x50(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
leaq 0xf8(%rsp), %rdx
movq 0x58(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
pushq %rax
callq 0x149b3f
addq $0x8, %rsp
movq %r13, %rdi
movq %r15, %rsi
leaq 0xf8(%rsp), %rdx
movl 0x68(%rsp), %ecx
movl %r14d, %r8d
movq 0x90(%rsp), %r9
xorl %r14d, %r14d
pushq %r14
leaq 0x20(%rsp), %rax
pushq %rax
pushq $0x4
pushq $0x1
leaq 0xd8(%rsp), %rax
pushq %rax
leaq 0x100(%rsp), %rax
pushq %rax
pushq 0x78(%rsp)
pushq 0xb0(%rsp)
pushq 0x98(%rsp)
callq 0x1522b6
addq $0x48, %rsp
movq %r13, %rdi
movq 0x58(%rsp), %r13
movq %r15, %rsi
leaq 0xf8(%rsp), %rdx
movl 0x6c(%rsp), %ecx
movq %r13, %r8
movl $0x1, %r9d
pushq %r14
callq 0x149b3f
addq $0x10, %rsp
movq 0x10(%rsp), %rax
cmpq %rbx, %rax
jbe 0x154b8a
movq 0x20(%rsp), %rcx
leal 0x1(%rbp), %edx
movq 0x38(%rsp), %r14
movq 0x80(%rsp), %r9
movq 0x78(%rsp), %r10
movl (%rcx,%rbx,4), %esi
movl %esi, %edi
shrl %edi
cmpb $0x0, (%r14,%rdi)
jne 0x154b7f
movb $0x1, (%r14,%rdi)
movl %ebp, %edi
movl %esi, (%rcx,%rdi,4)
incl %ebp
movl %edx, %ebx
incl %edx
cmpq %rbx, %rax
ja 0x154b65
jmp 0x154b9c
movq 0x38(%rsp), %r14
movq 0x80(%rsp), %r9
movq 0x78(%rsp), %r10
movl %ebp, %eax
movq %rax, 0x10(%rsp)
incq %r12
cmpq %r9, %r12
jne 0x154a5f
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x154bd4
movq 0x20(%rsp), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
shrl %esi
movb $0x0, (%r14,%rsi)
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0x154bc0
movq (%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x30(%rsp), %rdi
movl %edi, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rsi
movq 0x18(%rcx,%rsi,8), %rdx
testb $0x1, %dil
movq 0x48(%rsp), %r15
jne 0x154c08
leaq (%rcx,%rsi,8), %rcx
movl 0x10(%rcx), %ecx
decl %ecx
movl 0x4(%rdx,%rcx,8), %ecx
jmp 0x154c0e
movl 0x4(%rdx), %ecx
xorl $0x1, %ecx
movl %ecx, %ebx
shrl %ebx
testb $0x1, %cl
jne 0x154c22
movq 0x40(%r15), %rcx
movl (%rcx,%rbx,4), %ecx
incl %ecx
jmp 0x154c29
movq 0x40(%r15), %rcx
movl (%rcx,%rbx,4), %ecx
movq 0x28(%r15), %rdx
movl %ecx, %ecx
movl (%rdx,%rcx,4), %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
je 0x154ca5
movl %r12d, %ecx
shrl %ecx
movq (%r15), %rdx
movq 0x80(%r15), %rsi
movl (%rdx,%rcx,4), %ecx
incq %rsi
cmpq %rcx, %rsi
jb 0x154ca5
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jne 0x154c89
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rsp)
movl %r12d, (%rcx,%rax,4)
movq 0x48(%rsp), %r15
xorl $0x1, %r12d
movq 0xb8(%r15), %rax
testb $0x1, 0x30(%rsp)
jne 0x154cfd
subq $0x40, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movq 0x80(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %r12d, %ecx
movl %ebx, %r8d
jmp 0x154d41
subq $0x40, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movq 0x80(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdi
movl %r12d, %esi
movl %ebx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r15, %r9
callq 0x145cd9
addq $0x40, %rsp
movq (%rsp), %rdi
movq 0x30(%rsp), %r8
incq %r8
cmpq 0x68(%rsp), %r8
jne 0x1549cd
movq 0x38(%r15), %rax
cmpq %rax, 0x30(%r15)
jne 0x154d93
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x38(%r15)
movq 0x40(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x40(%r15)
movl 0x18(%r15), %eax
movq 0x40(%r15), %rcx
movq 0x30(%r15), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x30(%r15)
movl %eax, (%rcx,%rdx,4)
movq 0xb8(%r15), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %ecx
movl %ecx, %eax
movl 0x5c(%rsp), %edx
subl %edx, %eax
addq %rax, 0xa8(%r15)
cmpl %edx, %ecx
movq 0x40(%rsp), %r12
je 0x154dd9
movq %r15, %rdi
callq 0x1394bc
cmpl $0x0, 0xc(%rsp)
je 0x154e5c
xorl %r14d, %r14d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq (%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%r14,8), %rax
movq %rax, %rcx
andq %rbx, %rcx
je 0x154e52
movq 0x8(%rdx), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %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 0x154e20
testl %esi, %esi
je 0x154e52
movq 0xb8(%r15), %rsi
movq (%rsp), %rdx
xorl %ecx, %ecx
movl %r14d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x147dea
incq %r14
cmpq 0x68(%rsp), %r14
jne 0x154de8
movq 0xb8(%r15), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1481b6
movq (%rsp), %rdi
callq 0xa0004
movq 0xb8(%r15), %rdi
callq 0xb0285
movq %rax, 0xc0(%r15)
movq 0xd8(%r15), %rdi
callq 0x2220
movq 0xc0(%r15), %rbx
movq $0x0, 0xc8(%r15)
movq (%rbx), %r14
movq %r14, 0xd0(%r15)
movq %r14, %rdi
shlq $0x5, %rdi
callq 0x2280
movq %rax, 0xd8(%r15)
movq %r14, 0xc8(%r15)
cmpq $0x0, (%rbx)
je 0x154f17
movl $0x1, %ebp
xorl %eax, %eax
leaq (%rax,%rax,4), %rdi
shlq $0x3, %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 %r12, %rdx
callq 0x14655c
movl %ebp, %eax
incl %ebp
cmpq %rax, (%rbx)
ja 0x154edb
movq 0xc8(%r15), %rsi
movq 0xd8(%r15), %rdi
leaq -0xe86e(%rip), %rcx     # 0x1466be
movl $0x20, %edx
callq 0x23f0
movq %r13, %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
movq 0x138(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq