// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
callq 0x4380
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x4000
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
subq (%rax,%rcx,8), %rdi
callq 0x4000
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x60(%rbp)
movslq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x38(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0x4716
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x40(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0x4706
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movslq -0x64(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x46f9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x5c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x46fb
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x4697
jmp 0x4708
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x4669
movslq -0x5c(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x42a0
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x4420
movq -0x48(%rbp), %rdi
callq 0x4480
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x4c68
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x80(%rbp), %rax
addq -0x88(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x47ad
jmp 0x4b71
movq -0x80(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x47ce
movsd 0xf849(%rip), %xmm0     # 0x14008
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x4b6f
movq -0x80(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x47f7
cvtsi2sdq -0x88(%rbp), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x4b6d
movq -0x80(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
jne 0x48c4
movl $0x0, -0x90(%rbp)
movslq -0x90(%rbp), %rax
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x48bf
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movslq -0x94(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x48a9
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x93b0
cmpl $-0x1, %eax
je 0x4896
movsd 0xf77c(%rip), %xmm0     # 0x14008
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x4898
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x4840
jmp 0x48ab
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x4816
jmp 0x4b6b
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x4b69
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0xa0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x4000
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xa8(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0xa8(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
movl %eax, -0xb4(%rbp)
movq -0x88(%rbp), %rax
movl %eax, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jge 0x4972
movl -0xb4(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
movl %eax, -0xb4(%rbp)
movslq -0xb4(%rbp), %rdi
callq 0x4000
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movq -0x58(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x4a9d
movl $0x0, -0xd0(%rbp)
movslq -0xd0(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0xa8(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
movq -0x18(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0xa8(%rbp), %rsi
subq (%rdx,%rsi,8), %rcx
cmpq %rcx, %rax
jge 0x4a87
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xcc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0xc8(%rbp), %edx
addl -0xd0(%rbp), %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x4a71
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xcc(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xc4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x4a87
jmp 0x4a73
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x49d0
jmp 0x4a89
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x49b2
cmpl $0x0, -0xc4(%rbp)
jne 0x4b15
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0x4aca
movq -0xe8(%rbp), %rdi
callq 0x33f0
movq -0xc0(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
je 0x4aef
movq -0xf0(%rbp), %rdi
movl $0x10, %esi
callq 0x3330
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, %rax
je 0x4b13
movq -0xf8(%rbp), %rdi
callq 0x33f0
jmp 0x4b52
movslq -0xc4(%rbp), %rcx
movq -0xc0(%rbp), %rax
movq %rcx, (%rax)
movq -0xb0(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x42a0
movq %rax, -0xd8(%rbp)
movq -0x48(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x4420
jmp 0x4b52
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x48cf
jmp 0x4b6b
jmp 0x4b6d
jmp 0x4b6f
jmp 0x4b71
movq -0x58(%rbp), %rax
cmpq $0x0, (%rax)
jle 0x4b9e
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x4b9c
movq -0x100(%rbp), %rdi
callq 0x33f0
jmp 0x4b9e
movq -0x58(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, %rax
je 0x4bc0
movq -0x108(%rbp), %rdi
movl $0x10, %esi
callq 0x3330
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
jle 0x4bed
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x110(%rbp)
cmpq $0x0, %rax
je 0x4beb
movq -0x110(%rbp), %rdi
callq 0x33f0
jmp 0x4bed
movq -0x50(%rbp), %rax
movq %rax, -0x118(%rbp)
cmpq $0x0, %rax
je 0x4c0f
movq -0x118(%rbp), %rdi
movl $0x10, %esi
callq 0x3330
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0x4c34
movq -0x120(%rbp), %rdi
movl $0x10, %esi
callq 0x3330
movq -0x78(%rbp), %rax
movq %rax, -0x128(%rbp)
cmpq $0x0, %rax
je 0x4c56
movq -0x128(%rbp), %rdi
movl $0x10, %esi
callq 0x3330
movq -0x48(%rbp), %rdi
callq 0x4480
movq %rax, -0x78(%rbp)
jmp 0x474c
movq -0x48(%rbp), %rdi
callq 0x43d0
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nop