// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x160, %rax            # imm = 0x160
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0xe0(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0xf0(%rax)
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x2713b5
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x27137a
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x48(%rbp)
jmp 0x271385
movl $0x2, %eax
movq %rax, -0x48(%rbp)
jmp 0x271385
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x2c(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rsi
movq 0x38(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x38(%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x2713d8
movq -0x20(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x27169f
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x271426
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq $0x0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x2c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x2715a9
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x271496
jmp 0x27159b
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x2714ce
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movl -0x18(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x2714d3
jmp 0x27159b
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x38(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
cmpq $-0x1, (%rax,%rcx,8)
je 0x2714f8
jmp 0x27159b
jmp 0x2714fa
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x271563
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x271528
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x50(%rbp)
jmp 0x271533
movl $0x2, %eax
movq %rax, -0x50(%rbp)
jmp 0x271533
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rsi
movq 0x38(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x38(%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x27146c
cmpl $0x0, -0x34(%rbp)
je 0x2715b4
jmp 0x2713d8
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpq $0x1, (%rax,%rcx,8)
je 0x27169a
jmp 0x2715dc
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xf8(%rcx), %rax
jne 0x27165d
movq -0x10(%rbp), %rax
cmpq $0x0, 0xf8(%rax)
je 0x271616
movq -0x10(%rbp), %rax
movq 0xf8(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x271621
movl $0x2, %eax
movq %rax, -0x58(%rbp)
jmp 0x271621
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xf8(%rax)
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xf8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x100(%rax)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0xf0(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0xf0(%rsi)
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq $0x1, (%rax,%rcx,8)
jmp 0x2713d8
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0xf0(%rcx), %rax
jae 0x2716e2
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x100(%rcx), %rcx
movl -0x38(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movq $-0x1, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2716a6
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0xf0(%rax)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0xf0(%rcx), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x271791
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xf0(%rcx), %rcx
movl -0x38(%rbp), %edx
subq %rdx, %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x100(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xf0(%rcx), %rcx
movl -0x38(%rbp), %esi
subq %rsi, %rcx
subq $0x1, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2716ff
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)