// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r15
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rdx
movq (%rax,%rcx,8), %rbx
movq (%rdx,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movl $0x10, %edi
callq 0x32c0
xorpd %xmm0, %xmm0
movq %rax, 0x18(%rsp)
movupd %xmm0, (%rax)
movl $0x10, %edi
callq 0x32c0
movq %rax, %r14
movq $0x0, (%rax)
xorl %edi, %edi
callq 0x3030
movq %r14, 0x30(%rsp)
movq %rax, 0x8(%r14)
movq 0x10(%r15), %r12
movq (%r12,%rbx,8), %r13
movq 0x8(%r12,%rbx,8), %r14
movq %r14, %rbx
subq %r13, %rbx
movl $0x10, %edi
callq 0x32c0
movq %rax, %rbp
movq %rbx, (%rax)
movq %rbx, %rax
shrq $0x3d, %rax
shlq $0x3, %rbx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rbx, %rdi
callq 0x3030
movq %rax, 0x8(%rbp)
movslq %r13d, %rcx
movl $0x0, %r13d
cmpq %rcx, %r14
jle 0x412e
movq 0x18(%r15), %rdx
movq 0x8(%rsp), %rdi
movslq (%r12,%rdi,8), %rsi
movq 0x8(%r12,%rdi,8), %r8
xorl %edi, %edi
movq %rsi, %r9
cmpq %rsi, %r8
jle 0x4123
movq (%rdx,%rcx,8), %r10
cmpq (%rdx,%r9,8), %r10
jne 0x411b
movslq %edi, %rdi
movq %r10, (%rax,%rdi,8)
incl %edi
incq %r9
cmpq %r9, %r8
jg 0x4108
incq %rcx
cmpq %rcx, %r14
jg 0x4100
movslq %edi, %r13
movq %r13, (%rbp)
movl $0x10, %edi
callq 0x32c0
movq %rax, %r14
movq 0x30(%rsp), %rax
movq %rax, (%r14)
movq %rbp, 0x8(%r14)
movl $0x10, %edi
callq 0x32c0
movq %rax, %rbx
movq %r14, (%rax)
movq 0x18(%rsp), %rax
xorl %ecx, %ecx
movq %rcx, (%rax)
movq $0x0, 0x28(%rsp)
movq %rcx, 0x8(%rbx)
movl $0x0, 0x8(%rax)
movq 0x40(%rsp), %rdx
leaq -0x1(%rdx), %rax
movq %rax, 0x58(%rsp)
leaq -0x2(%rdx), %rax
movq %rax, 0x50(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %esi, %esi
xorl %eax, %eax
leaq (%rax,%r13), %rcx
cmpq %rdx, %rcx
movq %rbx, 0x70(%rsp)
jb 0x4433
cmpq %rdx, %rax
jne 0x41ce
movsd 0x8(%rsp), %xmm0
addsd 0x5e45(%rip), %xmm0     # 0xa008
movsd %xmm0, 0x8(%rsp)
jmp 0x442f
cmpq 0x58(%rsp), %rax
jne 0x41f2
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x442f
cmpq 0x50(%rsp), %rax
jne 0x4274
cmpq $0x2, %r13
jl 0x442f
movq %rsi, 0x10(%rsp)
leaq -0x2(%r13), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %ebx
xorl %r12d, %r12d
leaq 0x1(%r12), %rax
movq %rax, 0x38(%rsp)
movq %rbx, %r14
movq 0x8(%rbp), %rax
movq (%rax,%r12,8), %rsi
movq (%rax,%r14,8), %rdx
movq %r15, %rdi
callq 0x6e6a
movsd 0x8(%rsp), %xmm0
addsd 0x5dc0(%rip), %xmm0     # 0xa008
cmpl $-0x1, %eax
je 0x4253
movsd %xmm0, 0x8(%rsp)
incq %r14
cmpq %r14, %r13
jg 0x4226
incq %rbx
cmpq 0x20(%rsp), %r12
movq 0x38(%rsp), %r12
jne 0x4219
movq 0x10(%rsp), %rsi
jmp 0x442f
testq %r13, %r13
jle 0x442f
leaq 0x1(%rax), %rdx
movabsq $0x1fffffffffffffff, %rcx # imm = 0x1FFFFFFFFFFFFFFF
movq %rdx, 0x68(%rsp)
cmpq %rcx, %rdx
leaq 0x8(,%rax,8), %rcx
movq $-0x1, %rax
cmovaq %rax, %rcx
movq %rcx, 0x60(%rsp)
xorl %ecx, %ecx
movq %rsi, 0x10(%rsp)
movq 0x8(%rbp), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rbx
movl $0x10, %edi
callq 0x32c0
movq %rax, %r14
movq 0x68(%rsp), %rax
movq %rax, (%r14)
movq 0x60(%rsp), %rdi
callq 0x3030
movq %r14, 0x20(%rsp)
movq %rax, 0x8(%r14)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %r12
movq 0x8(%r12,%rbx,8), %rax
subq (%r12,%rbx,8), %rax
cmpl %eax, %r13d
cmovlq %r13, %rax
movslq %eax, %r14
movl $0x10, %edi
callq 0x32c0
movq %rax, %rcx
movq %r14, (%rax)
leaq (,%r14,8), %rdi
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r14
movq %rcx, %r14
movq $-0x1, %rax
cmovaq %rax, %rdi
callq 0x3030
movq %rax, 0x8(%r14)
movq (%rbp), %rdx
testq %rdx, %rdx
jle 0x43f1
movslq (%r12,%rbx,8), %rcx
shlq $0x3, %rcx
movq 0x48(%rsp), %rsi
addq 0x18(%rsi), %rcx
xorl %edi, %edi
xorl %esi, %esi
movq 0x8(%r12,%rbx,8), %r8
subq (%r12,%rbx,8), %r8
jle 0x4394
movq 0x8(%rbp), %r9
movq (%r9,%rdi,8), %r9
xorl %r10d, %r10d
cmpq (%rcx,%r10,8), %r9
je 0x4387
incq %r10
cmpq %r10, %r8
jne 0x4377
jmp 0x4394
movslq %esi, %rsi
movq %r9, (%rax,%rsi,8)
incl %esi
movq (%rbp), %rdx
incq %rdi
cmpq %rdi, %rdx
jg 0x4361
testl %esi, %esi
je 0x43f1
movslq %esi, %rax
movq %rax, (%r14)
movl $0x10, %edi
callq 0x32c0
movq %rax, %rbx
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
movq %r14, 0x8(%rbx)
movl $0x10, %edi
callq 0x32c0
movq %rbx, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0x28(%rsp), %rdx
incl %edx
movq %rdx, 0x28(%rsp)
movl %edx, 0x8(%rcx)
movq %rax, %rsi
jmp 0x441e
movq %rax, %rdi
callq 0x3360
movl $0x10, %esi
movq %r14, %rdi
callq 0x32d0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x4419
callq 0x3360
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rcx
incq %rcx
cmpq %r13, %rcx
jne 0x42ad
movq (%rbp), %r13
movq %rsi, %rbx
testq %r13, %r13
jle 0x4449
movq 0x8(%rbp), %rdi
testq %rdi, %rdi
je 0x4449
callq 0x3360
movl $0x10, %esi
movq %rbp, %rdi
callq 0x32d0
movq 0x30(%rsp), %r14
cmpq $0x0, (%r14)
jle 0x446f
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x446f
callq 0x3360
movl $0x10, %esi
movq %r14, %rdi
callq 0x32d0
movq 0x70(%rsp), %r14
movq (%r14), %rdi
movl $0x10, %esi
callq 0x32d0
movl $0x10, %esi
movq %r14, %rdi
callq 0x32d0
testq %rbx, %rbx
je 0x44e1
movq 0x8(%rbx), %rsi
movq 0x18(%rsp), %rax
movq %rsi, (%rax)
movq $0x0, 0x8(%rbx)
movl 0x8(%rax), %ecx
decl %ecx
movq %rcx, 0x28(%rsp)
movl %ecx, 0x8(%rax)
movq (%rbx), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rbp
movq %rcx, 0x30(%rsp)
movq (%rcx), %rax
movq (%rbp), %r13
movq 0x40(%rsp), %rdx
jmp 0x419e
movl $0x10, %esi
movq 0x18(%rsp), %rdi
callq 0x32d0
movsd 0x8(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq