// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, %rbx
movq %rdi, 0x50(%rsp)
movq 0x18(%rdi), %r9
movq 0x20(%r9), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1430ec
movl %esi, %r14d
movq %rcx, 0x38(%rsp)
movq 0x28(%r9), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rdx
xorl %eax, %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rdx, %rsi
je 0x1430ee
movq 0x8(%r9), %rdi
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rdi,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
movl (%rdx,%rdi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %eax
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x1430d0
cmpl $0x2, %eax
jge 0x143100
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
movq %rdx, 0x50(%rbx)
movq %rdx, 0x38(%rbx)
movq %rdx, 0x20(%rbx)
movq %rdx, 0x8(%rbx)
movq (%rbx), %rax
imulq $0x38, %rcx, %rcx
movq %rdx, 0x28(%rax,%rcx)
movq %rdx, 0x4(%rax,%rcx)
movq %rdx, 0x10(%rax,%rcx)
cmpq %rdx, 0x10(%rbx)
movq %r9, 0x18(%rsp)
jne 0x143152
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x18(%rsp), %r9
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rbx)
movl %r14d, (%rcx,%rax,4)
movl $0x1, 0x14(%rsp)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movq %rbx, 0x68(%rsp)
movl %r14d, 0xc(%rsp)
movq 0x18(%rbx), %rax
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, 0x8(%rbx)
movl -0x4(%rax,%rdx,4), %esi
movq 0x28(%r9), %rax
movq (%rax,%rsi,8), %rax
testl %eax, %eax
je 0x14364f
imulq $0x38, %rsi, %rdx
movl 0x4(%rcx,%rdx), %edi
movq %rdi, 0x60(%rsp)
movl 0x8(%rcx,%rdx), %edi
movq %rdi, 0x80(%rsp)
movl 0x28(%rcx,%rdx), %edi
movl %edi, 0x34(%rsp)
movl 0x2c(%rcx,%rdx), %edi
movl %edi, 0x30(%rsp)
movsd 0x10(%rcx,%rdx), %xmm0
movsd %xmm0, 0x78(%rsp)
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x3, %rdi
addq 0x8(%r9), %rdi
movl %eax, %edx
xorl %r12d, %r12d
movq 0x38(%rsp), %rcx
movq %rsi, 0x20(%rsp)
movq %rdi, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
leaq (%r12,%r12,2), %rax
movl 0x8(%rdi,%rax,8), %r13d
movl %r13d, %r15d
shrl %r15d
cmpl %ecx, %r15d
jne 0x143236
movl $0x9, %ebp
jmp 0x14361b
leaq (%rdi,%rax,8), %rax
xorl %r14d, %r14d
cmpl $0x0, 0x14(%rsp)
jne 0x143247
movl (%rax), %r14d
cmpl $0x0, 0xc(%rax)
js 0x143391
imulq $0x38, %r13, %rbp
addq (%rbx), %rbp
movq 0x58(%rbx), %rax
cmpq %rax, 0x50(%rbx)
jne 0x143291
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x58(%rbx)
movq 0x60(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r9
movq %rax, 0x60(%rbx)
movq 0x28(%r9), %rax
movl 0x4(%rax,%rsi,8), %eax
addl %r12d, %eax
movq 0x50(%rbx), %rcx
movq 0x60(%rbx), %rdx
leaq 0x1(%rcx), %rdi
movq %rdi, 0x50(%rbx)
movl %eax, (%rdx,%rcx,4)
cmpl $0x0, 0x30(%rbp)
js 0x14339b
movq 0x40(%rbx), %rax
cmpq %rax, 0x38(%rbx)
jne 0x1432f2
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x40(%rbx)
movq 0x48(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r9
movq %rax, 0x48(%rbx)
movq 0x38(%rbx), %rax
movq 0x48(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x38(%rbx)
movl %r13d, (%rcx,%rax,4)
movl %esi, (%rbp)
orb $-0x80, 0x33(%rbp)
addl 0x60(%rsp), %r14d
movl %r14d, 0x4(%rbp)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r15,%r15,4), %r14
movl 0x10(%rax,%r14,8), %eax
addl 0x80(%rsp), %eax
movl %eax, 0x8(%rbp)
movq %r13, %rax
xorq $0x1, %rax
movq 0x28(%r9), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0x143484
movq 0x8(%r9), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
xorl %r8d, %r8d
xorl %eax, %eax
movl (%rcx,%r8), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %eax
addq $0x18, %r8
cmpq %r8, %rdx
jne 0x143373
orl $0x80000000, %eax       # imm = 0x80000000
jmp 0x143489
movl $0x8, %ebp
jmp 0x14361b
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r15,%r15,4), %rdx
movq %rbx, %rcx
movl 0x10(%rax,%rdx,8), %ebx
movq 0x80(%rsp), %rax
addl %ebx, %eax
movl %eax, 0x2c(%rsp)
movl %r13d, %edi
movl 0xc(%rsp), %edx
movq 0x70(%rsp), %r8
callq 0x142fb8
movq 0x20(%rsp), %rsi
movq 0x58(%rsp), %rax
cmpb $0x0, (%rax,%r15)
movl $0x0, %eax
cmovel %ebx, %eax
movl $0x0, %ecx
cmovel %ecx, %ebx
addl 0x30(%rsp), %eax
cvtsi2sd %rax, %xmm1
addl 0x34(%rsp), %ebx
leal (%rax,%rbx), %ecx
cvtsi2sd %rcx, %xmm2
divsd %xmm2, %xmm1
movl 0x2c(%rbp), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl 0x28(%rbp), %edx
addl %ecx, %edx
cvtsi2sd %rdx, %xmm3
addsd 0x78(%rsp), %xmm0
divsd %xmm3, %xmm2
ucomisd %xmm2, %xmm1
ja 0x143447
jne 0x14345c
jp 0x14345c
ucomisd 0x10(%rbp), %xmm0
ja 0x143447
jne 0x14345c
jp 0x14345c
movl 0x2c(%rsp), %ecx
cmpl 0x8(%rbp), %ecx
jbe 0x143633
movl %esi, (%rbp)
movl 0x2c(%rsp), %ecx
movl %ecx, 0x8(%rbp)
movsd %xmm0, 0x10(%rbp)
movl %eax, 0x2c(%rbp)
movl %ebx, 0x28(%rbp)
addl 0x60(%rsp), %r14d
cmpl 0x4(%rbp), %r14d
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
jae 0x143475
movl %r14d, 0x4(%rbp)
movq 0x68(%rsp), %rbx
movq 0x18(%rsp), %r9
jmp 0x143507
movl $0x80000000, %eax       # imm = 0x80000000
movl %eax, 0x30(%rbp)
movl %r13d, %edi
movl 0xc(%rsp), %edx
movq %rbx, %rcx
movq 0x70(%rsp), %r8
callq 0x142fb8
addsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x10(%rbp)
xorl %eax, %eax
movq 0x58(%rsp), %rdx
cmpb $0x0, (%rdx,%r15)
movl $0x0, %ecx
jne 0x1434cc
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rcx,%r14,8), %ecx
addl 0x30(%rsp), %ecx
movl %ecx, 0x2c(%rbp)
cmpb $0x0, (%rdx,%r15)
movq 0x40(%rsp), %rdi
je 0x1434ed
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%r14,8), %eax
addl 0x34(%rsp), %eax
movl %eax, 0x28(%rbp)
incl 0x8(%rsp)
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %rsi
movq 0x48(%rsp), %rdx
movl 0x30(%rbp), %eax
leal -0x1(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl $0x80000000, %eax       # imm = 0x80000000
orl %ecx, %eax
movl %eax, 0x30(%rbp)
xorl %ebp, %ebp
testl %ecx, %ecx
je 0x14352d
movq 0x38(%rsp), %rcx
jmp 0x14361b
movq 0x28(%r9), %rax
movq (%rax,%r13,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0x1435e9
movq 0x8(%r9), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %r8d, %r8d
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %r8d
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x143567
testl %r8d, %r8d
movq 0x40(%rsp), %rdi
je 0x1435e9
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x1435c3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
movq 0x18(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r9
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, (%rcx,%rax,4)
movl 0x10(%rsp), %eax
movl %eax, %r13d
movl 0x4(%rsp), %eax
movq 0x38(%rsp), %rcx
jmp 0x143609
movl $0x1, %eax
cmpl $0x0, 0x4(%rsp)
movq 0x38(%rsp), %rcx
je 0x143609
movl $0x1, 0x4(%rsp)
movl $0x9, %ebp
jmp 0x143616
decl 0x8(%rsp)
movl %r13d, 0x10(%rsp)
movl %eax, 0x4(%rsp)
movq 0x48(%rsp), %rdx
testb $0x7, %bpl
jne 0x14370a
incq %r12
cmpq %rdx, %r12
jne 0x143218
jmp 0x14364f
jne 0x14345c
movq 0x60(%rsp), %rcx
addl %r14d, %ecx
cmpl 0x4(%rbp), %ecx
ja 0x143447
jmp 0x14345c
cmpl $0x1, 0x4(%rsp)
jne 0x1436ed
movl $0x0, 0x14(%rsp)
movl $0x9, %ebp
movl $0x1, 0x4(%rsp)
cmpl $-0x1, 0x10(%rsp)
movl 0xc(%rsp), %r14d
movb 0xc0(%rsp), %r15b
je 0x143717
cmpl $0x0, 0x8(%rsp)
jne 0x143717
cmpq $0x0, 0x8(%rbx)
jne 0x143717
cmpq $0x0, 0x10(%rbx)
jne 0x1436bf
movq $0x2, 0x10(%rbx)
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x18(%rsp), %r9
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rbx)
movl 0x10(%rsp), %edx
movl %edx, (%rcx,%rax,4)
movl $0x4, %ebp
movl $0x0, 0x14(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0x143717
xorl %eax, %eax
cmpq $0x0, 0x8(%rbx)
sete %al
leal (%rax,%rax,8), %ebp
movl $0x0, 0x4(%rsp)
movl $0x0, 0x14(%rsp)
movl 0xc(%rsp), %r14d
movb 0xc0(%rsp), %r15b
testl %ebp, %ebp
jne 0x143733
cmpq $0x1, 0x8(%rbx)
ja 0x143190
cmpl $0x0, 0x8(%rsp)
jne 0x143190
jmp 0x143738
cmpl $0x4, %ebp
jne 0x14376f
movq 0x18(%rbx), %rax
movl (%rax), %eax
movq 0x58(%rsp), %rsi
movq (%rbx), %rcx
movl %eax, %edx
imulq $0x38, %rdx, %rdx
movl (%rcx,%rdx), %ecx
shrl %eax
orb %r15b, (%rsi,%rax)
movl %ecx, %eax
cmpl %r14d, %ecx
jne 0x143743
movq 0x18(%rbx), %rax
movl (%rax), %eax
shrl %eax
movb $0x0, (%rsi,%rax)
movl $0x1, %eax
jmp 0x143771
xorl %eax, %eax
cmpq $0x0, 0x38(%rbx)
je 0x1430ee
movl $0x1, %ecx
xorl %edx, %edx
xorl %esi, %esi
movq (%rbx), %rdi
movq 0x48(%rbx), %r8
movl (%r8,%rsi,4), %esi
imulq $0x38, %rsi, %rsi
movl %edx, 0x30(%rdi,%rsi)
movq %rdx, 0x28(%rdi,%rsi)
movl %edx, 0x8(%rdi,%rsi)
movq %rdx, (%rdi,%rsi)
movq %rdx, 0x10(%rdi,%rsi)
movl %ecx, %esi
incl %ecx
cmpq %rsi, 0x38(%rbx)
ja 0x143785
jmp 0x1430ee