// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: trace_phase_path(ma_ug_t*, unsigned int, unsigned int, bub_p_t_warp*, min_cut_t*, unsigned char*, unsigned char)
uint64_t trace_phase_path(ma_ug_t *ug, uint32_t s, uint32_t d, bub_p_t_warp *b, min_cut_t* x, uint8_t* flag, uint8_t 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_rate, max_rate;
    ///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 = 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;

        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_hc_weight(w, v, s, b, x);
                t->ac = ac + (flag[(w>>1)] == 0? ug->u.a[(w>>1)].n : 0);
                t->uc = uc + (flag[(w>>1)] != 0? 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_hc_weight(w, v, s, b, x);
                cur_ac = ac + (flag[(w>>1)] == 0? ug->u.a[(w>>1)].n : 0);
                cur_uc = uc + (flag[(w>>1)] != 0? 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)
                    {
                        to_replace = 1;
                    }
                    else if(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;
                }


                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(b, s, flag, 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 = 0;
    }

    return n_pop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x68(%rsp)
movq %rcx, %rbx
movq %rdi, 0x40(%rsp)
movq 0x18(%rdi), %r12
movq 0x20(%r12), %rax
movl %esi, %ecx
shrl %ecx
movq %rcx, 0x90(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x101a97
movq %r9, 0x8(%rsp)
movq %r12, %rdi
movl %esi, %ebp
xorl %edx, %edx
callq 0xc2e7a
xorl %ecx, %ecx
cmpl $0x2, %eax
jl 0x101fa5
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
andq $0x0, 0x28(%rax,%rcx)
andq $0x0, 0x4(%rax,%rcx)
andq $0x0, 0x10(%rax,%rcx)
xorl %eax, %eax
cmpq %rax, 0x10(%rbx)
movl %ebp, %edx
je 0x101a9e
movq 0x18(%rbx), %rax
xorl %ecx, %ecx
jmp 0x101abe
xorl %ecx, %ecx
jmp 0x101fa5
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
movl %edx, %ebp
callq 0x2370
movl %ebp, %edx
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
movl %edx, 0x14(%rsp)
movl %edx, (%rax,%rcx,4)
incq %rcx
movq $0x0, 0x30(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x20(%rsp)
movq $0x0, 0x70(%rsp)
movl $0x0, 0x4(%rsp)
movq 0x8(%rsp), %rdx
movq %r12, 0x38(%rsp)
movq (%rbx), %rax
movq 0x18(%rbx), %rdi
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rdi,%rcx,4), %esi
imulq $0x38, %rsi, %rcx
movl 0x4(%rax,%rcx), %edi
movq %rdi, 0x48(%rsp)
movl 0x8(%rax,%rcx), %edi
movq %rdi, 0x60(%rsp)
movl 0x28(%rax,%rcx), %edi
movl %edi, 0x2c(%rsp)
movl 0x2c(%rax,%rcx), %edi
movl %edi, 0x28(%rsp)
movsd 0x10(%rax,%rcx), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x8(%r12), %rax
movq 0x28(%r12), %rcx
movq %rsi, 0x50(%rsp)
movq (%rcx,%rsi,8), %rcx
movl %ecx, %esi
movq %rsi, 0x88(%rsp)
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
addq $0xc, %rax
xorl %r13d, %r13d
movl $0x0, 0x10(%rsp)
cmpq %r13, 0x88(%rsp)
je 0x101ef6
movl -0x4(%rax), %r14d
movl %r14d, %ebp
shrl %ebp
cmpl 0x90(%rsp), %ebp
je 0x101f69
xorl %esi, %esi
testb $0x1, 0x70(%rsp)
je 0x101b9f
movl -0xc(%rax), %esi
cmpl $0x0, (%rax)
js 0x101ece
movq %rax, 0x78(%rsp)
imulq $0x38, %r14, %r15
addq (%rbx), %r15
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rax
cmpq 0x58(%rbx), %rcx
movq %rsi, 0x18(%rsp)
jne 0x101bed
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 %rax, 0x60(%rbx)
movq 0x50(%rbx), %rcx
movq 0x28(%r12), %rdx
movq 0x50(%rsp), %r12
movl 0x4(%rdx,%r12,8), %edx
movq %r13, 0x80(%rsp)
addl %r13d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x50(%rbx)
movl %edx, (%rax,%rcx,4)
movl 0x30(%r15), %r13d
testl %r13d, %r13d
js 0x101d39
movq 0x38(%rbx), %rcx
movq 0x48(%rbx), %rax
cmpq 0x40(%rbx), %rcx
jne 0x101c53
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 0x18(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rbx)
movl %r14d, (%rax,%rcx,4)
movl %r12d, (%r15)
orb $-0x80, 0x33(%r15)
addl 0x48(%rsp), %esi
movl %esi, 0x4(%r15)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbp, %rcx
movq %rcx, 0x18(%rsp)
movl 0x10(%rax,%rcx), %eax
addl 0x60(%rsp), %eax
movl %eax, 0x8(%r15)
movl %r14d, %esi
xorl $0x1, %esi
xorl %r12d, %r12d
movq 0x38(%rsp), %rdi
xorl %edx, %edx
callq 0xc2e7a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl 0x30(%r15), %r13d
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %r13d
orl %eax, %r13d
movl %r13d, 0x30(%r15)
movl %r14d, %edi
movq 0x50(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rcx
movq 0x68(%rsp), %r8
callq 0x10195a
addsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x10(%r15)
movq 0x8(%rsp), %rdx
cmpb $0x0, (%rdx,%rbp)
movl $0x0, %eax
jne 0x101d06
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
movl 0x10(%rax,%rcx), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x2c(%r15)
cmpb $0x0, (%rdx,%rbp)
je 0x101d27
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
movl 0x10(%rax,%rcx), %r12d
addl 0x2c(%rsp), %r12d
movl %r12d, 0x28(%r15)
incl 0x4(%rsp)
jmp 0x101e17
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbp, %rcx
movq %r12, %rsi
movl 0x10(%rax,%rcx), %r12d
movq 0x60(%rsp), %rax
addl %r12d, %eax
movl %eax, 0x24(%rsp)
movl %r14d, %edi
movl 0x14(%rsp), %edx
movq %rbx, %rcx
movq 0x68(%rsp), %r8
callq 0x10195a
movq 0x8(%rsp), %rdx
cmpb $0x0, (%rdx,%rbp)
movl $0x0, %eax
cmovel %r12d, %eax
movl $0x0, %ecx
cmovel %ecx, %r12d
addl 0x28(%rsp), %eax
cvtsi2sd %rax, %xmm1
addl 0x2c(%rsp), %r12d
leal (%rax,%r12), %ecx
cvtsi2sd %rcx, %xmm2
divsd %xmm2, %xmm1
movl 0x2c(%r15), %ecx
cvtsi2sd %rcx, %xmm2
movl 0x28(%r15), %esi
addl %ecx, %esi
cvtsi2sd %rsi, %xmm3
addsd 0x58(%rsp), %xmm0
divsd %xmm3, %xmm2
ucomisd %xmm2, %xmm1
movq 0x18(%rsp), %rsi
ja 0x101deb
jne 0x101e09
jp 0x101e09
ucomisd 0x10(%r15), %xmm0
ja 0x101deb
jne 0x101e09
jp 0x101e09
movl 0x24(%rsp), %ecx
cmpl 0x8(%r15), %ecx
jbe 0x101eda
movq 0x50(%rsp), %rcx
movl %ecx, (%r15)
movl 0x24(%rsp), %ecx
movl %ecx, 0x8(%r15)
movsd %xmm0, 0x10(%r15)
movl %eax, 0x2c(%r15)
movl %r12d, 0x28(%r15)
addl 0x48(%rsp), %esi
cmpl 0x4(%r15), %esi
jae 0x101e17
movl %esi, 0x4(%r15)
movq 0x38(%rsp), %r12
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 0x101e49
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %r13
jmp 0x101ece
movq %r12, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
movq 0x80(%rsp), %r13
je 0x101ea9
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x101e96
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 0x8(%rsp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl %r14d, (%rax,%rcx,4)
jmp 0x101ec5
movl %r14d, 0x20(%rsp)
cmpl $0x0, 0x10(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
movq 0x8(%rsp), %rdx
jne 0x101f69
decl 0x4(%rsp)
movq 0x78(%rsp), %rax
incq %r13
addq $0x18, %rax
jmp 0x101b6f
jne 0x101e09
movq 0x48(%rsp), %rcx
addl %esi, %ecx
cmpl 0x4(%r15), %ecx
ja 0x101deb
jmp 0x101e09
cmpl $0x1, 0x10(%rsp)
je 0x101f40
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x101f69
movb $0x1, %al
movq %rax, 0x70(%rsp)
cmpq $0x1, %rcx
jne 0x101af3
cmpl $0x0, 0x4(%rsp)
jne 0x101af3
movzbl 0xd0(%rsp), %ecx
movq %rbx, %rdi
movl 0x14(%rsp), %esi
callq 0x1019d3
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
jmp 0x101f69
movq $0x0, 0x30(%rsp)
movl 0x20(%rsp), %ebp
cmpl $-0x1, %ebp
je 0x101f69
cmpl $0x0, 0x4(%rsp)
jne 0x101f69
cmpq $0x0, 0x8(%rbx)
je 0x101fba
movq $0x0, 0x30(%rsp)
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x38(%rbx)
jbe 0x101fa0
movq (%rbx), %rcx
movq 0x48(%rbx), %rdx
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
andl $0x0, 0x30(%rcx,%rdx)
andq $0x0, 0x28(%rcx,%rdx)
andl $0x0, 0x8(%rcx,%rdx)
andq $0x0, (%rcx,%rdx)
andq $0x0, 0x10(%rcx,%rdx)
incl %eax
jmp 0x101f6b
movq 0x30(%rsp), %rcx
movq %rcx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x10(%rbx)
movq 0x18(%rbx), %rax
je 0x101fc9
xorl %ecx, %ecx
jmp 0x101fe9
movq $0x2, 0x10(%rbx)
pushq $0x8
popq %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%rbx)
movl %ebp, (%rax,%rcx,4)
jmp 0x101f22