// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r8, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rdx
movq (%rax,%rcx,8), %r12
movq (%rdx,%rcx,8), %r13
movl $0x10, %edi
callq 0x32c0
xorpd %xmm0, %xmm0
movq %rax, 0x28(%rsp)
movupd %xmm0, (%rax)
movl $0x10, %edi
callq 0x32c0
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x3030
movq %rbx, 0x8(%rsp)
movq %rax, 0x8(%rbx)
movq 0x10(%r15), %rbx
movq 0x8(%rbx,%r12,8), %r14
subq (%rbx,%r12,8), %r14
movl $0x10, %edi
callq 0x32c0
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq %r14, (%rax)
movq %r14, %rcx
shrq $0x3d, %rcx
shlq $0x3, %r14
negq %rcx
movl $0x0, %edi
sbbq %rdi, %rdi
orq %r14, %rdi
movq %rax, %r14
callq 0x3030
movq %rax, 0x8(%r14)
movslq (%rbx,%r12,8), %rcx
cmpq %rcx, 0x8(%rbx,%r12,8)
jle 0x4198
movq 0x18(%r15), %rdx
movslq (%rbx,%r13,8), %rsi
cmpq %rsi, 0x8(%rbx,%r13,8)
jle 0x418b
movq (%rdx,%rcx,8), %rdi
cmpq (%rdx,%rsi,8), %rdi
jne 0x4186
movslq %ebp, %rbp
movq %rdi, (%rax,%rbp,8)
incl %ebp
incq %rsi
jmp 0x416c
incq %rcx
cmpq %rcx, 0x8(%rbx,%r12,8)
jg 0x4168
movslq %ebp, %rbp
movq %rbp, (%r14)
movl $0x10, %edi
callq 0x32c0
movq %rax, %r12
movq 0x8(%rsp), %rax
movq %rax, (%r12)
movq %r14, 0x8(%r12)
movl $0x10, %edi
callq 0x32c0
movq %r12, (%rax)
movq 0x28(%rsp), %rsi
movq (%rsi), %rdi
movq %rdi, 0x8(%rax)
movl 0x8(%rsi), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0x8(%rsi)
movq %rdi, (%rsi)
movq $0x0, 0x8(%rax)
movl %ecx, 0x8(%rsi)
movq 0x18(%rsp), %rdx
leaq -0x1(%rdx), %rcx
movq %rcx, 0x50(%rsp)
leaq -0x2(%rdx), %rcx
movq %rcx, 0x48(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, 0x38(%rsp)
movq (%rax), %rcx
movq (%rcx), %rdi
movq 0x8(%rcx), %rbx
movq (%rdi), %rsi
movq (%rbx), %r13
leaq (%rsi,%r13), %rcx
cmpq %rdx, %rcx
movq %rax, 0x70(%rsp)
movq %rdi, 0x68(%rsp)
jb 0x44b2
cmpq %rdx, %rsi
jne 0x4251
movsd 0x8(%rsp), %xmm0
addsd 0x5dc2(%rip), %xmm0     # 0xa008
movsd %xmm0, 0x8(%rsp)
jmp 0x44b2
cmpq 0x50(%rsp), %rsi
jne 0x4275
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x44b2
cmpq 0x48(%rsp), %rsi
jne 0x42ec
cmpq $0x2, %r13
jl 0x44b2
leaq -0x2(%r13), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x1(%rbp), %rax
movq %rax, 0x30(%rsp)
movq %r14, %r12
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movq (%rax,%r12,8), %rdx
movq %r15, %rdi
callq 0x6ece
movsd 0x8(%rsp), %xmm0
addsd 0x5d43(%rip), %xmm0     # 0xa008
cmpl $-0x1, %eax
je 0x42d0
movsd %xmm0, 0x8(%rsp)
incq %r12
cmpq %r12, %r13
jg 0x42a3
incq %r14
cmpq 0x10(%rsp), %rbp
movq 0x30(%rsp), %rbp
jne 0x4297
jmp 0x44b2
testq %r13, %r13
jle 0x44b2
leaq 0x1(%rsi), %rax
movabsq $0x1fffffffffffffff, %rcx # imm = 0x1FFFFFFFFFFFFFFF
movq %rax, 0x60(%rsp)
cmpq %rcx, %rax
leaq 0x8(,%rsi,8), %rcx
movq $-0x1, %rax
cmovaq %rax, %rcx
movq %rcx, 0x58(%rsp)
xorl %ecx, %ecx
movq 0x20(%rsp), %r12
movq 0x8(%rbx), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %rbp
movl $0x10, %edi
callq 0x32c0
movq %rax, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq %rax, (%r14)
movq 0x58(%rsp), %rdi
callq 0x3030
movq %r14, 0x10(%rsp)
movq %rax, 0x8(%r14)
movq 0x10(%r12), %r14
movq 0x8(%r14,%rbp,8), %rax
subq (%r14,%rbp,8), %rax
cmpl %eax, %r13d
cmovlq %r13, %rax
movslq %eax, %r12
movl $0x10, %edi
callq 0x32c0
movq %rax, %r15
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq %r12, (%rax)
leaq (,%r12,8), %rdi
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r12
movq $-0x1, %rax
cmovaq %rax, %rdi
callq 0x3030
movq %r15, 0x40(%rsp)
movq %rax, 0x8(%r15)
cmpq $0x0, (%rbx)
jle 0x4421
movslq (%r14,%rbp,8), %rdx
shlq $0x3, %rdx
movq 0x20(%rsp), %rdi
addq 0x18(%rdi), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
movq %rdi, %r12
movq 0x38(%rsp), %r15
movq 0x8(%r14,%rbp,8), %rdi
subq (%r14,%rbp,8), %rdi
jle 0x4417
movq 0x8(%rbx), %r8
movq (%r8,%rsi,8), %r8
xorl %r9d, %r9d
cmpq (%rdx,%r9,8), %r8
je 0x440e
incq %r9
cmpq %r9, %rdi
jne 0x43fe
jmp 0x4417
movslq %ecx, %rcx
movq %r8, (%rax,%rcx,8)
incl %ecx
incq %rsi
cmpq %rsi, (%rbx)
jg 0x43e8
jmp 0x442d
xorl %ecx, %ecx
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %r15
testl %ecx, %ecx
je 0x4477
movslq %ecx, %rax
movq 0x40(%rsp), %r14
movq %rax, (%r14)
movl $0x10, %edi
callq 0x32c0
movq %rax, %rbp
movq 0x10(%rsp), %rax
movq %rax, (%rbp)
movq %r14, 0x8(%rbp)
movl $0x10, %edi
callq 0x32c0
movq %rbp, (%rax)
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rdx)
incl 0x8(%rdx)
jmp 0x44a1
movq %rax, %rdi
callq 0x3360
movl $0x10, %esi
movq 0x40(%rsp), %rdi
callq 0x32d0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x44a1
callq 0x3360
movq 0x30(%rsp), %rcx
incq %rcx
cmpq %r13, %rcx
jne 0x432a
cmpq $0x0, (%rbx)
jle 0x44c6
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x44c6
callq 0x3360
movl $0x10, %esi
movq %rbx, %rdi
callq 0x32d0
movq 0x68(%rsp), %r14
cmpq $0x0, (%r14)
movq 0x70(%rsp), %rbx
jle 0x44f1
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x44f1
callq 0x3360
movl $0x10, %esi
movq %r14, %rdi
callq 0x32d0
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4510
movl $0x10, %esi
callq 0x32d0
movl $0x10, %esi
movq %rbx, %rdi
callq 0x32d0
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x453e
movq 0x8(%rax), %rcx
movq %rcx, (%rdi)
movq $0x0, 0x8(%rax)
decl 0x8(%rdi)
jmp 0x4540
xorl %eax, %eax
movq 0x18(%rsp), %rdx
testq %rax, %rax
jne 0x420c
movq $0x0, (%rdi)
movl $0x10, %esi
callq 0x32d0
movsd 0x8(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq