// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: phase_bubble_chain_dir(H_partition*, ma_ug_t*, bub_p_t_warp*, bubble_type*, ma_utg_t*, unsigned char*, unsigned int, unsigned int, unsigned int)
void phase_bubble_chain_dir(H_partition* hap, ma_ug_t *ug, bub_p_t_warp* b, bubble_type* bub, 
ma_utg_t *u, uint8_t* hap_label_flag, uint32_t beg_idx, uint32_t end_idx, uint32_t is_forward)
{
    uint32_t i, j, beg, sink, *a, n, max_hap_label, max_uid;
    uint64_t bid;
    double cur_w0, cur_w1;
    for (i = beg_idx; i <= end_idx; i++)
    {
        bid = is_forward? u->a[i]>>33:u->a[u->n-i-1]>>33;
        get_bubbles(bub, bid, &beg, &sink, &a, &n, NULL);
        
        if(bub->b_g->seq[bid].c != HAP_LABLE/** && bid < bub->s_bub**/) ///simple bubble
        {   
            for (j = 0; j < n; j++) ///avoiding repeat phasing
            {
                if(is_hap_set(a[j]>>1, *hap)) break;
            }

            if(j < n) goto complete;

            ///get current max_hap_label from current chain
            get_weightest_hap_label_from_bubble(bid, hap, bub, hap_label_flag, &max_hap_label, NULL);

            
            ///three levels: 
            ///1. has setted weight with same hap label (using current max_hap_label)
            ///2. has setted weight but with different hap labels (using max max_hap_label from current weight)
            ///3. has unsetted weight (add hap->hap_label)
            ///4. skip, do nothing
            if(max_hap_label == (uint32_t)-1 && 
                    get_weightest_hap_label_from_bubble(bid, hap, bub, NULL, &max_hap_label, NULL) == 0) 
            {
                goto complete;
            }

            ///phase bubble
            b->exist_hap_label = (uint32_t)-1;
            get_phase_path(ug, beg, sink, b, hap, max_hap_label);
            get_phase_path(ug, beg, sink, b, hap, max_hap_label);
            for (j = 0; j < n; j++) ///set bubble as visited
            {
                hap_label_flag[a[j]>>1] = 1;
            }
        }
        else
        {
            ///select unitig with highest related weight at one time
            while (1)
            {
                max_uid = get_weightest_uid(a, n, hap, hap_label_flag, &max_hap_label);
                if(max_uid == (uint32_t)-1) break;

                get_related_weight(max_uid, hap, &cur_w0, &cur_w1, &max_hap_label);
                if(cur_w0 >= cur_w1)
                {
                    hap->hap[max_uid] |= (max_hap_label | hap->m[0]);
                }
                else
                {
                    hap->hap[max_uid] |= (max_hap_label | hap->m[1]);
                }

                hap_label_flag[max_uid] = 1;
            }
        }

        complete:;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movl 0x80(%rsp), %edx
cmpl 0x88(%rsp), %edx
ja 0x154980
movq %r9, %rbx
movq %rdi, %rbp
leaq 0x8(%rsp), %r13
movl %edx, %eax
cmpl $0x0, 0x90(%rsp)
jne 0x1547ed
movl %edx, %eax
notl %eax
movq 0x28(%rsp), %rcx
addl 0x10(%rcx), %eax
movl %edx, 0x1c(%rsp)
movq 0x28(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %r14
shrq $0x21, %r14
movq 0x10(%rsp), %rsi
movq 0x28(%rsi), %rax
movq 0x40(%rsi), %rdx
movl (%rdx,%r14,4), %ecx
movl 0x4(%rdx,%r14,4), %r12d
leaq (%rax,%rcx,4), %r15
addq $0x8, %r15
subl %ecx, %r12d
addl $-0x2, %r12d
movq 0xb8(%rsi), %rdx
movq 0x20(%rdx), %rdx
cmpb $0x2, 0x4(%rdx,%r14,8)
jne 0x15489d
movq %r15, %rdi
movl %r12d, %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x1541e7
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x15496d
movl %r14d, %edi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r13, %r8
callq 0x15284f
movsd 0x40(%rsp), %xmm0
xorl %eax, %eax
ucomisd 0x38(%rsp), %xmm0
setb %al
movq 0x10(%rbp), %rcx
movl %r14d, %edx
movl 0x18(%rbp,%rax,4), %eax
orl 0x8(%rsp), %eax
orl %eax, (%rcx,%rdx,4)
movb $0x1, (%rbx,%rdx)
jmp 0x15483c
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
testl %r12d, %r12d
je 0x1548d2
movq 0x10(%rbp), %rax
movl 0x1c(%rbp), %ecx
orl 0x18(%rbp), %ecx
orl 0x20(%rbp), %ecx
movl %r12d, %edx
xorl %esi, %esi
movl (%r15,%rsi,4), %edi
shrl %edi
testl %ecx, (%rax,%rdi,4)
jne 0x15496d
incq %rsi
cmpq %rsi, %rdx
jne 0x1548bb
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x15350d
movl 0x8(%rsp), %r9d
cmpl $-0x1, %r9d
jne 0x154917
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x15350d
testl %eax, %eax
je 0x15496d
movl 0x8(%rsp), %r9d
movq 0x30(%rsp), %r14
movl $0xffffffff, 0x68(%r14) # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r14, %rcx
movq %rbp, %r8
callq 0x152a42
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r14, %rcx
movq %rbp, %r8
callq 0x152a42
testl %r12d, %r12d
je 0x15496d
movl %r12d, %eax
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
shrl %edx
movb $0x1, (%rbx,%rdx)
incq %rcx
cmpq %rcx, %rax
jne 0x15495b
movl 0x1c(%rsp), %edx
incl %edx
cmpl 0x88(%rsp), %edx
jbe 0x1547d5
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq