// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl 0x10(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl 0x18(%rbp), %eax
ja 0x1ef986
cmpl $0x0, 0x20(%rbp)
je 0x1ef74d
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x78(%rbp)
jmp 0x1ef770
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x34(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0x48(%rbp), %r8
leaq -0x4c(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x1c4b20
movq -0x20(%rbp), %rax
movq 0xb8(%rax), %rax
movq 0x20(%rax), %rax
movq -0x60(%rbp), %rcx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x2, %eax
je 0x1ef8e1
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x1ef813
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movl -0x38(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %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 0x1ef806
jmp 0x1ef813
jmp 0x1ef808
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1ef7c7
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x1ef820
jmp 0x1ef976
movq -0x60(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x50(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
callq 0x1ed1f0
cmpl $-0x1, -0x50(%rbp)
jne 0x1ef86b
movq -0x60(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x50(%rbp), %r8
movq %r9, %rcx
callq 0x1ed1f0
cmpl $0x0, %eax
jne 0x1ef86b
jmp 0x1ef976
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x68(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %r8
movl -0x50(%rbp), %r9d
callq 0x1ebd50
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %r8
movl -0x50(%rbp), %r9d
callq 0x1ebd50
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x1ef8dc
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x38(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1ef8b3
jmp 0x1ef974
jmp 0x1ef8e3
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x8(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x1eeda0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1ef906
jmp 0x1ef972
movl -0x54(%rbp), %edi
movq -0x8(%rbp), %rsi
leaq -0x68(%rbp), %rdx
leaq -0x70(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x1eb960
movsd -0x68(%rbp), %xmm0
ucomisd -0x70(%rbp), %xmm0
jb 0x1ef947
movl -0x50(%rbp), %edx
movq -0x8(%rbp), %rax
orl 0x18(%rax), %edx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x1ef962
movl -0x50(%rbp), %edx
movq -0x8(%rbp), %rax
orl 0x1c(%rax), %edx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movl -0x54(%rbp), %ecx
movb $0x1, (%rax,%rcx)
jmp 0x1ef8e3
jmp 0x1ef974
jmp 0x1ef976
jmp 0x1ef978
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1ef722
addq $0x80, %rsp
popq %rbp
retq
nop