// File: /workspace/github_cpp_makefile_star2_O2/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 %ecx, %ebp
movq %rsi, %rbx
movq 0xe0(%rsi), %r8
andq $0x0, 0x198(%rsi)
movl %edx, %r14d
andq $0x0, 0xf0(%rsi)
movq %rdi, 0x20(%rsp)
xorl %eax, %eax
cmpq %rax, 0x1a0(%rsi)
movq %r8, 0x8(%rsp)
je 0x151312
movq 0x1a8(%rbx), %rax
xorl %edx, %edx
jmp 0x15133f
movq $0x2, 0x1a0(%rbx)
movq 0x1a8(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x1a8(%rbx)
movq 0x198(%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x198(%rbx)
movl %r14d, (%rax,%rdx,4)
shrl %r14d
shrl %ebp
movl %r14d, 0x14(%rsp)
testq %rcx, %rcx
je 0x151506
movq 0x1a8(%rbx), %rax
movl -0x4(%rax,%rcx,4), %esi
movl %esi, %ecx
shrl %ecx
movq %rcx, (%rsp)
cmpq $-0x1, (%r8,%rcx,8)
jne 0x151384
movq (%rsp), %rcx
andq $0x0, (%r8,%rcx,8)
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq %rsi, 0x18(%rsp)
movq (%rdx,%rsi,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x18, %rsi, %r15
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r13
addq $0x8, %r13
xorl %r12d, %r12d
xorl %r9d, %r9d
cmpq %r12, %r15
je 0x151467
cmpl $0x0, 0x4(%r13,%r12)
js 0x15145e
movl (%r13,%r12), %ecx
movl %ecx, %edx
shrl %edx
cmpl %r14d, %edx
sete %sil
cmpl %ebp, %edx
sete %dil
orb %sil, %dil
jne 0x15145e
cmpq $-0x1, (%r8,%rdx,8)
jne 0x15145e
movq 0x198(%rbx), %rdx
cmpq 0x1a0(%rbx), %rdx
jne 0x15144d
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x1a0(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
movl %ebp, %r14d
movq %rbx, %rbp
movl %r9d, %ebx
callq 0x2370
movl %ebx, %r9d
movq %rbp, %rbx
movl %r14d, %ebp
movl 0x14(%rsp), %r14d
movq 0x8(%rsp), %r8
movq %rax, 0x1a8(%rbx)
movl (%r13,%r12), %ecx
movq 0x198(%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x198(%rbx)
movl %ecx, (%rax,%rdx,4)
incl %r9d
addq $0x18, %r12
jmp 0x1513b6
movq 0x198(%rbx), %rcx
testl %r9d, %r9d
jne 0x151358
decq %rcx
movq %rcx, 0x198(%rbx)
movq (%rsp), %rax
cmpq $0x1, (%r8,%rax,8)
je 0x151358
movq 0xf0(%rbx), %rcx
movq 0x100(%rbx), %rax
cmpq 0xf8(%rbx), %rcx
jne 0x1514db
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0xf8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x100(%rbx)
movq 0xf0(%rbx), %rcx
movq 0x18(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0xf0(%rbx)
movl %esi, (%rax,%rcx,4)
movq (%rsp), %rax
movq $0x1, (%r8,%rax,8)
movq 0x198(%rbx), %rcx
jmp 0x151358
xorl %eax, %eax
movl %eax, %ecx
movq 0xf0(%rbx), %rax
cmpq %rcx, %rax
jbe 0x15152c
movq 0x100(%rbx), %rax
movl (%rax,%rcx,4), %eax
shrl %eax
orq $-0x1, (%r8,%rax,8)
leal 0x1(%rcx), %eax
jmp 0x151508
decq %rax
movq %rax, 0xf0(%rbx)
movq %rax, %rcx
shrq %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x151569
movq 0x100(%rbx), %rsi
movl (%rsi,%rdx,4), %edi
movq %rdx, %r8
notq %r8
leaq (%rsi,%rax,4), %r9
movl (%r9,%r8,4), %r10d
movl %r10d, (%rsi,%rdx,4)
movl %edi, (%r9,%r8,4)
incl %edx
jmp 0x15153e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq