// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %rcx, %r14
movq 0xc0(%rcx), %rax
movq 0x10(%rax), %rax
movl %r9d, %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0x10(%rax,%rcx,8)
je 0x154b12
movq %r8, %rbx
movq %rsi, %r15
movq %rdi, %rbp
movq %rdx, 0x8(%rsp)
leaq (%rax,%rcx,8), %r12
leaq 0x4(%rsp), %rax
movl $0x1, (%rax)
movq 0x18(%rsi), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r8, %rdi
xorl %esi, %esi
callq 0x20e0
leaq 0x14(%rsp), %r13
movq %rsp, %r8
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r13, %rcx
leaq 0x4(%rsp), %r9
callq 0x1543b4
movl (%r13), %eax
cmpl $-0x1, %eax
je 0x154ad2
movq (%rbp), %r10
testq %r10, %r10
movq 0x8(%rsp), %rdx
movq %r15, %r13
je 0x154a6f
movl $0x1, %esi
xorl %edi, %edi
movq 0x10(%rbp), %rcx
movl 0x1c(%rbp), %r9d
orl 0x18(%rbp), %r9d
movl (%rcx,%rdi,4), %r8d
orl 0x20(%rbp), %r9d
testl %r8d, %r9d
je 0x154a66
movb 0x2c(%rbp), %cl
shrl %cl, %r8d
movl %eax, %r9d
shrl %cl, %r9d
cmpl %r9d, %r8d
jne 0x154a66
movb $0x1, (%rbx,%rdi)
movq (%rbp), %r10
movl %esi, %edi
incl %esi
cmpq %rdi, %r10
ja 0x154a34
movl (%rsp), %eax
testl %eax, %eax
je 0x154aea
cmpl $0x0, 0x4(%rsp)
je 0x154b21
movl 0x10(%r12), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rcx
movq %r12, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
movq %rdx, %r15
callq 0x154794
addq $0x20, %rsp
movl (%rsp), %eax
decl %eax
subq $0x8, %rsp
xorl %r10d, %r10d
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
movq %rbx, %r9
pushq %r10
pushq %rax
pushq %r10
jmp 0x154b09
movl 0x10(%r12), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
jmp 0x154afb
movl 0x10(%r12), %eax
decl %eax
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rcx
movq %r12, %r8
movq %rbx, %r9
pushq $0x1
pushq %rax
pushq $0x0
callq 0x154794
addq $0x20, %rsp
addq $0x18, %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 %r13, %rsi
movq %r14, %rcx
movq %r12, %r8
movq %rbx, %r9
pushq %r10
pushq %rax
pushq %r10
movq %rdx, %r15
callq 0x154794
addq $0x20, %rsp
movl (%rsp), %eax
movl 0x10(%r12), %r10d
decl %r10d
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
movq %rbx, %r9
pushq $0x1
pushq %r10
pushq %rax
jmp 0x154b09