// File: /workspace/github_cpp_makefile_star2_O1/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, %ebx
movq %rdi, %r15
movl 0x14(%rdi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %r12
movq (%rax), %rax
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq %rcx, %rax
btsq $0x3b, %rax
movq %rax, (%r12)
movq $0x3, 0x10(%r12)
movl $0x10, %edi
callq 0x2280
movq %rax, 0x18(%r12)
movq %rbp, %rcx
xorl $0x1, %ebp
movl %ebx, %eax
shrl %eax
orb $0x1, (%r14,%rax)
shrl %ecx
orb $0x1, (%r14,%rcx)
movq %r12, %rdi
movl %ebx, %esi
callq 0xe8be3
movq %r12, 0x8(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0xe8be3
cmpq $0x0, 0x50(%rsp)
je 0xed9c6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rax
movl %eax, %ecx
movq 0x20(%r15), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0xed9b8
cmpb $0x1, 0x4(%rdx,%rcx,8)
je 0xed9b8
addl %eax, %eax
xorl %ebx, %ebx
movl %ebx, %r12d
orl %eax, %r12d
movq 0x28(%r15), %rcx
movq (%rcx,%r12,8), %rcx
movq %rcx, %rdx
andq %rbp, %rdx
je 0xed995
movq 0x8(%r15), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xed97a
testl %edi, %edi
jne 0xed9ac
shrl %eax
orb $0x1, (%r14,%rax)
movl %r12d, %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
callq 0xe8be3
leal 0x1(%rbx), %ecx
movl %r12d, %eax
testl %ebx, %ebx
movl %ecx, %ebx
je 0xed944
incl %r13d
cmpq 0x50(%rsp), %r13
jb 0xed924
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0xedadf
movq 0x18(%rdi), %rcx
leaq -0x1(%rax), %rdx
movq %rdx, 0x8(%rdi)
movq (%rdi), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rdx, %rax
decq %rax
andq 0x10(%rdi), %rax
movl (%rcx,%rax,4), %eax
movq 0x28(%r15), %rcx
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0xed9cb
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r13
shlq $0x3, %r13
addq 0x8(%r15), %r13
movl %eax, %ebp
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %rax
cmpl $0x0, 0xc(%r13,%rax,8)
js 0xedace
leaq (,%rax,8), %rax
addq %r13, %rax
movl 0x8(%rax), %esi
movq 0x20(%r15), %rax
movl %esi, %r12d
shrl %r12d
cmpl $0x0, (%rax,%r12,8)
js 0xedace
testb $0x1, (%r14,%r12)
jne 0xedace
movq %rsi, %rax
xorq $0x1, %rax
movq 0x28(%r15), %rcx
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0xedabf
movq 0x8(%r15), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %r8d
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %r8
leaq (%r8,%r8,2), %rcx
xorl %edx, %edx
xorl %r9d, %r9d
cmpl $0x0, (%rax,%rdx)
js 0xedab1
movl -0x4(%rax,%rdx), %r8d
shrl %r8d
movzbl (%r14,%r8), %r8d
notl %r8d
andl $0x1, %r8d
addl %r8d, %r9d
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xeda94
testl %r9d, %r9d
jne 0xedace
callq 0xe8be3
movq 0x8(%rsp), %rdi
orb $0x1, (%r14,%r12)
incq %rbx
cmpq %rbp, %rbx
jne 0xeda24
jmp 0xed9cb
cmpq $0x0, 0x50(%rsp)
je 0xedb1f
movq 0x20(%r15), %rax
movl $0x1, %ecx
xorl %edx, %edx
movq 0x10(%rsp), %rsi
movl (%rsi,%rdx,8), %edx
cmpl $0x0, (%rax,%rdx,8)
js 0xedb14
cmpb $0x1, 0x4(%rax,%rdx,8)
je 0xedb14
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
testb $0x1, (%r14,%rdx)
je 0xedb30
movl %ecx, %edx
incl %ecx
cmpq 0x50(%rsp), %rdx
jb 0xedaf7
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
jmp 0xedb21