// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
movq 0xc0(%rsi), %rax
movq 0x10(%rax), %r15
movl %r9d, %eax
leaq (%rax,%rax,4), %r12
movl %r8d, %edx
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x10(%r15,%r12,8), %ecx
testl %ecx, %ecx
je 0x15bdd3
leaq (%r15,%r12,8), %r8
xorl %eax, %eax
movq 0x18(%r8), %rdx
movq (%rdx,%rax,8), %rdx
movq 0x40(%rbp), %rdi
shrq $0x21, %rdx
movl (%rdi,%rdx,4), %esi
movl 0x4(%rdi,%rdx,4), %edx
subl %esi, %edx
addl $-0x2, %edx
je 0x15bb2a
movq 0x28(%rbp), %rcx
leaq (%rcx,%rsi,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 0x15bb15
movl 0x10(%r8), %ecx
incq %rax
movl %ecx, %edx
cmpq %rdx, %rax
jb 0x15bae7
testl %ecx, %ecx
je 0x15bdd3
xorl %ecx, %ecx
movq %rbp, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %rbx, 0x30(%rsp)
movq %r14, 0x10(%rsp)
movq $0x0, 0x8(%r14)
movq 0x18(%r8), %rax
movq %rcx, 0x48(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x28(%rbp), %r13
movq 0x40(%rbp), %rcx
shrq $0x21, %rax
movl (%rcx,%rax,4), %r15d
movl 0x4(%rcx,%rax,4), %r12d
subl %r15d, %r12d
addl $-0x2, %r12d
cmpq %r12, 0x10(%r14)
jae 0x15bbde
leaq -0x1(%r12), %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), %rax
addq %r13, %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x8(%r14)
movq %r12, 0x28(%rsp)
testq %r12, %r12
movq 0x18(%rsp), %rbx
movq 0x30(%rsp), %r8
je 0x15bcd3
movq 0x28(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %r9
movl (%rdx), %esi
shrl %esi
movb $0x3, (%r8,%rsi)
movl (%rdx), %esi
movq (%r9), %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 0x15bc27
movq 0x8(%r9), %r13
testq %r13, %r13
movq 0x18(%rsp), %rbx
je 0x15bcd3
movq 0x10(%rsp), %rax
movq (%rax), %r12
movl $0x1, %r15d
movq 0x8(%r12), %rbp
shrq %rbp
movl %ebp, %edi
movq %rbx, %rsi
movq %r8, %rdx
movl $0x1, %ecx
movq %r8, %r14
callq 0x15b924
addsd (%r12), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd %xmm0, (%r12)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x2, %ecx
callq 0x15b924
movq %r14, %r8
movsd 0x50(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%r12)
movl %r15d, %eax
addq $0x28, %r12
incl %r15d
cmpq %rax, %r13
ja 0x15bc70
xorl %ebp, %ebp
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %r15
xorl %esi, %esi
movq %r8, %rbx
callq 0x157328
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
movq 0x20(%rsp), %r13
je 0x15bd3b
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 %r15, %rdx
movq %rbx, %rcx
movl $0x3, %r8d
callq 0x15b9ab
movq %r14, %rdi
xorl %esi, %esi
callq 0x157328
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
jne 0x15bcfa
movq %r14, %rdi
leaq 0xc(%rsp), %r12
movq %r12, %rsi
callq 0x157328
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x15bd87
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 %r15, %rdx
movq %rbx, %rcx
movl $0x3, %r8d
callq 0x15b9ab
movq %r14, %rdi
jmp 0x15bd43
movq 0x28(%rsp), %r15
cmpl %r15d, %ebp
jne 0x15bdcc
testl %r15d, %r15d
movq 0x40(%rsp), %rbp
je 0x15bdb0
xorl %eax, %eax
movl (%r13,%rax,4), %ecx
shrl %ecx
movb $0x1, (%rbx,%rcx)
incq %rax
cmpq %rax, %r15
jne 0x15bd9d
movq 0x48(%rsp), %rcx
incq %rcx
movq 0x38(%rsp), %r8
movl 0x10(%r8), %eax
cmpq %rax, %rcx
jb 0x15bb52
jmp 0x15bdd3
callq 0x1cafa
jmp 0x15bd91
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq