// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: reorder_bubbble_chain(kv_u_trans_t*, bubble_type*, bub_sort_vec*, unsigned char*, unsigned int, unsigned int)
void reorder_bubbble_chain(kv_u_trans_t *ta, bubble_type* bub, bub_sort_vec* w_stack, 
uint8_t* vis, uint32_t n_utg, uint32_t chain_id)
{
    uint32_t i, k, m, max_idx, flag_cur = 3, flag_right = 2, flag_left = 1, flag_unset = 0, *a, n;
    uint64_t bid, uid;
    ma_utg_t *u = &(bub->b_ug->u.a[chain_id]);
    memset(vis, flag_unset, n_utg);
    for (i = 0; i < u->n; i++)
    {
        bid = u->a[i]>>33;
        get_bubbles(bub, bid, NULL, NULL, &a, &n, NULL);
        for (k = 0; k < n; k++)
        {
            uid = a[k]>>1;
            vis[uid] = flag_right;
        }
    }

    for (i = 0; i < u->n; i++)
    {
        w_stack->n = 0;
        bid = u->a[i]>>33;
        get_bubbles(bub, bid, NULL, NULL, &a, &n, NULL);
        kv_resize(bub_sort_type, *w_stack, n); 
        w_stack->n = n;
        for (k = 0; k < n; k++)
        {
            uid = a[k]>>1;
            vis[uid] = flag_cur;
            w_stack->a[k].p_id = a[k];
            w_stack->a[k].weight = 0;
            w_stack->a[k].used = 0;
        }
        
        for (k = 0; k < w_stack->n; k++)
        {
            w_stack->a[k].weight += get_specific_hic_weight_by_chain(w_stack->a[k].p_id>>1, 
            ta, vis, flag_left);
            w_stack->a[k].weight -= get_specific_hic_weight_by_chain(w_stack->a[k].p_id>>1, 
            ta, vis, flag_right);
        }
        m = 0;
        while ((max_idx = get_max_hap_g(w_stack, NULL)) != (uint32_t)-1)
        {
            a[m] = w_stack->a[max_idx].p_id;
            m++;
            update_bubble_weight(w_stack, max_idx, ta, vis, flag_cur);
        }

        while ((max_idx = get_max_hap_g(w_stack, &max_idx)) != (uint32_t)-1)
        {
            a[m] = w_stack->a[max_idx].p_id;
            m++;
            update_bubble_weight(w_stack, max_idx, ta, vis, flag_cur);
        }
        if(m != n) fprintf(stderr, "ERROR\n");


        for (k = 0; k < n; k++)
        {
            uid = a[k]>>1;
            vis[uid] = flag_left;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0xc0(%rsi), %rax
movq 0x10(%rax), %rax
movl %r9d, %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %r13
movl %r8d, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
cmpl $0x0, 0x10(%r13)
je 0x1551d4
xorl %eax, %eax
movq 0x18(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movq 0x40(%r15), %rsi
shrq $0x21, %rdx
movl (%rsi,%rdx,4), %ecx
movl 0x4(%rsi,%rdx,4), %edx
subl %ecx, %edx
addl $-0x2, %edx
je 0x1551c8
movq 0x28(%r15), %rsi
leaq (%rsi,%rcx,4), %rcx
addq $0x8, %rcx
movl %edx, %edx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
shrl %edi
movb $0x2, (%rbx,%rdi)
incq %rsi
cmpq %rsi, %rdx
jne 0x1551b7
incq %rax
movl 0x10(%r13), %ecx
cmpq %rcx, %rax
jb 0x155189
cmpl $0x0, 0x10(%r13)
je 0x155458
xorl %ecx, %ecx
movq %r15, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movq $0x0, 0x8(%r14)
movq 0x18(%r13), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x28(%r15), %r13
movq 0x40(%r15), %rcx
shrq $0x21, %rax
movl (%rcx,%rax,4), %r15d
movl 0x4(%rcx,%rax,4), %ebp
subl %r15d, %ebp
addl $-0x2, %ebp
cmpq %rbp, 0x10(%r14)
jae 0x155279
leaq -0x1(%rbp), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%r14)
movq (%r14), %rdi
leaq 0x8(,%rcx,8), %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq %rax, (%r14)
leaq 0x8(,%r15,4), %r8
addq %r13, %r8
movq %rbp, 0x8(%r14)
testq %rbp, %rbp
je 0x1552ca
leaq (,%rbp,8), %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
movq %r8, %rdx
movl (%rdx), %esi
shrl %esi
movb $0x3, (%rbx,%rsi)
movl (%rdx), %esi
movq (%r14), %rdi
movq %rsi, 0x8(%rdi,%rcx)
movq $0x0, (%rdi,%rcx)
movb $0x0, 0x20(%rdi,%rcx)
addq $0x4, %rdx
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0x15529e
movq %r8, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
movq %r14, %rax
movq %rbx, %r14
movq 0x8(%rax), %rbx
testq %rbx, %rbx
movq %r12, %r15
je 0x155351
movq 0x10(%rsp), %rax
movq (%rax), %r12
movl $0x1, %r13d
movq 0x8(%r12), %rbp
shrq %rbp
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x154fa2
addsd (%r12), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd %xmm0, (%r12)
movl %ebp, %edi
movq %r15, %rsi
movq %r14, %rdx
movl $0x2, %ecx
callq 0x154fa2
movsd 0x40(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r12)
movl %r13d, %eax
addq $0x28, %r12
incl %r13d
cmpq %rax, %rbx
ja 0x1552f4
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x150b26
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
movq %r15, %r12
movq 0x18(%rsp), %r13
movq %r14, %rbx
movq 0x10(%rsp), %r14
je 0x1553bb
xorl %ebp, %ebp
movq (%r14), %rcx
movl %eax, %eax
leaq (%rax,%rax,4), %rax
movl 0x8(%rcx,%rax,8), %eax
movl %ebp, %ecx
movl %eax, (%r13,%rcx,4)
incl %ebp
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq %r12, %rdx
movq %rbx, %rcx
movl $0x3, %r8d
callq 0x155029
movq %r14, %rdi
xorl %esi, %esi
callq 0x150b26
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
jne 0x15537a
movq %r14, %rdi
leaq 0xc(%rsp), %r15
movq %r15, %rsi
callq 0x150b26
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x155407
movq (%r14), %rcx
movl %eax, %eax
leaq (%rax,%rax,4), %rax
movl 0x8(%rcx,%rax,8), %eax
movl %ebp, %ecx
movl %eax, (%r13,%rcx,4)
incl %ebp
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq %r12, %rdx
movq %rbx, %rcx
movl $0x3, %r8d
callq 0x155029
movq %r14, %rdi
jmp 0x1553c3
movq 0x30(%rsp), %r15
cmpl %r15d, %ebp
jne 0x15544c
testq %r15, %r15
je 0x15542b
xorl %eax, %eax
movl (%r13,%rax,4), %ecx
shrl %ecx
movb $0x1, (%rbx,%rcx)
incq %rax
cmpq %rax, %r15
jne 0x155418
movq 0x38(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r13
movl 0x10(%r13), %eax
cmpq %rax, %rcx
movq 0x28(%rsp), %r15
jb 0x1551f0
jmp 0x155458
callq 0x1d874
movq 0x18(%rsp), %r13
jmp 0x155411
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq