// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: detec_circles(asg_t*, unsigned int, unsigned int, unsigned char*, ma_ug_t*, unsigned long*, unsigned long)
int detec_circles(asg_t *nsg, uint32_t vSrc, uint32_t vDest,  
uint8_t* visit, ma_ug_t *ug, uint64_t* nodes, uint64_t nodes_n)
{
    uint32_t nv, nw, knw, i, k, v, w;
    asg_arc_t *av, *aw;

    memset(visit, 0, nsg->n_seq);
    kdq_t(uint32_t) *buf;
    buf = kdq_init(uint32_t);

    vDest = vDest^1;

    Set_vis(visit, vSrc, 1);
    Set_vis(visit, vDest, 1);
    kdq_push(uint32_t, buf, vSrc);
    kdq_push(uint32_t, buf, vDest);

    for (i = 0; i < nodes_n; i++)
    {
        v = (uint32_t)nodes[i];
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;
        v = v<<1;

        for (k = 0; k < 2; k++)
        {
            v = v|k;
            if(get_real_length(nsg, v, NULL)==0)
            {
                Set_vis(visit, v, 1);
                kdq_push(uint32_t, buf, v^1);
            }
        }
    }



    while (kdq_size(buf) != 0)
    {
        v = *(kdq_pop(uint32_t, buf));
        ///if(Get_vis(visit, v, 1) == 0) fprintf(stderr, "ERROR\n");

        nv = asg_arc_n(nsg, v);
        av = asg_arc_a(nsg, v);
        for(k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            w = av[k].v^1;
            if(nsg->seq[w>>1].del) continue;
            if(Get_vis(visit, w, 1) != 0) continue;

            ///check indegree
            nw = asg_arc_n(nsg, w);
            aw = asg_arc_a(nsg, w);
            for (i = 0, knw = 0; i < nw; i++)
            {
                if(aw[i].del) continue;
                if(Get_vis(visit, aw[i].v, 1) != 0) continue;
                knw++;
            }

            if(knw == 0)
            {
                kdq_push(uint32_t, buf, w^1);
                Set_vis(visit, (w^1), 1);
            }
        }
    }

    for (i = 0; i < nodes_n; i++)
    {
        v = (uint32_t)nodes[i];
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;
        v = v<<1;
        if(Get_vis(visit, v, 1) == 0) return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, 0x10(%rsp)
movq %rcx, %r14
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r15
movl 0x14(%rdi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
callq 0xabb23
movq %rax, %rbx
movq %rbp, %rcx
xorl $0x1, %ebp
movl %r12d, %eax
shrl %eax
orb $0x1, (%r14,%rax)
shrl %ecx
orb $0x1, (%r14,%rcx)
movq %rbx, %rdi
movl %r12d, %esi
callq 0xabb74
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0xabb74
movl %r13d, %r13d
cmpq 0x50(%rsp), %r13
jae 0xaff64
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rbx
movl %ebx, %eax
movq 0x20(%r15), %rcx
cmpl $0x0, (%rcx,%rax,8)
js 0xafe5a
cmpb $0x1, 0x4(%rcx,%rax,8)
jne 0xafe5f
incl %r13d
jmp 0xafe30
addl %ebx, %ebx
xorl %r12d, %r12d
cmpl $0x2, %r12d
je 0xafe5a
movl %r12d, %ebp
orl %ebx, %ebp
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jne 0xafe95
shrl %ebx
orb $0x1, (%r14,%rbx)
movl %ebp, %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
callq 0xabb74
incl %r12d
movl %ebp, %ebx
jmp 0xafe64
callq 0xabcc1
movl (%rax), %eax
movq 0x28(%r15), %rcx
movq (%rcx,%rax,8), %rax
movl %eax, %ebp
shrq $0x20, %rax
imulq $0x18, %rax, %r13
addq 0x8(%r15), %r13
xorl %ebx, %ebx
cmpq %rbp, %rbx
je 0xaff64
imulq $0x18, %rbx, %rax
cmpl $0x0, 0xc(%r13,%rax)
js 0xaff5c
addq %r13, %rax
movl 0x8(%rax), %esi
movq 0x20(%r15), %rax
movl %esi, %r12d
shrl %r12d
cmpl $0x0, (%rax,%r12,8)
js 0xaff5c
testb $0x1, (%r14,%r12)
jne 0xaff5c
movq %rsi, %rax
xorq $0x1, %rax
movq 0x8(%r15), %rcx
movq 0x28(%r15), %rdx
movq (%rdx,%rax,8), %rax
movl %eax, %edx
movq %rax, %rdi
shrq $0x20, %rdi
imulq $0x18, %rdx, %rax
imulq $0x18, %rdi, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
xorl %edi, %edi
cmpq %rdx, %rax
je 0xaff49
cmpl $0x0, (%rcx,%rdx)
js 0xaff43
movl -0x4(%rcx,%rdx), %r8d
shrl %r8d
movzbl (%r14,%r8), %r8d
notl %r8d
andl $0x1, %r8d
addl %r8d, %edi
addq $0x18, %rdx
jmp 0xaff21
testl %edi, %edi
jne 0xaff5c
movq 0x8(%rsp), %rdi
callq 0xabb74
orb $0x1, (%r14,%r12)
incq %rbx
jmp 0xafebb
movq 0x8(%rsp), %rdi
cmpq $0x0, 0x8(%rdi)
jne 0xafe9c
xorl %eax, %eax
pushq $0x1
popq %rcx
xorl %edx, %edx
movq 0x50(%rsp), %rsi
cmpl %esi, %edx
jae 0xaffb1
movq 0x10(%rsp), %rsi
movl (%rsi,%rdx,8), %esi
movq 0x20(%r15), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0xaffaa
cmpb $0x1, 0x4(%rdi,%rsi,8)
je 0xaffaa
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
testb $0x1, (%r14,%rsi)
je 0xaffaf
incq %rdx
jmp 0xaff7b
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq