// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: phase_bubble_chain(H_partition*, ma_ug_t*, bub_p_t_warp*, bubble_type*, unsigned char*, unsigned int)
void phase_bubble_chain(H_partition* hap, ma_ug_t *ug, bub_p_t_warp* b, bubble_type* bub, 
                                                    uint8_t* hap_label_flag, uint32_t chain_id)
{
    ma_utg_t *u = &(bub->b_ug->u.a[chain_id]);
    if(u->n == 0) return;
    uint32_t is_forward = 1, i, max_hap_label, max_bid_idx;
    memset(hap_label_flag, 0, ug->g->n_seq);

    get_weightest_hap_label_from_chain(u, hap, bub, &max_hap_label, &max_bid_idx, &is_forward);

    if(max_hap_label != (uint32_t)-1) ///means this is not a new chain
    {
        for (i = 0; i < hap->n; i++)
        {
            if(is_hap_set_label(i, *hap, max_hap_label)) hap_label_flag[i] = 1;
        }
    }

    if(max_hap_label == (uint32_t)-1) ///a totally new chain
    {   
        phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, 0, u->n - 1, 1);
    }
    else
    {
        if(max_bid_idx == 0)
        {
            phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, 0, u->n - 1, 1);
        }
        else
        {
            if(is_forward)
            {
                phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, max_bid_idx, u->n - 1, 1);
                phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, 0, max_bid_idx-1, 0);
            }
            else
            {
                phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, 0, max_bid_idx-1, 0);
                phase_bubble_chain_dir(hap, ug, b, bub, u, hap_label_flag, max_bid_idx, u->n - 1, 1);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0xc0(%rcx), %rax
movq 0x10(%rax), %r15
movl %r9d, %eax
imulq $0x28, %rax, %rax
cmpl $0x0, 0x10(%r15,%rax)
je 0x10d7ea
movq %r8, %rbx
movq %rdi, %rbp
movq %rdx, 0x10(%rsp)
addq %rax, %r15
leaq 0xc(%rsp), %rax
movl $0x1, (%rax)
movq 0x18(%rsi), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rcx, %r12
movq %rsi, 0x18(%rsp)
xorl %r14d, %r14d
movq %r8, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x24(%rsp), %r13
leaq 0x8(%rsp), %r8
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
leaq 0xc(%rsp), %r9
callq 0x10d11a
movl (%r13), %eax
cmpl $-0x1, %eax
je 0x10d7aa
movq 0x18(%rsp), %r13
movl %r14d, %r14d
cmpq %r14, (%rbp)
jbe 0x10d744
movq 0x10(%rbp), %rcx
movl 0x1c(%rbp), %edi
orl 0x18(%rbp), %edi
movl (%rcx,%r14,4), %edx
orl 0x20(%rbp), %edi
testl %edx, %edi
je 0x10d73f
movb 0x2c(%rbp), %cl
shrl %cl, %edx
movl %eax, %edi
shrl %cl, %edi
cmpl %edi, %edx
jne 0x10d73f
movb $0x1, (%rbx,%r14)
incl %r14d
jmp 0x10d70f
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x10d7be
cmpl $0x0, 0xc(%rsp)
movq %r12, %r14
je 0x10d7f9
movl 0x10(%r15), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
callq 0x10d47d
addq $0x20, %rsp
movl 0x8(%rsp), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
pushq $0x0
jmp 0x10d7de
movl 0x10(%r15), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
jmp 0x10d7ce
movl 0x10(%r15), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
movq %rbx, %r9
pushq $0x1
pushq %rax
pushq $0x0
callq 0x10d47d
addq $0x20, %rsp
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
pushq $0x0
pushq %rax
pushq $0x0
callq 0x10d47d
addq $0x20, %rsp
movl 0x8(%rsp), %eax
movl 0x10(%r15), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
jmp 0x10d7e1