// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, %rbp
movq 0xc0(%rcx), %rax
movq 0x10(%rax), %rax
movl %r9d, %edi
leaq (%rdi,%rdi,4), %rdi
cmpl $0x0, 0x10(%rax,%rdi,8)
je 0x14e563
movq %r8, %rbx
movq %rcx, %r12
leaq (%rax,%rdi,8), %r14
movq %rdx, %r15
leaq 0xc(%rsp), %rax
movl $0x1, (%rax)
movq %rsi, 0x10(%rsp)
movq 0x18(%rsi), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r8, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x24(%rsp), %r13
leaq 0x8(%rsp), %r8
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
leaq 0xc(%rsp), %r9
callq 0x14ddee
movl (%r13), %eax
cmpl $-0x1, %eax
je 0x14e535
cmpq $0x0, (%rbp)
je 0x14e4c2
movl $0x1, %edx
xorl %esi, %esi
movq 0x10(%rbp), %rcx
movl 0x1c(%rbp), %r8d
orl 0x18(%rbp), %r8d
movl (%rcx,%rsi,4), %edi
orl 0x20(%rbp), %r8d
testl %edi, %r8d
je 0x14e4b8
movb 0x2c(%rbp), %cl
shrl %cl, %edi
movl %eax, %r8d
shrl %cl, %r8d
cmpl %r8d, %edi
jne 0x14e4b8
movb $0x1, (%rbx,%rsi)
movl %edx, %esi
incl %edx
cmpq %rsi, (%rbp)
ja 0x14e48c
cmpl $-0x1, %eax
je 0x14e535
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x14e535
cmpl $0x0, 0xc(%rsp)
je 0x14e572
movq 0x18(%rsp), %r14
movl 0x10(%r14), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
callq 0x14e1b7
addq $0x20, %rsp
movl 0x8(%rsp), %eax
decl %eax
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq %r10
pushq %rax
pushq %r10
jmp 0x14e55a
movq 0x18(%rsp), %r8
movl 0x10(%r8), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbx, %r9
pushq $0x1
pushq %rax
pushq $0x0
callq 0x14e1b7
addq $0x20, %rsp
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decl %eax
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rbp, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r14
movq %r14, %r8
movq %rbx, %r9
pushq %r10
pushq %rax
pushq %r10
callq 0x14e1b7
addq $0x20, %rsp
movl 0x8(%rsp), %eax
movl 0x10(%r14), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
jmp 0x14e55a