// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0xc0(%rax), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x1ef9dc
jmp 0x1efc2d
movl $0x1, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x2100
movq -0x38(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x44(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x3c(%rbp), %r9
callq 0x1ef030
cmpl $-0x1, -0x44(%rbp)
je 0x1efaa9
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1efaa7
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
movl 0x18(%rcx), %ecx
movq -0x8(%rbp), %rdx
orl 0x1c(%rdx), %ecx
movq -0x8(%rbp), %rdx
orl 0x20(%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1efa9a
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
movl -0x44(%rbp), %edx
movq -0x8(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jne 0x1efa9a
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movb $0x1, (%rax,%rcx)
jmp 0x1efa9c
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1efa30
jmp 0x1efaa9
cmpl $-0x1, -0x44(%rbp)
jne 0x1efaf1
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
xorl %r10d, %r10d
movl $0x0, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x1ef6f0
jmp 0x1efc2d
cmpl $0x0, -0x48(%rbp)
jne 0x1efb39
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
xorl %r10d, %r10d
movl $0x0, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x1ef6f0
jmp 0x1efc2b
cmpl $0x0, -0x3c(%rbp)
je 0x1efbb5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movl -0x48(%rbp), %r10d
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x1ef6f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movl -0x48(%rbp), %eax
subl $0x1, %eax
xorl %r10d, %r10d
movl $0x0, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x1ef6f0
jmp 0x1efc29
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movl -0x48(%rbp), %eax
subl $0x1, %eax
xorl %r10d, %r10d
movl $0x0, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x1ef6f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x28(%rbp), %r9
movl -0x48(%rbp), %r10d
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x1ef6f0
jmp 0x1efc2b
jmp 0x1efc2d
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)