// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: topologicalSortUtil(asg_t*, hap_cov_t*, unsigned int, unsigned int)
void topologicalSortUtil(asg_t *g, hap_cov_t *cov, uint32_t beg, uint32_t sink)
{
    buf_t *b = &(cov->t_ch->b_buf_0);
    uint64_t *visited = cov->pos_idx;
    uint32_t v = beg, nv, kv, i;
    asg_arc_t *av = NULL;

    b->b.n = 0; cov->t_ch->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, cov->t_ch->topo_res, v);
            visited[v>>1] = 1;
        }
    }
    for (i = 0; i < cov->t_ch->topo_res.n; ++i) 
    {
        visited[cov->t_ch->topo_res.a[i]>>1] = (uint64_t)-1;
    }

    cov->t_ch->topo_res.n--;//remove beg
    for (i = 0; i < (cov->t_ch->topo_res.n>>1); ++i) 
    {
        v = cov->t_ch->topo_res.a[i];
        cov->t_ch->topo_res.a[i] = cov->t_ch->topo_res.a[cov->t_ch->topo_res.n - i - 1];
        cov->t_ch->topo_res.a[cov->t_ch->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
movq 0xa8(%rax), %rax
addq $0x68, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%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 0xa8(%rax), %rax
movq $0x0, 0x50(%rax)
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x1347cb
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x134790
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x48(%rbp)
jmp 0x13479b
movl $0x2, %eax
movq %rax, -0x48(%rbp)
jmp 0x13479b
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 0x1347ee
movq -0x20(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x134ade
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 0x13483c
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 0x1349bf
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 0x1348ac
jmp 0x1349b1
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 0x1348e4
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 0x1348e9
jmp 0x1349b1
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 0x13490e
jmp 0x1349b1
jmp 0x134910
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x134979
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x13493e
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
shlq %rax
movq %rax, -0x50(%rbp)
jmp 0x134949
movl $0x2, %eax
movq %rax, -0x50(%rbp)
jmp 0x134949
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 0x134882
cmpl $0x0, -0x34(%rbp)
je 0x1349ca
jmp 0x1347ee
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 0x134ad9
jmp 0x1349f2
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
cmpq 0x58(%rcx), %rax
jne 0x134a97
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x134a40
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x58(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x134a4b
movl $0x2, %eax
movq %rax, -0x58(%rbp)
jmp 0x134a4b
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x58(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rcx, 0x60(%rax)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rsi
movq 0x50(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x50(%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 0x1347ee
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
cmpq 0x50(%rcx), %rax
jae 0x134b29
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq 0x60(%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 0x134ae5
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x50(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x50(%rax)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq 0x50(%rcx), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x134bf9
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq 0x50(%rcx), %rcx
movl -0x38(%rbp), %edx
subq %rdx, %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rax
movl -0x38(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq 0x50(%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 0x134b47
addq $0x60, %rsp
popq %rbp
retq
nop