// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/tovlp.cpp
// Function: tpSort(asg_t*, utg_trans_t*, unsigned int, unsigned int)
void tpSort(asg_t *g, utg_trans_t *o, uint32_t beg, uint32_t sink)
{
    buf_t *b = &(o->b0);
    uint64_t *visited = o->pos_idx;
    uint32_t v = beg, nv, kv, i;
    asg_arc_t *av = NULL;

    b->b.n = 0; o->topo_res.n = 0;
    kv_push(uint32_t, b->b, v);
    while (b->b.n > 0)
    {
        ///b->b.n--;
        v = b->b.a[b->b.n-1];
        if(visited[v>>1] == (uint64_t)-1)
        {
            visited[v>>1] = 0;
        } 
        
        nv = asg_arc_n(g, v);
        av = asg_arc_a(g, v);
        for (i = kv = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            if((av[i].v>>1) == (beg>>1) || (av[i].v>>1) == (sink>>1)) continue;
            if(visited[av[i].v>>1] != (uint64_t)-1) continue;
            kv_push(uint32_t, b->b, av[i].v);
            kv++;
        }

        if(kv != 0) continue;
        b->b.n--;
        if(visited[v>>1] != 1)
        {
            kv_push(uint32_t, o->topo_res, v);
            visited[v>>1] = 1;
        }
    }
    for (i = 0; i < o->topo_res.n; ++i) 
    {
        visited[o->topo_res.a[i]>>1] = (uint64_t)-1;
    }

    o->topo_res.n--;//remove beg
    for (i = 0; i < (o->topo_res.n>>1); ++i) 
    {
        v = o->topo_res.a[i];
        o->topo_res.a[i] = o->topo_res.a[o->topo_res.n - i - 1];
        o->topo_res.a[o->topo_res.n - i - 1] = v;
    }

    /*******************************for debug************************************/
    // for (i = 0; i < cov->n; ++i) 
    // {
    //     if(visited[i] != (uint64_t)-1) fprintf(stderr, "ERROR-2\n");
    // }
    // debug_topo_sorting(g, cov, beg, sink);
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
movq 0xe0(%rsi), %rdi
xorl %eax, %eax
movq %rax, 0x198(%rsi)
movq %rax, 0xf0(%rsi)
cmpq %rax, 0x1a0(%rsi)
movq %rdi, 0x8(%rsp)
jne 0x18f83b
movq $0x2, 0x1a0(%rbx)
movq 0x1a8(%rbx), %rdi
movl $0x8, %esi
movl %ecx, %ebp
callq 0x2370
movl %ebp, %ecx
movq 0x8(%rsp), %rdi
movq %rax, 0x1a8(%rbx)
movq 0x198(%rbx), %rsi
movq 0x1a8(%rbx), %rdx
movq %rsi, %rax
incq %rax
movq %rax, 0x198(%rbx)
movl %r14d, (%rdx,%rsi,4)
je 0x18fa0d
shrl %r14d
shrl %ecx
movl %ecx, 0x4(%rsp)
movq %r15, 0x10(%rsp)
movq 0x1a8(%rbx), %rdx
movl -0x4(%rdx,%rax,4), %r12d
movl %r12d, %r13d
shrl %r13d
cmpq $-0x1, (%rdi,%r13,8)
jne 0x18f88f
movq $0x0, (%rdi,%r13,8)
movq 0x28(%r15), %rax
movq (%rax,%r12,8), %rax
testl %eax, %eax
je 0x18f983
movq %r13, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%r15), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %r15
addq $0x8, %r15
shlq $0x3, %rax
leaq (%rax,%rax,2), %r13
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpl $0x0, 0x4(%r15,%r12)
js 0x18f963
movl (%r15,%r12), %eax
shrl %eax
cmpl %r14d, %eax
sete %sil
cmpl %ecx, %eax
sete %dl
orb %sil, %dl
jne 0x18f963
cmpq $-0x1, (%rdi,%rax,8)
jne 0x18f963
movq 0x1a0(%rbx), %rax
cmpq %rax, 0x198(%rbx)
jne 0x18f940
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x1a0(%rbx)
movq 0x1a8(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %rdi
movq %rax, 0x1a8(%rbx)
movl (%r15,%r12), %eax
movq 0x198(%rbx), %r8
movq 0x1a8(%rbx), %rdx
leaq 0x1(%r8), %rsi
movq %rsi, 0x198(%rbx)
movl %eax, (%rdx,%r8,4)
incl %ebp
addq $0x18, %r12
cmpq %r12, %r13
jne 0x18f8cf
testl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jne 0x18f9fd
decq 0x198(%rbx)
cmpq $0x1, (%rdi,%r13,8)
je 0x18f9fd
movq 0xf8(%rbx), %rax
cmpq %rax, 0xf0(%rbx)
jne 0x18f9d8
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xf8(%rbx)
movq 0x100(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %rdi
movq %rax, 0x100(%rbx)
movq 0xf0(%rbx), %rax
movq 0x100(%rbx), %rsi
leaq 0x1(%rax), %rdx
movq %rdx, 0xf0(%rbx)
movl %r12d, (%rsi,%rax,4)
movq $0x1, (%rdi,%r13,8)
movq 0x198(%rbx), %rax
testq %rax, %rax
jne 0x18f86e
movq 0xf0(%rbx), %rax
testq %rax, %rax
je 0x18fa44
movq 0x100(%rbx), %rcx
movl $0x1, %edx
xorl %esi, %esi
movl (%rcx,%rsi,4), %eax
shrl %eax
movq $-0x1, (%rdi,%rax,8)
movl %edx, %esi
movq 0xf0(%rbx), %rax
incl %edx
cmpq %rsi, %rax
ja 0x18fa27
leaq -0x1(%rax), %rcx
movq %rcx, 0xf0(%rbx)
cmpq $0x2, %rcx
jb 0x18fa80
shrq %rcx
movq 0x100(%rbx), %rdx
addq $-0x2, %rax
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movl (%rdx,%rax,4), %r8d
movl %r8d, (%rdx,%rsi,4)
movl %edi, (%rdx,%rax,4)
incq %rsi
movl %esi, %edi
decq %rax
cmpq %rdi, %rcx
ja 0x18fa65
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq