// File: /workspace/github_cpp_makefile_star2_O1/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 0x14e3db
movq %r9, %rbx
movq %rdi, %rbp
movl %edx, %eax
cmpl $0x0, 0x90(%rsp)
jne 0x14e20b
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 0x14e2d1
movq %r15, %rdi
movl %r12d, %esi
movq %rbp, %rdx
movq %rbx, %rcx
leaq 0x8(%rsp), %r8
callq 0x14dc11
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x14e3c8
movl %r14d, %edi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x8(%rsp), %r13
movq %r13, %r8
callq 0x14c1ed
movsd 0x40(%rsp), %xmm0
xorl %eax, %eax
ucomisd 0x38(%rsp), %xmm0
setb %al
movl %r14d, %ecx
movl 0x18(%rbp,%rax,4), %eax
orl 0x8(%rsp), %eax
movq 0x10(%rbp), %rdx
orl %eax, (%rdx,%rcx,4)
movb $0x1, (%rbx,%rcx)
movq %r15, %rdi
movl %r12d, %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r13, %r8
jmp 0x14e26b
movl (%rax,%rcx,4), %edx
movl %edx, 0xc(%rsp)
movl %r12d, %r13d
testl %r12d, %r12d
setne %dil
je 0x14e330
movq 0x10(%rbp), %rdx
movl 0x1c(%rbp), %esi
orl 0x18(%rbp), %esi
orl 0x20(%rbp), %esi
movl (%r15), %r8d
shrl %r8d
testl %esi, (%rdx,%r8,4)
jne 0x14e327
leaq (%rax,%rcx,4), %rax
addq $0xc, %rax
leaq -0x1(%r13), %rcx
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x14e330
movl (%rax,%rdi,4), %r8d
shrl %r8d
incq %rdi
testl %esi, (%rdx,%r8,4)
je 0x14e30b
cmpq %r13, %rdi
setb %dil
testb %dil, %dil
jne 0x14e3c8
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rcx
leaq 0x8(%rsp), %r8
xorl %r9d, %r9d
callq 0x14cf23
cmpl $-0x1, 0x8(%rsp)
jne 0x14e370
movq %r14, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x8(%rsp), %r8
xorl %r9d, %r9d
callq 0x14cf23
testl %eax, %eax
je 0x14e3c8
movq 0x30(%rsp), %r14
movl $0xffffffff, 0x68(%r14) # imm = 0xFFFFFFFF
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r14, %rcx
movq %rbp, %r8
callq 0x14c3d3
movl 0x8(%rsp), %r9d
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r14, %rcx
movq %rbp, %r8
callq 0x14c3d3
testl %r12d, %r12d
je 0x14e3c8
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
shrl %ecx
movb $0x1, (%rbx,%rcx)
incq %rax
cmpq %rax, %r13
jne 0x14e3b6
movl 0x1c(%rsp), %edx
incl %edx
cmpl 0x88(%rsp), %edx
jbe 0x14e1f3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq