// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x3, -0x3c(%rbp)
movl $0x2, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
movl -0x48(%rbp), %eax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
movzbl %al, %esi
callq 0x2100
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1fe14e
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %r8
leaq -0x54(%rbp), %r9
movq %rcx, %rdx
movq $0x0, (%rsp)
callq 0x1c4b20
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1fe13e
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movl -0x40(%rbp), %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movq -0x68(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1fe109
jmp 0x1fe140
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1fe0b7
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x1fe4ff
movq -0x18(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %r8
leaq -0x54(%rbp), %r9
movq %rcx, %rdx
movq $0x0, (%rsp)
callq 0x1c4b20
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1fe283
movl -0x54(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
imulq $0x28, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1fe285
movl -0x54(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1fe31f
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movl -0x3c(%rbp), %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movq -0x68(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x20(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1fe299
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x1fe3d8
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq %rax
movl %eax, %edi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x44(%rbp), %eax
movzbl %al, %ecx
callq 0x1fdd20
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq %rax
movl %eax, %edi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x40(%rbp), %eax
movzbl %al, %ecx
callq 0x1fdd20
movaps %xmm0, %xmm1
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1fe326
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f4a60
movl %eax, -0x38(%rbp)
cmpl $-0x1, %eax
je 0x1fe438
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x8(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x3c(%rbp), %r8d
callq 0x1fde50
jmp 0x1fe3df
jmp 0x1fe43a
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1f4a60
movl %eax, -0x38(%rbp)
cmpl $-0x1, %eax
je 0x1fe493
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x8(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x3c(%rbp), %r8d
callq 0x1fde50
jmp 0x1fe43a
movl -0x34(%rbp), %eax
cmpl -0x54(%rbp), %eax
je 0x1fe4b3
movq 0x1c5b3e(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x18b603(%rip), %rsi    # 0x389aaf
movb $0x0, %al
callq 0x22a0
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0x1fe4ef
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movl -0x44(%rbp), %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movq -0x68(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1fe4ba
jmp 0x1fe4f1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1fe155
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)