// File: /workspace/github_cpp_makefile_star2_O3/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, 0x80(%rsp)
movq %rcx, %r8
movq %rdi, 0x48(%rsp)
movq 0x18(%rdi), %r13
movq 0x20(%r13), %rax
movl %esi, %ecx
shrl %ecx
movq %rcx, 0x90(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0x1496d2
movl %esi, %ebx
movq 0x28(%r13), %rcx
movl %esi, %eax
movq (%rcx,%rax,8), %rcx
xorl %r15d, %r15d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
je 0x1496d5
movq 0x8(%r13), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl (%rcx,%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %r15d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x1496b8
cmpl $0x2, %r15d
jge 0x1496ea
xorl %r15d, %r15d
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
movq %rcx, 0x50(%r8)
movq %rcx, 0x38(%r8)
movq %rcx, 0x20(%r8)
movq %rcx, 0x8(%r8)
movq (%r8), %rdx
imulq $0x38, %rax, %rax
movq %rcx, 0x28(%rdx,%rax)
movq %rcx, 0x4(%rdx,%rax)
movq %rcx, 0x10(%rdx,%rax)
cmpq %rcx, 0x10(%r8)
movq %r8, 0x8(%rsp)
movq %r9, 0x20(%rsp)
je 0x14972a
movq 0x18(%r8), %rax
xorl %ecx, %ecx
jmp 0x149752
movq $0x2, 0x10(%r8)
movq 0x18(%r8), %rdi
movl $0x8, %esi
callq 0x2370
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movl %ebx, (%rax,%rcx,4)
incq %rcx
xorl %r15d, %r15d
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x88(%rsp)
movl $0x0, 0x4(%rsp)
movl %ebx, (%rsp)
movq %r13, 0x40(%rsp)
movq (%r8), %rax
movq 0x18(%r8), %rdx
leaq -0x1(%rcx), %rsi
movq %rsi, 0x8(%r8)
movl -0x4(%rdx,%rcx,4), %edx
movq 0x28(%r13), %rcx
movq (%rcx,%rdx,8), %r12
testl %r12d, %r12d
je 0x149c42
movq %rdx, 0x38(%rsp)
imulq $0x38, %rdx, %rcx
movl 0x4(%rax,%rcx), %edx
movq %rdx, 0x50(%rsp)
movl 0x8(%rax,%rcx), %edx
movq %rdx, 0x70(%rsp)
movl 0x28(%rax,%rcx), %edx
movl %edx, 0x34(%rsp)
movl 0x2c(%rax,%rcx), %edx
movl %edx, 0x30(%rsp)
movsd 0x10(%rax,%rcx), %xmm0
movsd %xmm0, 0x68(%rsp)
movq %r12, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbx
shlq $0x3, %rbx
addq 0x8(%r13), %rbx
xorl %ebp, %ebp
movl $0x0, 0x14(%rsp)
movq %rbx, 0x60(%rsp)
movq %r12, 0x78(%rsp)
movl %ebp, %eax
leaq (%rax,%rax,2), %rax
movl 0x8(%rbx,%rax,8), %edi
movl %edi, %r14d
shrl %r14d
cmpl 0x90(%rsp), %r14d
je 0x149ca3
leaq (%rbx,%rax,8), %rax
xorl %edx, %edx
testb $0x1, 0x88(%rsp)
je 0x149831
movl (%rax), %edx
cmpl $0x0, 0xc(%rax)
js 0x149c0a
imulq $0x38, %rdi, %r12
addq (%r8), %r12
movq 0x50(%r8), %rcx
movq 0x60(%r8), %rax
cmpq 0x58(%r8), %rcx
movq %rdi, 0x18(%rsp)
movq %rdx, 0x58(%rsp)
jne 0x14988c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x58(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %r8
movq %rax, 0x60(%r8)
movq 0x50(%r8), %rcx
movq 0x38(%rsp), %rsi
movq 0x28(%r13), %rdx
movl 0x4(%rdx,%rsi,8), %edx
addl %ebp, %edx
leaq 0x1(%rcx), %r9
movq %r9, 0x50(%r8)
movl %edx, (%rax,%rcx,4)
movl 0x30(%r12), %r13d
testl %r13d, %r13d
js 0x1499a0
movq 0x38(%r8), %rcx
movq 0x48(%r8), %rax
cmpq 0x40(%r8), %rcx
jne 0x1498f9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x38(%r8), %rcx
movq 0x40(%rsp), %r9
movq 0x58(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r8)
movl %edi, (%rax,%rcx,4)
movl %esi, (%r12)
orb $-0x80, 0x33(%r12)
addl 0x50(%rsp), %r10d
movl %r10d, 0x4(%r12)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rbx
movl 0x10(%rax,%rbx,8), %eax
addl 0x70(%rsp), %eax
movl %eax, 0x8(%r12)
movq %rdi, %rax
xorq $0x1, %rax
movq 0x28(%r9), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0x149a8b
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 %r13d, %r13d
movl (%rax,%rdx), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r13d
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x14997d
orl $0x80000000, %r13d      # imm = 0x80000000
jmp 0x149a91
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rcx
movl 0x10(%rax,%rcx,8), %ebx
movq 0x70(%rsp), %rax
addl %ebx, %eax
movl %eax, 0x2c(%rsp)
movl (%rsp), %edx
movq %r8, %rcx
movq 0x80(%rsp), %r8
callq 0x14959f
movq 0x20(%rsp), %r9
cmpb $0x0, (%r9,%r14)
movl $0x0, %eax
cmovel %ebx, %eax
cmovel %r15d, %ebx
addl 0x30(%rsp), %eax
cvtsi2sd %rax, %xmm1
addl 0x34(%rsp), %ebx
leal (%rax,%rbx), %ecx
cvtsi2sd %rcx, %xmm2
divsd %xmm2, %xmm1
movl 0x2c(%r12), %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
movl 0x28(%r12), %edx
addl %ecx, %edx
cvtsi2sd %rdx, %xmm3
addsd 0x68(%rsp), %xmm0
divsd %xmm3, %xmm2
ucomisd %xmm2, %xmm1
movq 0x58(%rsp), %rdx
ja 0x149a4c
jne 0x149a6f
jp 0x149a6f
ucomisd 0x10(%r12), %xmm0
ja 0x149a4c
jne 0x149a6f
jp 0x149a6f
movl 0x2c(%rsp), %ecx
cmpl 0x8(%r12), %ecx
jbe 0x149c17
movq 0x38(%rsp), %rcx
movl %ecx, (%r12)
movl 0x2c(%rsp), %ecx
movl %ecx, 0x8(%r12)
movsd %xmm0, 0x10(%r12)
movl %eax, 0x2c(%r12)
movl %ebx, 0x28(%r12)
addl 0x50(%rsp), %edx
cmpl 0x4(%r12), %edx
movq 0x60(%rsp), %rbx
jae 0x149a84
movl %edx, 0x4(%r12)
movq 0x8(%rsp), %r8
jmp 0x149b0a
movl $0x80000000, %r13d      # imm = 0x80000000
movl %r13d, 0x30(%r12)
movl (%rsp), %edx
movq %r8, %rcx
movq 0x80(%rsp), %r8
callq 0x14959f
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%r12)
xorl %eax, %eax
movq 0x20(%rsp), %r9
cmpb $0x0, (%r9,%r14)
movl $0x0, %ecx
jne 0x149ad6
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rcx,%rbx,8), %ecx
addl 0x30(%rsp), %ecx
movl %ecx, 0x2c(%r12)
cmpb $0x0, (%r9,%r14)
je 0x149af3
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%rbx,8), %eax
addl 0x34(%rsp), %eax
movl %eax, 0x28(%r12)
incl 0x4(%rsp)
movq 0x8(%rsp), %r8
movq 0x60(%rsp), %rbx
movq 0x18(%rsp), %r10
leal -0x1(%r13), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl $0x80000000, %r13d      # imm = 0x80000000
orl %eax, %r13d
movl %r13d, 0x30(%r12)
testl %eax, %eax
je 0x149b3a
movq 0x40(%rsp), %r13
movq 0x78(%rsp), %r12
jmp 0x149c0a
movq 0x40(%rsp), %r13
movq 0x28(%r13), %rax
movq (%rax,%r10,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
movq 0x78(%rsp), %r12
je 0x149bee
movq 0x8(%r13), %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 %esi, %esi
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x149b7d
testl %esi, %esi
je 0x149bee
movq 0x8(%r8), %rcx
movq 0x18(%r8), %rax
cmpq 0x10(%r8), %rcx
jne 0x149bd9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r8)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x18(%rsp), %r10
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %r10d, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
movl %eax, %r10d
jmp 0x149c01
cmpl $0x0, 0x14(%rsp)
movl $0x1, 0x14(%rsp)
jne 0x149ca3
decl 0x4(%rsp)
movl %r10d, 0x10(%rsp)
incl %ebp
cmpl %r12d, %ebp
jne 0x149801
jmp 0x149c34
jne 0x149a6f
movq 0x50(%rsp), %rcx
addl %edx, %ecx
cmpl 0x4(%r12), %ecx
ja 0x149a4c
jmp 0x149a6f
cmpl $0x1, 0x14(%rsp)
je 0x149cea
movl (%rsp), %ebx
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x149ca3
movb $0x1, %al
movq %rax, 0x88(%rsp)
cmpq $0x1, %rcx
jne 0x14977f
cmpl $0x0, 0x4(%rsp)
jne 0x14977f
movq 0x18(%r8), %rax
movb 0xd0(%rsp), %sil
movl (%rax), %eax
movq (%r8), %rcx
movl %eax, %edx
imulq $0x38, %rdx, %rdx
movl (%rcx,%rdx), %ecx
shrl %eax
orb %sil, (%r9,%rax)
movl %ecx, %eax
cmpl %ebx, %ecx
jne 0x149c78
movq 0x18(%r8), %rax
movl (%rax), %eax
shrl %eax
movb $0x0, (%r9,%rax)
movl $0x1, %r15d
cmpq $0x0, 0x38(%r8)
je 0x1496d5
movl $0x1, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq (%r8), %rsi
movq 0x48(%r8), %rdi
movl (%rdi,%rdx,4), %edx
imulq $0x38, %rdx, %rdx
movl %ecx, 0x30(%rsi,%rdx)
movq %rcx, 0x28(%rsi,%rdx)
movl %ecx, 0x8(%rsi,%rdx)
movq %rcx, (%rsi,%rdx)
movq %rcx, 0x10(%rsi,%rdx)
movl %eax, %edx
incl %eax
cmpq %rdx, 0x38(%r8)
ja 0x149cb7
jmp 0x1496d5
xorl %r15d, %r15d
movl 0x10(%rsp), %ebp
cmpl $-0x1, %ebp
movl (%rsp), %ebx
je 0x149ca3
cmpl $0x0, 0x4(%rsp)
jne 0x149ca3
cmpq $0x0, 0x8(%r8)
je 0x149d0c
xorl %r15d, %r15d
jmp 0x149ca3
cmpq $0x0, 0x10(%r8)
movq 0x18(%r8), %rax
je 0x149d1b
xorl %ecx, %ecx
jmp 0x149d42
movq $0x2, 0x10(%r8)
movl $0x8, %esi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x18(%r8)
movq 0x8(%r8), %rcx
movb 0xd0(%rsp), %sil
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
movl %ebp, (%rax,%rcx,4)
jmp 0x149c76