// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_phase_path(ma_ug_t*, unsigned int, unsigned int, bub_p_t_warp*, H_partition*, unsigned int)
uint64_t get_phase_path(ma_ug_t *ug, uint32_t s, uint32_t d, bub_p_t_warp *b, H_partition* hap, uint32_t max_hap_label)
{
    asg_t *g = ug->g;
    if(g->seq[s>>1].del) return 0; // already deleted
    if(get_real_length(g, s, NULL)<2) return 0;
    uint32_t i, n_pending, is_first, 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, max_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[s].d = b->a[s].nc = b->a[s].ac = b->a[s].uc = 0; b->a[s].nh = b->a[s].w[0] = b->a[s].w[1] = 0;
    ///b->S is the nodes with all incoming edges visited
    kv_push(uint32_t, b->S, s);
    n_pop = n_tips = n_pending = 0;
    tip_end = (uint32_t)-1;
    is_first = 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;
        double nw_0 = b->a[v].w[0], nw_1 = b->a[v].w[1];

        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];
            //got a circle
            if ((w>>1) == (s>>1)) goto pop_reset;
            //important when poping at long untig graph
            if(is_first) l = 0;
            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->nc = nc + ug->u.a[(w>>1)].n;
                t->r = get_real_length(g, w^1, NULL);
                /**need fix**/
                t->nh = nh + get_path_weight(w, v, s, b, hap->link);

                get_related_weight(w>>1, hap, &(t->w[0]), &(t->w[1]), &max_hap_label);
                t->w[0] += nw_0; t->w[1] += nw_1; 

                t->ac = ac + ((!is_hap_set_label(w>>1, *hap, max_hap_label))?ug->u.a[(w>>1)].n : 0);
                t->uc = uc + ((is_hap_set_label(w>>1, *hap, max_hap_label))?ug->u.a[(w>>1)].n : 0);
                
                ++n_pending;
            }
            else {
                to_replace = 0;

                cur_nc = nc + ug->u.a[(w>>1)].n;
                /**need fix**/
                cur_nh = nh + get_path_weight(w, v, s, b, hap->link);
                get_related_weight(w>>1, hap, &cur_w0, &cur_w1, &max_hap_label);
                cur_w0 += nw_0; cur_w1 += nw_1;
                cur_weight = cur_nh + MAX(cur_w0, cur_w1) - MIN(cur_w0, cur_w1);
                max_weight = t->nh + MAX(t->w[0], t->w[1]) - MIN(t->w[0], t->w[1]);

                cur_ac = ac + ((!is_hap_set_label(w>>1, *hap, max_hap_label))?ug->u.a[(w>>1)].n : 0);;
                cur_uc = uc + ((is_hap_set_label(w>>1, *hap, max_hap_label))?ug->u.a[(w>>1)].n : 0);
                cur_rate = ((double)(cur_ac)/(double)(cur_ac+cur_uc));
                max_rate = ((double)(t->ac)/(double)(t->ac+t->uc));

                if(cur_rate > max_rate)
                {
                    to_replace = 1;
                }
                else if(cur_rate == max_rate)
                {
                    ///if(cur_nh > t->nh)
                    if(cur_weight > max_weight)
                    {
                        to_replace = 1;
                    }
                    else if(cur_weight == max_weight)///(cur_nh == t->nh)
                    {
                        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;
            }
        }
        is_first = 0;


        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_path_hap(b, s, hap, max_hap_label);
    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 $0xc8, %rsp
movq %r8, 0x40(%rsp)
movq %rcx, %rbx
movl %r9d, 0x8(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x18(%rdi), %r13
movq 0x20(%r13), %rax
movl %esi, %ecx
shrl %ecx
movq %rcx, 0xa8(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x10b727
xorl %r14d, %r14d
movq %r13, %rdi
xorl %edx, %edx
movl %esi, %ebp
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x10b72a
andq $0x0, 0x50(%rbx)
andq $0x0, 0x38(%rbx)
andq $0x0, 0x20(%rbx)
andq $0x0, 0x8(%rbx)
movq (%rbx), %rax
movl %ebp, 0x10(%rsp)
movl %ebp, %ecx
imulq $0x38, %rcx, %rcx
andq $0x0, 0x4(%rax,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax,%rcx)
movupd %xmm0, 0x20(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
je 0x10b73f
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x10b75b
xorl %r14d, %r14d
movq %r14, %rax
addq $0xc8, %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 0x10(%rsp), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
xorl %r14d, %r14d
pushq $-0x1
popq %rax
movl %eax, 0x2c(%rsp)
movq $0x0, 0x70(%rsp)
movl $0x0, 0xc(%rsp)
movq %r13, 0x48(%rsp)
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, 0x58(%rsp)
movl 0x8(%rax,%rcx), %esi
movl %esi, 0x3c(%rsp)
movl 0x28(%rax,%rcx), %esi
movl %esi, 0x38(%rsp)
movl 0x2c(%rax,%rcx), %esi
movl %esi, 0x34(%rsp)
movsd 0x10(%rax,%rcx), %xmm0
movsd %xmm0, 0x68(%rsp)
movupd 0x18(%rax,%rcx), %xmm0
movapd %xmm0, 0xb0(%rsp)
movq 0x8(%r13), %rax
movq 0x28(%r13), %rcx
movq %rdx, 0x60(%rsp)
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq %rdx, 0xa0(%rsp)
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
addq $0xc, %rax
xorl %edi, %edi
movl $0x0, 0x14(%rsp)
cmpq %rdi, 0xa0(%rsp)
je 0x10bd25
movl -0x4(%rax), %r12d
movl %r12d, %ebp
shrl %ebp
cmpl 0xa8(%rsp), %ebp
je 0x10bd89
xorl %edx, %edx
testb $0x1, 0x70(%rsp)
je 0x10b83c
movl -0xc(%rax), %edx
cmpl $0x0, (%rax)
js 0x10bcfd
movq %rax, 0x78(%rsp)
imulq $0x38, %r12, %r15
addq (%rbx), %r15
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movq %rdi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
jne 0x10b894
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 0x20(%rsp), %rdi
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x60(%rsp), %r14
movq 0x28(%r13), %rdx
movl 0x4(%rdx,%r14,8), %edx
addl %edi, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
movl 0x30(%r15), %r13d
testl %r13d, %r13d
js 0x10ba46
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x10b8f0
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 %rax, 0x48(%rbx)
movq 0x38(%rbx), %rcx
movq 0x48(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r12d, (%rax,%rcx,4)
movl %r14d, (%r15)
orb $-0x80, 0x33(%r15)
addl 0x58(%rsp), %esi
movl %esi, 0x4(%r15)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbp, %rcx
movq %rcx, 0x18(%rsp)
movl 0x10(%rax,%rcx), %eax
addl 0x3c(%rsp), %eax
movl %eax, 0x8(%r15)
movl %r12d, %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)
movq 0x40(%rsp), %r13
movq 0x30(%r13), %r8
movl %r12d, %edi
movl %r14d, %esi
movl 0x10(%rsp), %edx
movq %rbx, %rcx
callq 0x10b42b
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%r15)
leaq 0x18(%r15), %rdx
leaq 0x20(%r15), %rcx
movl %ebp, %edi
movq %r13, %rsi
leaq 0x8(%rsp), %r8
callq 0x10b4a6
movupd 0x18(%r15), %xmm0
addpd 0xb0(%rsp), %xmm0
movupd %xmm0, 0x18(%r15)
movq 0x10(%r13), %rax
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
movl (%rax,%rbp,4), %edx
orl 0x20(%r13), %ecx
testl %edx, %ecx
je 0x10b9d8
movb 0x2c(%r13), %cl
shrl %cl, %edx
movl 0x8(%rsp), %esi
shrl %cl, %esi
xorl %ecx, %ecx
cmpl %esi, %edx
movq 0x40(%rsp), %r13
je 0x10b9ea
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x18(%rsp), %rdx
movl 0x10(%rcx,%rdx), %ecx
addl 0x34(%rsp), %ecx
movl %ecx, 0x2c(%r15)
movl (%rax,%rbp,4), %edx
movl 0x1c(%r13), %ecx
orl 0x18(%r13), %ecx
orl 0x20(%r13), %ecx
xorl %eax, %eax
testl %edx, %ecx
movq 0x20(%rsp), %rdi
je 0x10ba2e
movb 0x2c(%r13), %cl
shrl %cl, %edx
movl 0x8(%rsp), %esi
shrl %cl, %esi
cmpl %esi, %edx
jne 0x10ba2e
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
movl 0x10(%rax,%rcx), %eax
addl 0x38(%rsp), %eax
movl %eax, 0x28(%r15)
incl 0xc(%rsp)
movl 0x30(%r15), %r13d
xorl %r14d, %r14d
jmp 0x10bc59
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movl %ebp, %ecx
movq %rcx, 0x90(%rsp)
imulq $0x28, %rcx, %rcx
movl 0x10(%rax,%rcx), %eax
movl %eax, 0x30(%rsp)
movq %r14, %rsi
movq 0x40(%rsp), %r14
movq 0x30(%r14), %r8
movl %r12d, %edi
movl 0x10(%rsp), %edx
movq %rbx, %rcx
callq 0x10b42b
movsd %xmm0, 0x98(%rsp)
movl %ebp, %edi
movq %r14, %rsi
leaq 0x88(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x10b4a6
movsd 0x88(%rsp), %xmm0
movhpd 0x80(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
addpd 0xb0(%rsp), %xmm0
movlpd %xmm0, 0x88(%rsp)
movhpd %xmm0, 0x80(%rsp)
movq 0x10(%r14), %rax
movl 0x1c(%r14), %ecx
orl 0x18(%r14), %ecx
movq 0x90(%rsp), %rdx
movl (%rax,%rdx,4), %eax
orl 0x20(%r14), %ecx
testl %eax, %ecx
je 0x10bb1f
movb 0x2c(%r14), %cl
shrl %cl, %eax
movl 0x8(%rsp), %edx
shrl %cl, %edx
cmpl %edx, %eax
movl 0x30(%rsp), %r8d
movl %r8d, %eax
movl $0x0, %r14d
cmovel %r14d, %eax
movl $0x0, %ecx
cmovel %r8d, %ecx
jmp 0x10bb2c
xorl %ecx, %ecx
movl 0x30(%rsp), %r8d
movl %r8d, %eax
xorl %r14d, %r14d
addl 0x3c(%rsp), %r8d
addl 0x34(%rsp), %eax
cvtsi2sd %rax, %xmm1
addl 0x38(%rsp), %ecx
leal (%rcx,%rax), %edx
cvtsi2sd %rdx, %xmm2
divsd %xmm2, %xmm1
movl 0x2c(%r15), %edx
cvtsi2sd %rdx, %xmm2
movl 0x28(%r15), %esi
addl %edx, %esi
cvtsi2sd %rsi, %xmm3
movsd 0x98(%rsp), %xmm6
addsd 0x68(%rsp), %xmm6
divsd %xmm3, %xmm2
ucomisd %xmm2, %xmm1
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
ja 0x10bc2b
jne 0x10bc4b
jp 0x10bc4b
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
movapd %xmm2, %xmm1
cmplesd %xmm0, %xmm1
movapd %xmm1, %xmm3
andpd %xmm0, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
addsd %xmm6, %xmm1
movapd %xmm0, %xmm3
cmplesd %xmm2, %xmm3
movapd %xmm3, %xmm4
andpd %xmm0, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
subsd %xmm3, %xmm1
movsd 0x18(%r15), %xmm2
movsd 0x20(%r15), %xmm3
movapd %xmm3, %xmm4
cmplesd %xmm2, %xmm4
movapd %xmm4, %xmm5
andpd %xmm2, %xmm5
andnpd %xmm3, %xmm4
orpd %xmm5, %xmm4
addsd 0x10(%r15), %xmm4
movapd %xmm2, %xmm5
cmplesd %xmm3, %xmm5
andpd %xmm5, %xmm2
andnpd %xmm3, %xmm5
orpd %xmm2, %xmm5
subsd %xmm5, %xmm4
ucomisd %xmm4, %xmm1
ja 0x10bc2b
jne 0x10bc4b
jp 0x10bc4b
cmpl 0x8(%r15), %r8d
jbe 0x10bd09
movq 0x60(%rsp), %rdx
movl %edx, (%r15)
movl %r8d, 0x8(%r15)
movsd %xmm6, 0x10(%r15)
movl %eax, 0x2c(%r15)
movl %ecx, 0x28(%r15)
movupd %xmm0, 0x18(%r15)
addl 0x58(%rsp), %esi
cmpl 0x4(%r15), %esi
jae 0x10bc59
movl %esi, 0x4(%r15)
leal -0x1(%r13), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %r13d      # imm = 0x80000000
orl %eax, %r13d
movl %r13d, 0x30(%r15)
testl %eax, %eax
je 0x10bc7b
movq 0x48(%rsp), %r13
jmp 0x10bcf8
movq 0x48(%rsp), %r13
movq %r13, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
je 0x10bcd8
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x10bcc5
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 0x20(%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r12d, (%rax,%rcx,4)
jmp 0x10bcf4
movl %r12d, 0x2c(%rsp)
cmpl $0x0, 0x14(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rdi
jne 0x10bd89
decl 0xc(%rsp)
movq 0x78(%rsp), %rax
incq %rdi
addq $0x18, %rax
jmp 0x10b80c
jne 0x10bc4b
movq 0x58(%rsp), %rdx
addl %esi, %edx
cmpl 0x4(%r15), %edx
ja 0x10bc2b
jmp 0x10bc4b
cmpl $0x1, 0x14(%rsp)
je 0x10bd6c
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x10bd89
movb $0x1, %al
movq %rax, 0x70(%rsp)
cmpq $0x1, %rcx
jne 0x10b785
cmpl $0x0, 0xc(%rsp)
jne 0x10b785
movl 0x8(%rsp), %ecx
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movq 0x40(%rsp), %rdx
callq 0x10b584
pushq $0x1
popq %r14
jmp 0x10bd89
xorl %r14d, %r14d
movl 0x2c(%rsp), %ebp
cmpl $-0x1, %ebp
je 0x10bd89
cmpl $0x0, 0xc(%rsp)
jne 0x10bd89
cmpq $0x0, 0x8(%rbx)
je 0x10bdc8
xorl %r14d, %r14d
xorl %eax, %eax
xorpd %xmm0, %xmm0
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0x10b72a
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 0x10bd8f
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x10bdd7
xorl %ecx, %ecx
jmp 0x10bdf2
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 %ebp, (%rax,%rcx,4)
jmp 0x10bd51