// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: bub_phase(ma_ug_t*, unsigned int, unsigned int, bub_p_t_warp*, kv_u_trans_t*, ps_t*)
uint32_t bub_phase(ma_ug_t *ug, uint32_t beg, uint32_t end, bub_p_t_warp *b, kv_u_trans_t *ta, ps_t *s)
{
    asg_t *g = ug->g;
    if(g->seq[beg>>1].del) return 0; // already deleted
    if(get_real_length(g, beg, NULL)<2) return 0;
    uint32_t i, is_end, n_pending, to_replace, cur_nc, cur_uc, cur_ac, n_tips, tip_end, n_pop;
    double cur_nh, cur_w0, cur_w1, cur_rate, max_rate, cur_weight, min_weight;
    ///S saves nodes with all incoming edges visited
    b->S.n = b->T.n = b->b.n = b->e.n = 0;
    ///for each node, b->a saves all related information
    b->a[beg].d = b->a[beg].nc = b->a[beg].ac = b->a[beg].uc = 0; 
    b->a[beg].nh = b->a[beg].w[0] = b->a[beg].w[1] = 0;
    b->a[beg].p = (uint32_t)-1;
    ///b->S is the nodes with all incoming edges visited
    kv_push(uint32_t, b->S, beg);
    n_pop = n_tips = n_pending = 0;
    tip_end = (uint32_t)-1;

    do {
        ///v is a node that all incoming edges have been visited
        ///d is the distance from v0 to v
        uint32_t v = kv_pop(b->S);
        uint32_t d = b->a[v].d, nc = b->a[v].nc, uc = b->a[v].uc, ac = b->a[v].ac; 
        double nh = b->a[v].nh;///path weight
        double nw_0 = b->a[v].w[0], nw_1 = b->a[v].w[1];///weight to haplotype 1/2

        uint32_t nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        for (i = 0; i < nv; ++i) {
            uint32_t w = av[i].v, l = (uint32_t)av[i].ul; // v->w with length l, not overlap length
            bub_p_t *t = &b->a[w];
            is_end = 0;
            if((w>>1) == (end>>1)) is_end = 1;
            //got a circle
            if ((w>>1) == (beg>>1)) goto pop_reset;
            if (av[i].del) continue;
            ///push the edge
            kv_push(uint32_t, b->e, (g->idx[v]>>32) + i);

            if (t->s == 0) 
            { // this vertex has never been visited
                kv_push(uint32_t, b->b, w); // save it for revert
                ///t->p is the parent node of 
                ///t->s = 1 means w has been visited
                ///d is len(v0->v), l is len(v->w), so t->d is len(v0->w)
                t->p = v, t->s = 1, t->d = d + l; 
                t->r = get_real_length(g, w^1, NULL);

                if(is_end == 0)
                {
                    t->nc = nc + ug->u.a[(w>>1)].n;
                    t->nh = nh + get_path_phasing_weight(w, v, beg, b, ta);
                    get_related_phasing_weight(w>>1, ta, &(t->w[0]), &(t->w[1]), s->s);
                    t->w[0] += nw_0; t->w[1] += nw_1; 
                    t->ac = ac + ((s->s[w>>1] == 0)? ug->u.a[(w>>1)].n : 0);
                    t->uc = uc + ((s->s[w>>1] != 0)? ug->u.a[(w>>1)].n : 0);
                }

                ++n_pending;
            }
            else {
                to_replace = 0;

                if(is_end)
                {
                    cur_nc = nc; cur_nh = nh; 
                    cur_w0 = nw_0; cur_w1= nw_1;
                    cur_ac = ac; cur_uc = uc;
                }
                else
                {
                    cur_nc = nc + ug->u.a[(w>>1)].n;
                    cur_nh = nh + get_path_phasing_weight(w, v, beg, b, ta);
                    get_related_phasing_weight(w>>1, ta, &cur_w0, &cur_w1, s->s);
                    cur_w0 += nw_0; cur_w1 += nw_1;
                    cur_ac = ac + ((s->s[w>>1] == 0)? ug->u.a[(w>>1)].n : 0);
                    cur_uc = uc + ((s->s[w>>1] != 0)? ug->u.a[(w>>1)].n : 0);
                }
                
                cur_weight = cur_nh + MIN(cur_w0, cur_w1) - MAX(cur_w0, cur_w1);
                min_weight = t->nh + MIN(t->w[0], t->w[1]) - MAX(t->w[0], t->w[1]);
                cur_rate = ((cur_ac+cur_uc == 0)? -1 : ((double)(cur_ac)/(double)(cur_ac+cur_uc)));
                max_rate = ((t->ac+t->uc == 0)? -1 : ((double)(t->ac)/(double)(t->ac+t->uc)));

                if(cur_rate > max_rate)
                {
                    to_replace = 1;
                }
                else if(cur_rate == max_rate)
                {
                    if(cur_weight < min_weight)
                    {
                        to_replace = 1;
                    }
                    else if(cur_weight == min_weight)
                    {
                        if(cur_nc > t->nc)
                        {
                            to_replace = 1;
                        }
                        else if(cur_nc == t->nc)
                        {
                            if(d + l > t->d)
                            {
                                to_replace = 1;
                            }
                        }
                    }
                }


                if(to_replace)
                {
                    t->p = v;
                    t->nc = cur_nc;
                    t->nh = cur_nh;
                    t->ac = cur_ac;
                    t->uc = cur_uc;
                    t->w[0] = cur_w0;
                    t->w[1] = cur_w1;
                }


                if (d + l < t->d) t->d = d + l; // update dist
            }

            if (--(t->r) == 0) {
                uint32_t x = get_real_length(g, w, NULL);
                if(x > 0)
                {
                    kv_push(uint32_t, b->S, w);
                }
                else
                {
                    ///at most one tip
                    if(n_tips != 0) goto pop_reset;
                    n_tips++;
                    tip_end = w;
                }
                --n_pending;
            }
        }

        if(n_tips == 1)
        {
            if(tip_end != (uint32_t)-1 && n_pending == 0 && b->S.n == 0)
            {
                ///sink is b.S.a[0]
                kv_push(uint32_t, b->S, tip_end);
                break;
            }
            else
            {
                goto pop_reset;
            }
        }

        if (i < nv || b->S.n == 0) goto pop_reset;
    }while (b->S.n > 1 || n_pending);

    n_pop = 1;
    /**need fix**/
    set_phase_path(b, beg, ta, s);

    pop_reset:

    for (i = 0; i < b->b.n; ++i) { // clear the states of visited vertices
        bub_p_t *t = &b->a[b->b.a[i]];
        t->p = t->d = t->nc = t->ac = t->uc = t->r = t->s = 0;
        t->nh = t->w[0] = t->w[1] = 0;
    }
    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, %rbx
movl %edx, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x18(%rdi), %rdi
movq 0x20(%rdi), %rax
movl %esi, %ecx
shrl %ecx
movq %rcx, 0xb8(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x1110e9
movl %esi, %ebp
xorl %r14d, %r14d
movq %rdi, (%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x1110ec
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rax
movl %ebp, %ecx
imulq $0x38, %rcx, %rcx
andl $0x0, 0x8(%rax,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax,%rcx)
movupd %xmm0, 0x20(%rax,%rcx)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, (%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
je 0x111101
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x11111d
xorl %r14d, %r14d
movl %r14d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %ebp, (%rax,%rcx,4)
incq %rcx
shrl 0x20(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %r14d, %r14d
movl %ebp, 0x4c(%rsp)
movq (%rsp), %rdi
movq (%rbx), %rax
movq 0x18(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdx,%rcx,4), %edx
imulq $0x38, %rdx, %rcx
movl 0x4(%rax,%rcx), %esi
movq %rsi, 0x70(%rsp)
movl 0x8(%rax,%rcx), %esi
movl %esi, 0x1c(%rsp)
movl 0x28(%rax,%rcx), %esi
movl %esi, 0x18(%rsp)
movl 0x2c(%rax,%rcx), %esi
movl %esi, 0x14(%rsp)
movsd 0x10(%rax,%rcx), %xmm0
movsd %xmm0, 0x68(%rsp)
movupd 0x18(%rax,%rcx), %xmm0
movq 0x8(%rdi), %rax
movq 0x28(%rdi), %rcx
movq %rdx, 0x40(%rsp)
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq %rdx, 0xb0(%rsp)
shrq $0x20, %rcx
movapd %xmm0, 0x90(%rsp)
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0xc0(%rsp)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
addq $0xc, %rax
xorl %r8d, %r8d
movl $0x0, 0x10(%rsp)
cmpq %r8, 0xb0(%rsp)
je 0x111731
movl -0x4(%rax), %r13d
movl %r13d, %r12d
shrl %r12d
cmpl 0xb8(%rsp), %r12d
je 0x11179d
cmpl $0x0, (%rax)
js 0x111708
movq $0x0, 0x28(%rsp)
movl %r14d, 0x24(%rsp)
movq %rax, 0x88(%rsp)
movl -0xc(%rax), %r14d
imulq $0x38, %r13, %r15
addq (%rbx), %r15
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movq %r8, 0x30(%rsp)
jne 0x111260
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x30(%rsp), %r8
movq (%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%rdi), %rdx
movq 0x40(%rsp), %rsi
movl 0x4(%rdx,%rsi,8), %edx
addl %r8d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
cmpl $0x0, 0x30(%r15)
js 0x1113e8
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x1112be
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %rdi
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r13d, (%rax,%rcx,4)
movq 0x40(%rsp), %rax
movl %eax, (%r15)
orb $-0x80, 0x33(%r15)
addl 0x70(%rsp), %r14d
movl %r14d, 0x4(%r15)
movl %r13d, %esi
xorl $0x1, %esi
xorl %edx, %edx
callq 0xc2e7a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x30(%r15), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x30(%r15)
cmpl 0x20(%rsp), %r12d
je 0x1113d2
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r12, %rcx
movq %rcx, 0x50(%rsp)
movl 0x10(%rax,%rcx), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x8(%r15)
movl %r13d, %edi
movq 0x40(%rsp), %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq 0x60(%rsp), %rbp
movq %rbp, %r8
callq 0x110e1d
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%r15)
leaq 0x18(%r15), %rdx
leaq 0x20(%r15), %rcx
movq 0x58(%rsp), %r14
movq (%r14), %r8
movl %r12d, %edi
movq %rbp, %rsi
callq 0x110eb7
movupd 0x18(%r15), %xmm0
addpd 0x90(%rsp), %xmm0
movupd %xmm0, 0x18(%r15)
movq (%r14), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx,%r12)
movl $0x0, %edx
jne 0x1113a5
movq 0x38(%rsp), %rdx
movq 0x10(%rdx), %rdx
movq 0x50(%rsp), %rsi
movl 0x10(%rdx,%rsi), %edx
addl 0x14(%rsp), %edx
movl %edx, 0x2c(%r15)
cmpb $0x0, (%rcx,%r12)
je 0x1113c6
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rsp), %rcx
movl 0x10(%rax,%rcx), %eax
addl 0x18(%rsp), %eax
movl %eax, 0x28(%r15)
movl 0x4c(%rsp), %ebp
movl 0x24(%rsp), %r14d
incl %r14d
movq (%rsp), %rdi
movq 0x30(%rsp), %r8
jmp 0x111668
cmpl 0x20(%rsp), %r12d
jne 0x111426
movapd 0x90(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movaps 0xc0(%rsp), %xmm1
movsd %xmm1, 0x78(%rsp)
movsd 0x68(%rsp), %xmm6
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %esi
jmp 0x111512
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r12, %rcx
movq %rcx, 0xa8(%rsp)
movq %r14, 0x50(%rsp)
movl 0x10(%rax,%rcx), %r14d
addl 0x1c(%rsp), %r14d
movl %r13d, %edi
movq 0x40(%rsp), %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq 0x60(%rsp), %rbp
movq %rbp, %r8
callq 0x110e1d
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %r8
movl %r12d, %edi
movq %rbp, %rsi
movl 0x4c(%rsp), %ebp
leaq 0x80(%rsp), %rdx
leaq 0x78(%rsp), %rcx
callq 0x110eb7
movsd 0xa0(%rsp), %xmm6
movl %r14d, %esi
movq 0x50(%rsp), %r14
movq 0x30(%rsp), %r8
movq (%rsp), %rdi
movsd 0x80(%rsp), %xmm0
movhpd 0x78(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
addpd 0x90(%rsp), %xmm0
movlpd %xmm0, 0x80(%rsp)
movhpd %xmm0, 0x78(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
cmpb $0x0, (%rax,%r12)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movq 0xa8(%rsp), %rcx
movl 0x10(%rax,%rcx), %eax
movl $0x0, %ecx
cmovel %eax, %ecx
movl $0x0, %edx
cmovel %edx, %eax
addl 0x14(%rsp), %ecx
addl 0x18(%rsp), %eax
movd 0x28(%r15), %xmm1
movd %eax, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movd 0x2c(%r15), %xmm1
movd %ecx, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
paddd %xmm3, %xmm2
pshufd $0x50, %xmm2, %xmm1     # xmm1 = xmm2[0,0,1,1]
pxor %xmm4, %xmm4
pcmpeqd %xmm4, %xmm1
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
movdqa 0xd6685(%rip), %xmm5    # 0x1e7bd0
por %xmm5, %xmm3
subpd %xmm5, %xmm3
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
por %xmm5, %xmm2
subpd %xmm5, %xmm2
divpd %xmm2, %xmm3
movdqa %xmm1, %xmm2
pandn %xmm3, %xmm2
pand 0xdbb6d(%rip), %xmm1    # 0x1ed0e0
por %xmm2, %xmm1
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
ucomisd %xmm2, %xmm1
ja 0x111634
ucomisd %xmm2, %xmm1
jne 0x111654
jp 0x111654
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
movapd %xmm0, %xmm1
cmplesd %xmm2, %xmm1
movapd %xmm1, %xmm3
andpd %xmm0, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
addsd %xmm6, %xmm1
movapd %xmm2, %xmm3
cmplesd %xmm0, %xmm3
movapd %xmm3, %xmm4
andpd %xmm0, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
subsd %xmm3, %xmm1
movsd 0x18(%r15), %xmm3
movsd 0x20(%r15), %xmm4
movapd %xmm3, %xmm2
cmplesd %xmm4, %xmm2
movapd %xmm2, %xmm5
andpd %xmm3, %xmm5
andnpd %xmm4, %xmm2
orpd %xmm5, %xmm2
addsd 0x10(%r15), %xmm2
movapd %xmm4, %xmm5
cmplesd %xmm3, %xmm5
andpd %xmm5, %xmm3
andnpd %xmm4, %xmm5
orpd %xmm3, %xmm5
subsd %xmm5, %xmm2
ucomisd %xmm1, %xmm2
ja 0x111634
ucomisd %xmm2, %xmm1
jne 0x111654
jp 0x111654
cmpl 0x8(%r15), %esi
jbe 0x111714
movq 0x40(%rsp), %rdx
movl %edx, (%r15)
movl %esi, 0x8(%r15)
movsd %xmm6, 0x10(%r15)
movl %ecx, 0x2c(%r15)
movl %eax, 0x28(%r15)
movupd %xmm0, 0x18(%r15)
addl 0x70(%rsp), %r14d
cmpl 0x4(%r15), %r14d
jae 0x111663
movl %r14d, 0x4(%r15)
movl 0x24(%rsp), %r14d
movl 0x30(%r15), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x30(%r15)
testl %ecx, %ecx
jne 0x111700
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0x1116dd
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x1116c6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movq (%rsp), %rdi
movq 0x30(%rsp), %r8
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, (%rax,%rcx,4)
jmp 0x1116fd
movl %r13d, 0xc(%rsp)
cmpl $0x0, 0x10(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
movq (%rsp), %rdi
movq 0x30(%rsp), %r8
jne 0x1117a6
decl %r14d
movq 0x88(%rsp), %rax
incq %r8
addq $0x18, %rax
jmp 0x1111ce
jne 0x111654
movq 0x70(%rsp), %rdx
addl %r14d, %edx
cmpl 0x4(%r15), %edx
ja 0x111634
jmp 0x111654
cmpl $0x1, 0x10(%rsp)
je 0x111772
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x11179d
cmpq $0x1, %rcx
jne 0x111139
testl %r14d, %r14d
jne 0x111139
movq %rbx, %rdi
movl %ebp, %esi
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x110f25
pushq $0x1
popq %rax
movq %rax, 0x28(%rsp)
jmp 0x1117a6
cmpl $-0x1, 0xc(%rsp)
je 0x11179d
movq $0x0, 0x28(%rsp)
testl %r14d, %r14d
jne 0x1117a6
cmpq $0x0, 0x8(%rbx)
jne 0x1117a6
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x1117ea
xorl %ecx, %ecx
jmp 0x111805
movq $0x0, 0x28(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %r14
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0x1110ec
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
andl $0x0, 0x8(%rcx,%rdx)
andq $0x0, (%rcx,%rdx)
incl %eax
movupd %xmm0, 0x10(%rcx,%rdx)
movupd %xmm0, 0x20(%rcx,%rdx)
andl $0x0, 0x30(%rcx,%rdx)
jmp 0x1117b1
movq $0x2, 0x10(%rbx)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl 0xc(%rsp), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x111754