// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %r9, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %rbp
movl 0xa0(%rsp), %edx
leaq 0x14(%rsp), %r13
leaq 0x50(%rsp), %r14
cmpl 0xa8(%rsp), %edx
ja 0x10d667
movl %edx, %eax
cmpl $0x0, 0xb0(%rsp)
jne 0x10d4db
movl %edx, %eax
notl %eax
movq 0x38(%rsp), %rcx
addl 0x10(%rcx), %eax
movl %edx, 0x34(%rsp)
movq 0x38(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %r15
shrq $0x21, %r15
andq $0x0, (%rsp)
movq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x64(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0xf6ab5
movq 0xb8(%r12), %rax
movq 0x20(%rax), %rax
cmpb $0x2, 0x4(%rax,%r15,8)
leaq 0x58(%rsp), %r12
jne 0x10d58f
movq 0x28(%rsp), %rdi
movl 0x18(%rsp), %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x10cf69
cmpl $-0x1, %eax
je 0x10d5bd
movl %eax, %r15d
movl %eax, %edi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r13, %r8
callq 0x10b4a6
movsd 0x58(%rsp), %xmm0
xorl %eax, %eax
ucomisd 0x50(%rsp), %xmm0
setb %al
movq 0x10(%rbp), %rcx
movl %r15d, %edx
movl 0x18(%rbp,%rax,4), %eax
orl 0x14(%rsp), %eax
orl %eax, (%rcx,%rdx,4)
movb $0x1, (%rbx,%rdx)
jmp 0x10d534
movl 0x18(%rsp), %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x10d5c8
movq 0x10(%rbp), %rsi
movl (%rcx,%rdx,4), %edi
movl 0x1c(%rbp), %r8d
orl 0x18(%rbp), %r8d
orl 0x20(%rbp), %r8d
shrl %edi
incq %rdx
testl %r8d, (%rsi,%rdi,4)
je 0x10d59a
movl 0x34(%rsp), %edx
incl %edx
jmp 0x10d4b6
movq %r15, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
movq %rbx, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x10bfca
movl 0x14(%rsp), %r9d
cmpl $-0x1, %r9d
jne 0x10d60d
movq %r15, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x10bfca
testl %eax, %eax
je 0x10d5bd
movl 0x14(%rsp), %r9d
movq 0x48(%rsp), %r15
orl $-0x1, 0x68(%r15)
movl 0x1c(%rsp), %esi
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r15, %rcx
movq %rbp, %r8
callq 0x10b68f
movl 0x1c(%rsp), %esi
movl 0x14(%rsp), %r9d
movq %r12, %rdi
movq %r15, %rcx
movq %rbp, %r8
callq 0x10b68f
movl 0x18(%rsp), %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x10d5bd
movl (%rcx,%rdx,4), %esi
shrl %esi
movb $0x1, (%rbx,%rsi)
incq %rdx
jmp 0x10d650
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq