// File: /workspace/github_cpp_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rsi, 0x78(%rsp)
movq %rdx, 0x18(%rsp)
movq 0xc0(%rdx), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rax), %rax
movl 0x14(%rax), %ebx
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %rax
movl 0x14(%rax), %eax
addl %eax, %eax
movslq %eax, %r14
pushq $0x1
popq %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, 0x58(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, 0x68(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, %r13
xorps %xmm0, %xmm0
leaq 0x100(%rsp), %r15
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
andq $0x0, 0x60(%r15)
movaps %xmm0, 0xe0(%rsp)
andq $0x0, 0xf0(%rsp)
movaps %xmm0, 0xc0(%rsp)
andq $0x0, 0xd0(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movaps %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
xorl %r12d, %r12d
pushq $0x2
popq %r14
movl %ebx, %eax
xorl %ebp, %ebp
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %eax, %ebp
jae 0x115096
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movl %r12d, %edx
movq 0x58(%rsp), %rcx
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x10519d
incl %ebp
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
addl $0x2, %r12d
jmp 0x11505e
addl %ebx, %ebx
movq 0x18(%rsp), %rbp
movq 0x30(%rbp), %rax
testq %rax, %rax
je 0x1150ad
decq %rax
movq %rax, 0x30(%rbp)
movq 0xb8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x24(%rsp)
movl %ebx, %ebx
xorl %r15d, %r15d
movq %rbx, 0x70(%rsp)
cmpq %rbx, %r15
je 0x1153ec
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x28(%rdi), %rax
movq (%rax,%r15,8), %rbx
testl %ebx, %ebx
je 0x1153da
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %r14
addq 0x8(%rdi), %r14
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0x1153da
andq $0x0, 0x30(%rsp)
movl %r15d, %eax
xorl $0x1, %eax
movl %eax, 0x60(%rsp)
movl %ebx, %eax
movq %rax, 0x90(%rsp)
xorl %ebx, %ebx
xorl %ecx, %ecx
movq %r15, 0x88(%rsp)
movq %r14, 0x80(%rsp)
cmpq 0x90(%rsp), %rcx
je 0x115274
imulq $0x18, %rcx, %rax
cmpl $0x0, 0xc(%r14,%rax)
js 0x11526c
movq %rcx, 0x98(%rsp)
addq %r14, %rax
movl 0x8(%rax), %ecx
movl %ecx, 0x64(%rsp)
movl %ecx, %r14d
xorl $0x1, %r14d
subq $0x8, %rsp
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
leaq 0x108(%rsp), %rdx
movq 0x60(%rsp), %r12
movq %r12, %r8
pushq $0x1
popq %r9
pushq $0x0
callq 0x107d53
popq %rax
popq %rcx
subq $0x8, %rsp
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x108(%rsp), %rdx
movl 0x68(%rsp), %ecx
movl %r14d, %r8d
movq 0x80(%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 0x60(%rsp)
pushq 0xa8(%rsp)
pushq %r12
callq 0x11201e
addq $0x48, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq 0x90(%rsp), %r15
leaq 0x108(%rsp), %rdx
movl 0x6c(%rsp), %ecx
movq %r12, %r8
pushq $0x1
popq %r9
pushq $0x0
callq 0x107d53
addq $0x10, %rsp
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
movl %ebx, %edx
movl %edx, %edx
movl %ebx, %esi
cmpq %rdx, %rax
jbe 0x115254
movl (%rcx,%rdx,4), %edi
movl %edi, %r8d
shrl %r8d
cmpb $0x0, (%r13,%r8)
jne 0x115250
movb $0x1, (%r13,%r8)
movl %edi, (%rcx,%rsi,4)
incl %ebx
incl %edx
jmp 0x11522b
movq %rsi, 0x30(%rsp)
movq %rsi, %rbx
movq 0x80(%rsp), %r14
movq 0x98(%rsp), %rcx
incq %rcx
jmp 0x115136
xorl %eax, %eax
movq 0x40(%rsp), %r12
movq 0x18(%rsp), %r14
movl %eax, %eax
cmpq %rax, %rbx
jbe 0x115297
movl (%r12,%rax,4), %ecx
shrl %ecx
movb $0x0, (%r13,%rcx)
incl %eax
jmp 0x115280
movq 0x8(%rsp), %rsi
movl %r15d, %edx
callq 0x10629b
movl %eax, %ebp
shrl %ebp
testb $0x1, %al
leaq 0x14(%rsp), %rsi
movq %rsi, %rdx
movl $0x0, %eax
cmoveq %rax, %rdx
movl $0x0, %ecx
cmoveq %rsi, %rcx
subq $0x8, %rsp
movq %r14, %rdi
movq %rbp, %rsi
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
callq 0xf6ab5
popq %rax
popq %rcx
movl 0x14(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x115348
movl %eax, %ecx
shrl %ecx
movq (%r14), %rdx
movq 0x80(%r14), %rsi
movl (%rdx,%rcx,4), %ecx
incq %rsi
cmpq %rcx, %rsi
jb 0x115348
cmpq 0x38(%rsp), %rbx
jne 0x11533b
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x38(%rsp)
shlq $0x2, %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq %rax, 0x40(%rsp)
movl 0x14(%rsp), %eax
movq 0x30(%rsp), %rbx
leaq 0x1(%rbx), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, (%r12,%rbx,4)
movl 0x14(%rsp), %ecx
xorl $0x1, %ecx
movq 0xb8(%r14), %rax
subq $0x40, %rsp
testb $0x1, %r15b
jne 0x115397
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movq 0x68(%rsp), %rdx
movq %rdx, 0x20(%rsp)
movups %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 %ebp, %r8d
jmp 0x1153ce
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movq 0x68(%rsp), %rdx
movq %rdx, 0x20(%rsp)
movups %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x70(%rsp), %rdi
movl %ecx, %esi
movl %ebp, %edx
pushq $-0x1
popq %rcx
movl %ecx, %r8d
movq %r14, %r9
callq 0x10441e
addq $0x40, %rsp
incq %r15
movq 0x18(%rsp), %rbp
movq 0x70(%rsp), %rbx
jmp 0x1150c5
andl $0x7fffffff, 0x24(%rsp) # imm = 0x7FFFFFFF
movq 0x30(%rbp), %rcx
cmpq 0x38(%rbp), %rcx
jne 0x115427
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x38(%rbp)
movq 0x40(%rbp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x40(%rbp)
movq 0x30(%rbp), %rcx
jmp 0x11542b
movq 0x40(%rbp), %rax
movl 0x18(%rbp), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x30(%rbp)
movl %edx, (%rax,%rcx,4)
movq 0xb8(%rbp), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%rax), %ecx
movl %ecx, %eax
movl 0x24(%rsp), %edx
subl %edx, %eax
addq %rax, 0xa8(%rbp)
cmpl %edx, %ecx
je 0x115463
movq %rbp, %rdi
callq 0xf7cab
xorl %r14d, %r14d
pushq $-0x1
popq %r12
cmpq %r14, %rbx
je 0x1154af
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x28(%rdi), %rax
cmpl $0x0, (%rax,%r14,8)
je 0x1154aa
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0x1154aa
movq 0xb8(%rbp), %rsi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
movl %r14d, %r8d
movl %r12d, %r9d
callq 0x1062c5
incq %r14
jmp 0x11546a
movq 0xb8(%rbp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1066d7
movq 0x8(%rsp), %rdi
callq 0x6921d
movq 0xb8(%rbp), %rdi
callq 0x76d91
movq %rax, 0xc0(%rbp)
movq 0xd8(%rbp), %rdi
callq 0x2220
movq 0xc0(%rbp), %rbx
movq (%rbx), %r15
movq %r15, 0xd0(%rbp)
movq %r15, %rdi
shlq $0x5, %rdi
callq 0x2280
movq %rax, 0xd8(%rbp)
movq %r15, 0xc8(%rbp)
xorl %eax, %eax
movl %eax, %r14d
cmpq %r14, %r15
jbe 0x11555b
imulq $0x28, %r14, %rdi
addq 0x10(%rbx), %rdi
movq 0xd8(%rbp), %rax
movq %r14, %rdx
shlq $0x5, %rdx
leaq (%rax,%rdx), %rcx
movq %r14, 0x10(%rax,%rdx)
movb $0x0, 0x18(%rax,%rdx)
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0x104bf5
leal 0x1(%r14), %eax
movq (%rbx), %r15
jmp 0x115519
movq 0xc8(%rbp), %rsi
movq 0xd8(%rbp), %rdi
leaq -0x10827(%rip), %rcx    # 0x104d49
pushq $0x20
popq %rdx
callq 0x23f0
movq 0x58(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movq %r13, %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
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq