// File: /workspace/github_cpp_makefile_star2_O2/sjain12[P]CliqueCounting/tests/../Escape/CliquesGraft.h
// Function: get_edge_cliques(Escape::Graph&, Escape::CGraph&, CDAG&, long, unsigned long long)
double get_edge_cliques(Graph &g, CGraph &CG, CDAG &DG, EdgeIdx edge, unsigned long long clique_size)
{
    double num_cliques = 0;
    VertexIdx v1 = g.srcs[edge];
    VertexIdx v2 = g.dsts[edge];
    
    Stack* stack = newStack();
    VertexSet *path = NULL, *nbrs = NULL;

    path = newVertexSet(0);
    nbrs = newVertexSet(CG.offsets[v1+1] - CG.offsets[v1]);

    int c = 0;
    for (int i=CG.offsets[v1]; i<CG.offsets[v1+1]; i++)
    {
        for (int j=CG.offsets[v2]; j<CG.offsets[v2+1]; j++)
        {
            if (CG.nbors[i] == CG.nbors[j])
            {
                nbrs->vertices[c] = CG.nbors[i];
                c++;
            }
        }
    }
    nbrs->nVertices = c;
    PartialClique *pc1 = newPartialClique(path, nbrs);
    stack->push(pc1);
    StackItem* si = stack->pop();

    while (si != NULL)
    {
        path = si->pc->path;
        nbrs = si->pc->nbrs;  // get the current path and its nbrs
        VertexIdx psize = path->nVertices, nsize = nbrs->nVertices;

        int flag = 0;
        if (psize + nsize < clique_size)
        {
        }
        else if (psize == clique_size)
        {   
            num_cliques++;
        }
        else if (psize == clique_size - 1)
        {
            num_cliques += nsize;
        }
        else if (psize == clique_size - 2)
        {
            for (int i=0; i<nsize-1; i++)
                for (int j=i+1; j<nsize; j++)
                    if (CG.isEdge(nbrs->vertices[i], nbrs->vertices[j]) != -1)
                        num_cliques++;
        }
        else
        {
            for (VertexIdx i=0; i<nsize; i++)
            {
                VertexIdx v = nbrs->vertices[i];
                VertexSet *new_path = newVertexSet(psize+1);

                int s = DG.outlist.offsets[v+1]-DG.outlist.offsets[v], l = nsize;
                if (l < s)
                {
                    l = s;
                    s = nsize;
                }

                VertexSet *common = newVertexSet(s);
                int k = 0, offset = DG.outlist.offsets[v];
                for (int l =0; l < nbrs->nVertices; l++) {
                    for (int j=0; j<DG.outlist.offsets[v+1]-DG.outlist.offsets[v]; j++) {
                        if (nbrs->vertices[l] == DG.outlist.nbors[offset + j])
                        {
                            common->vertices[k] = nbrs->vertices[l];
                            k++;
                            break;
                        }
                    }
                }
                if (k == 0) 
                {
                    delete[] common->vertices;
                    delete common;
                    delete[] new_path->vertices;
                    continue;
                }
                else
                {
                    common->nVertices = k; 
                    PartialClique *pc = newPartialClique(new_path, common);
                    stack->push(pc);
                }
            }
        }
        
        if (nbrs->nVertices > 0) delete[] nbrs->vertices;
        delete nbrs;
        if (path->nVertices > 0) delete[] path->vertices;
        delete path;
        delete si->pc;
        delete si;
        si = stack->pop();
    }

    delStack(stack);
    double ret = num_cliques; 
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rdx
movq (%rax,%rcx,8), %r12
movq (%rdx,%rcx,8), %r13
callq 0x3c67
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x3a88
movq %rax, %rbx
movq 0x10(%r15), %rax
movq 0x8(%rax,%r12,8), %rdi
subq (%rax,%r12,8), %rdi
callq 0x3a88
movq 0x10(%r15), %rcx
movslq (%rcx,%r12,8), %rdx
cmpq %rdx, 0x8(%rcx,%r12,8)
jle 0x3e0e
movslq (%rcx,%r13,8), %rsi
movq 0x18(%r15), %rdi
cmpq %rsi, 0x8(%rcx,%r13,8)
jle 0x3e09
movq (%rdi,%rdx,8), %r8
cmpq (%rdi,%rsi,8), %r8
jne 0x3e04
movq 0x8(%rax), %r9
movslq %ebp, %rbp
movq %r8, (%r9,%rbp,8)
incl %ebp
incq %rsi
jmp 0x3de6
incq %rdx
jmp 0x3dd7
movq %r15, 0x30(%rsp)
movslq %ebp, %rcx
movq %rcx, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3c01
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3c86
movq %rbx, %rdi
callq 0x3cb2
movq %rax, %rcx
movq 0x10(%rsp), %r15
leaq -0x1(%r15), %rax
movq %rax, 0x40(%rsp)
leaq -0x2(%r15), %rax
movq %rax, 0x38(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
testq %rcx, %rcx
je 0x40f1
movq %rcx, 0x58(%rsp)
movq (%rcx), %rax
movq (%rax), %rcx
movq 0x8(%rax), %r12
movq %rcx, 0x50(%rsp)
movq (%rcx), %rdi
movq (%r12), %rbp
leaq (%rdi,%rbp), %rax
cmpq %r15, %rax
jb 0x407d
cmpq %r15, %rdi
jne 0x3eaf
movsd 0x8(%rsp), %xmm0
addsd 0x5164(%rip), %xmm0     # 0x9008
movsd %xmm0, 0x8(%rsp)
jmp 0x4079
cmpq 0x40(%rsp), %rdi
jne 0x3ed0
cvtsi2sd %rbp, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x4079
cmpq 0x38(%rsp), %rdi
jne 0x3f4a
cmpq $0x2, %rbp
pushq $0x1
popq %rbx
movq %rbx, %rcx
cmovgeq %rbp, %rcx
decq %rcx
movq %rcx, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x30(%rsp), %r15
cmpq 0x20(%rsp), %r13
je 0x4074
movq %rbx, 0x28(%rsp)
cmpq %rbx, %rbp
jle 0x3f3d
movq 0x8(%r12), %rax
movq (%rax,%r13,8), %rsi
movq (%rax,%rbx,8), %rdx
movq %r15, %rdi
callq 0x5eb2
movsd 0x8(%rsp), %xmm0
addsd 0x50db(%rip), %xmm0     # 0x9008
cmpl $-0x1, %eax
je 0x3f38
movsd %xmm0, 0x8(%rsp)
incq %rbx
jmp 0x3f05
incq %r13
movq 0x28(%rsp), %rbx
incq %rbx
jmp 0x3ef5
incq %rdi
testq %rbp, %rbp
movl $0x0, %eax
cmovgq %rbp, %rax
movq %rax, 0x60(%rsp)
xorl %ecx, %ecx
movq %rdi, 0x48(%rsp)
cmpq 0x60(%rsp), %rcx
je 0x4079
movq 0x8(%r12), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r13
callq 0x3a88
movq %rax, 0x20(%rsp)
movq 0x10(%r14), %rax
movq 0x8(%rax,%r13,8), %rcx
subq (%rax,%r13,8), %rcx
cmpl %ecx, %ebp
cmovlq %rbp, %rcx
movslq %ecx, %rdi
callq 0x3a88
movq %rax, %rbx
movq 0x10(%r14), %rax
movslq (%rax,%r13,8), %rcx
shlq $0x3, %rcx
xorl %edx, %edx
xorl %esi, %esi
movslq %esi, %rdi
cmpq %rdx, (%r12)
jle 0x4009
movq 0x8(%rax,%r13,8), %r8
subq (%rax,%r13,8), %r8
testq %r8, %r8
movl $0x0, %r9d
cmovleq %r9, %r8
movq 0x18(%r14), %r9
addq %rcx, %r9
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x4004
movq 0x8(%r12), %r11
movq (%r11,%rdx,8), %r11
leaq 0x1(%r10), %r15
cmpq (%r9,%r10,8), %r11
movq %r15, %r10
jne 0x3fdf
movq 0x8(%rbx), %r8
movq %r11, (%r8,%rdi,8)
incl %esi
incq %rdx
jmp 0x3fb6
testl %esi, %esi
je 0x4031
movq %rdi, (%rbx)
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c01
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x3c86
movq 0x10(%rsp), %r15
jmp 0x4062
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %r15
je 0x4044
callq 0x3320
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x32b0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x4062
callq 0x3320
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %rdi
jmp 0x3f65
movq 0x10(%rsp), %r15
movq (%r12), %rbp
testq %rbp, %rbp
jle 0x4091
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x4091
callq 0x3320
movq %r12, %rdi
pushq $0x10
popq %rsi
callq 0x32b0
movq 0x50(%rsp), %rbx
cmpq $0x0, (%rbx)
jle 0x40b5
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x40b5
callq 0x3320
movq %rbx, %rdi
pushq $0x10
popq %rbx
movq %rbx, %rsi
callq 0x32b0
movq 0x58(%rsp), %r12
movq (%r12), %rdi
movq %rbx, %rsi
callq 0x32b0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32b0
movq 0x18(%rsp), %rdi
callq 0x3cb2
movq %rax, %rcx
jmp 0x3e60
movq 0x18(%rsp), %rdi
callq 0x3c78
movsd 0x8(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq