// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rdi, %rbp
movq 0xc0(%rsi), %rax
movl %r9d, %ecx
imulq $0x28, %rcx, %r13
addq 0x10(%rax), %r13
movl %r8d, %edx
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x10(%r13), %eax
cmpq %rax, %r15
jae 0x1157b8
movq 0x18(%r13), %rax
movq (%rax,%r15,8), %rax
movq 0x28(%r12), %rcx
movq 0x40(%r12), %rdx
shrq $0x21, %rax
movl (%rdx,%rax,4), %esi
movl 0x4(%rdx,%rax,4), %eax
leaq (%rcx,%rsi,4), %rcx
addq $0x8, %rcx
subl %esi, %eax
addl $-0x2, %eax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x1157b3
movl (%rcx,%rdx,4), %esi
shrl %esi
movb $0x2, (%rbx,%rsi)
incq %rdx
jmp 0x1157a0
incq %r15
jmp 0x11576b
xorl %ecx, %ecx
pushq $0x3
popq %r15
movq %r12, 0x38(%rsp)
movq %rbp, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq %r13, 0x30(%rsp)
movl %eax, %eax
cmpq %rax, %rcx
jae 0x115a35
andq $0x0, 0x8(%r14)
movq 0x18(%r13), %rax
movq %rcx, 0x40(%rsp)
movq (%rax,%rcx,8), %rax
movq 0x28(%r12), %r13
movq 0x40(%r12), %rcx
shrq $0x21, %rax
movl (%rcx,%rax,4), %ebp
movl 0x4(%rcx,%rax,4), %ecx
subl %ebp, %ecx
addl $-0x2, %ecx
cmpq %rcx, 0x10(%r14)
movq %rcx, 0x8(%rsp)
jae 0x115869
leaq -0x1(%rcx), %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
incq %rcx
movq %rcx, 0x10(%r14)
movq (%r14), %rdi
imulq $0x28, %rcx, %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq %rax, (%r14)
leaq 0x8(,%rbp,4), %rdx
addq %r13, %rdx
movq %rcx, 0x8(%r14)
imulq $0xa, %rcx, %rax
movq %rdx, 0x10(%rsp)
movq %rdx, %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x1158b1
movl (%rcx), %esi
shrl %esi
movb $0x3, (%rbx,%rsi)
movl (%rcx), %esi
movq (%r14), %rdi
movq %rsi, 0x8(%rdi,%rdx,4)
andq $0x0, (%rdi,%rdx,4)
movb $0x0, 0x20(%rdi,%rdx,4)
addq $0xa, %rdx
addq $0x4, %rcx
jmp 0x115886
movq 0x8(%r14), %rax
movq %rax, 0x48(%rsp)
xorl %eax, %eax
movl %eax, %r12d
cmpq %r12, 0x48(%rsp)
jbe 0x115936
movq 0x18(%rsp), %rax
movq (%rax), %rbp
imulq $0x28, %r12, %r15
movq 0x8(%rbp,%r15), %r13
shrq %r13
movl %r13d, %edi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdx
pushq $0x1
popq %rcx
callq 0x1155e4
addsd (%rbp,%r15), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd %xmm0, (%rbp,%r15)
movl %r13d, %edi
movq %rbx, %rsi
movq %r14, %rdx
pushq $0x2
popq %rcx
callq 0x1155e4
movsd 0x50(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%r15)
pushq $0x3
popq %r15
leal 0x1(%r12), %eax
jmp 0x1158bc
xorl %r13d, %r13d
movq 0x38(%rsp), %r12
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x110479
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x1159c2
movq (%r14), %rcx
movl %eax, %eax
imulq $0x28, %rax, %rax
movl 0x8(%rcx,%rax), %eax
movl %r13d, %ecx
movq 0x10(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
incl %r13d
movl 0x4(%rsp), %esi
movq %r14, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
movl %r15d, %r8d
callq 0x11565c
jmp 0x11594d
movq (%r14), %rcx
movl %eax, %eax
imulq $0x28, %rax, %rax
movl 0x8(%rcx,%rax), %eax
movl %r13d, %ecx
movq 0x10(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
incl %r13d
movl 0x4(%rsp), %esi
movq %r14, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
movl %r15d, %r8d
callq 0x11565c
movq %r14, %rdi
leaq 0x4(%rsp), %rsi
callq 0x110479
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
jne 0x115992
movq 0x8(%rsp), %rdx
cmpl %r13d, %edx
jne 0x115a12
xorl %eax, %eax
movq 0x30(%rsp), %r13
cmpq %rax, %rdx
je 0x115a01
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
shrl %ecx
movb $0x1, (%rbx,%rcx)
incq %rax
jmp 0x1159e9
movq 0x40(%rsp), %rcx
incq %rcx
movl 0x10(%r13), %eax
jmp 0x1157d7
movq 0x13d5c7(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xd5361(%rip), %rdi     # 0x1ead84
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x8(%rsp), %rdx
jmp 0x1159e2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq