// File: /workspace/github_cpp_makefile_star2_O3/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), %r8
xorl %eax, %eax
movq %rax, 0x198(%rsi)
movq %rax, 0xf0(%rsi)
cmpq %rax, 0x1a0(%rsi)
movq %r8, 0x8(%rsp)
je 0x198613
movq 0x1a8(%rbx), %rax
movq $0x1, 0x198(%rbx)
movl %r14d, (%rax)
movl $0x1, %esi
jmp 0x19865d
movq $0x2, 0x1a0(%rbx)
movq 0x1a8(%rbx), %rdi
movl $0x8, %esi
movl %ecx, %ebp
callq 0x2370
movl %ebp, %ecx
movq 0x8(%rsp), %r8
movq %rax, 0x1a8(%rbx)
movq 0x198(%rbx), %rdx
movq %rdx, %rsi
incq %rsi
movq %rsi, 0x198(%rbx)
movl %r14d, (%rax,%rdx,4)
je 0x198816
shrl %r14d
shrl %ecx
movl %ecx, 0x4(%rsp)
movq %r15, 0x10(%rsp)
movq 0x1a8(%rbx), %rax
movl -0x4(%rax,%rsi,4), %r12d
movl %r12d, %r13d
shrl %r13d
cmpq $-0x1, (%r8,%r13,8)
jne 0x19868c
movq $0x0, (%r8,%r13,8)
movq 0x28(%r15), %rdx
movq (%rdx,%r12,8), %rdi
testl %edi, %edi
je 0x198786
movq %r13, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%r15), %rdx
movq %rdi, %rsi
shrq $0x20, %rsi
movl %edi, %edi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r15
addq $0x8, %r15
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %r13
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpl $0x0, 0x4(%r15,%r12)
js 0x198759
movl (%r15,%r12), %r9d
movl %r9d, %edx
shrl %edx
cmpl %r14d, %edx
sete %sil
cmpl %ecx, %edx
sete %dil
orb %sil, %dil
jne 0x198759
cmpq $-0x1, (%r8,%rdx,8)
jne 0x198759
movq 0x198(%rbx), %rdx
cmpq 0x1a0(%rbx), %rdx
jne 0x198748
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x1a0(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r8
movq %rax, 0x1a8(%rbx)
movl (%r15,%r12), %r9d
movq 0x198(%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x198(%rbx)
movl %r9d, (%rax,%rdx,4)
incl %ebp
addq $0x18, %r12
cmpq %r12, %r13
jne 0x1986cc
movq 0x198(%rbx), %rsi
testl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jne 0x19880d
jmp 0x19878d
movq 0x198(%rbx), %rsi
decq %rsi
movq %rsi, 0x198(%rbx)
cmpq $0x1, (%r8,%r13,8)
je 0x19880d
movq 0xf0(%rbx), %rdi
movq 0x100(%rbx), %rax
cmpq 0xf8(%rbx), %rdi
jne 0x1987ef
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0xf8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %r8
movq %rax, 0x100(%rbx)
movq 0xf0(%rbx), %rdi
leaq 0x1(%rdi), %rdx
movq %rdx, 0xf0(%rbx)
movl %r12d, (%rax,%rdi,4)
movq $0x1, (%r8,%r13,8)
movq 0x198(%rbx), %rsi
testq %rsi, %rsi
jne 0x19866b
cmpq $0x0, 0xf0(%rbx)
je 0x198863
movq 0x100(%rbx), %rcx
movl $0x1, %edx
xorl %esi, %esi
movl (%rcx,%rsi,4), %eax
shrl %eax
movq $-0x1, (%r8,%rax,8)
movl %edx, %esi
movq 0xf0(%rbx), %rax
incl %edx
cmpq %rsi, %rax
ja 0x19882e
decq %rax
movq %rax, 0xf0(%rbx)
cmpq $0x2, %rax
jb 0x1988a6
movq %rax, %rcx
shrq %rcx
jmp 0x19887f
movq $-0x1, 0xf0(%rbx)
movq $-0x1, %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
movq 0x100(%rbx), %rdx
decq %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 0x19888b
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq