// File: /workspace/github_cpp_makefile_star2_O2/sjain12[P]CliqueCounting/tests/../Escape/CliquesBruteForce.h
// Function: cliques_brute_force(Escape::CGraph&, CDAG&, std::basic_ofstream<char, std::char_traits<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long long)
void cliques_brute_force(CGraph &CG, CDAG &DG, ofstream &of, string gname, string fname, unsigned long long clique_size)
{    
    high_resolution_clock::time_point t_elim_s = high_resolution_clock::now();
    
    Stack* stack = newStack();
    VertexSet *path = NULL, *nbrs = NULL;
    double num_cliques = 0;

    for (VertexIdx c=0; c<CG.nVertices; c++)
    {
        path = newVertexSet(1);
        path->vertices[0] = c;
        nbrs = newVertexSet(DG.outlist.offsets[c+1] - DG.outlist.offsets[c]);
        std::copy(DG.outlist.nbors+DG.outlist.offsets[c], DG.outlist.nbors+DG.outlist.offsets[c+1], nbrs->vertices);
        PartialClique* pc = newPartialClique(path, nbrs);
        stack->push(pc);

        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);

    high_resolution_clock::time_point t_elim_e = high_resolution_clock::now();
    auto duration_program = std::chrono::duration_cast<std::chrono::microseconds>( t_elim_e - t_elim_s ).count();
    
    cout << "time reqd: " << duration_program << endl;
	cout << "number of cliques = " << num_cliques << endl;
	cout << "clique size = " << clique_size << endl;

	of << clique_size << "," << num_cliques << "," << duration_program << "," << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r14
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movq %rdi, 0x30(%rsp)
callq 0x3050
movq %rax, 0x38(%rsp)
callq 0x3c47
movq %rax, 0x10(%rsp)
leaq -0x1(%r14), %rax
movq %rax, 0x50(%rsp)
leaq -0x2(%r14), %rax
movq %rax, 0x48(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq $0x0, 0x20(%rsp)
movq %r14, 0x8(%rsp)
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %r14
cmpq (%rax), %r14
jge 0x40b8
pushq $0x1
popq %rdi
callq 0x3a68
movq %rax, %rbx
movq 0x8(%rax), %rax
movq %r14, (%rax)
movq 0x10(%r12), %rax
movq 0x8(%rax,%r14,8), %rdi
subq (%rax,%r14,8), %rdi
callq 0x3a68
movq %r14, %rcx
movq %rax, %r14
movq 0x10(%r12), %rax
movq (%rax,%rcx,8), %rsi
movq 0x8(%rax,%rcx,8), %rdx
shlq $0x3, %rsi
shlq $0x3, %rdx
subq %rsi, %rdx
je 0x3e20
addq 0x18(%r12), %rsi
movq 0x8(%r14), %rdi
callq 0x3430
incq 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3be1
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3c66
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rdi
callq 0x3c92
testq %rax, %rax
je 0x3db9
movq %rax, 0x68(%rsp)
movq (%rax), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rbp
movq %rcx, 0x60(%rsp)
movq (%rcx), %rdi
movq (%rbp), %rbx
leaq (%rbx,%rdi), %rax
cmpq %r14, %rax
jb 0x4053
cmpq %r14, %rdi
jne 0x3e99
movsd (%rsp), %xmm0
addsd 0x5179(%rip), %xmm0     # 0x9008
movsd %xmm0, (%rsp)
jmp 0x404f
cmpq 0x50(%rsp), %rdi
jne 0x3eb8
cvtsi2sd %rbx, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
jmp 0x404f
cmpq 0x48(%rsp), %rdi
jne 0x3f29
cmpq $0x2, %rbx
pushq $0x1
popq %r14
movq %r14, %rcx
cmovgeq %rbx, %rcx
decq %rcx
movq %rcx, 0x18(%rsp)
xorl %r15d, %r15d
cmpq 0x18(%rsp), %r15
je 0x404a
movq %r14, %r13
cmpq %r14, %rbx
jle 0x3f1e
movq 0x8(%rbp), %rax
movq (%rax,%r15,8), %rsi
movq (%rax,%r14,8), %rdx
movq 0x30(%rsp), %rdi
callq 0x588e
movsd (%rsp), %xmm0
addsd 0x50f9(%rip), %xmm0     # 0x9008
cmpl $-0x1, %eax
je 0x3f19
movsd %xmm0, (%rsp)
incq %r14
jmp 0x3ee7
incq %r15
movq %r13, %r14
incq %r14
jmp 0x3ed9
incq %rdi
testq %rbx, %rbx
movl $0x0, %eax
cmovgq %rbx, %rax
movq %rax, 0x70(%rsp)
xorl %ecx, %ecx
movq %rdi, 0x58(%rsp)
cmpq 0x70(%rsp), %rcx
je 0x404a
movq 0x8(%rbp), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %r15
callq 0x3a68
movq %rax, 0x28(%rsp)
movq 0x10(%r12), %rax
movq 0x8(%rax,%r15,8), %rcx
subq (%rax,%r15,8), %rcx
cmpl %ecx, %ebx
cmovlq %rbx, %rcx
movslq %ecx, %rdi
callq 0x3a68
movq %rax, %r14
movq 0x10(%r12), %rax
movslq (%rax,%r15,8), %rcx
shlq $0x3, %rcx
xorl %edx, %edx
xorl %esi, %esi
movslq %esi, %rdi
cmpq %rdx, (%rbp)
jle 0x3fe9
movq 0x8(%rax,%r15,8), %r8
subq (%rax,%r15,8), %r8
testq %r8, %r8
movl $0x0, %r9d
cmovleq %r9, %r8
movq 0x18(%r12), %r9
addq %rcx, %r9
xorl %r10d, %r10d
cmpq %r10, %r8
je 0x3fe4
movq 0x8(%rbp), %r11
movq (%r11,%rdx,8), %r11
leaq 0x1(%r10), %r13
cmpq (%r9,%r10,8), %r11
movq %r13, %r10
jne 0x3fc0
movq 0x8(%r14), %r8
movq %r11, (%r8,%rdi,8)
incl %esi
incq %rdx
jmp 0x3f96
testl %esi, %esi
je 0x400c
movq %rdi, (%r14)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x3be1
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3c66
jmp 0x4038
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x401a
callq 0x3300
movq %r14, %rdi
pushq $0x10
popq %rsi
callq 0x3290
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x4038
callq 0x3300
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x58(%rsp), %rdi
jmp 0x3f44
movq 0x8(%rsp), %r14
movq (%rbp), %rbx
testq %rbx, %rbx
jle 0x4066
movq 0x8(%rbp), %rdi
testq %rdi, %rdi
je 0x4066
callq 0x3300
movq %rbp, %rdi
pushq $0x10
popq %rsi
callq 0x3290
movq 0x60(%rsp), %rbx
cmpq $0x0, (%rbx)
jle 0x408a
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x408a
callq 0x3300
movq %rbx, %rdi
pushq $0x10
popq %rbx
movq %rbx, %rsi
callq 0x3290
movq 0x68(%rsp), %r15
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x3290
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3290
jmp 0x3e42
movq 0x10(%rsp), %rdi
callq 0x3c58
callq 0x3050
subq 0x38(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rbx
movq 0x9ef0(%rip), %r14      # 0xdfd0
leaq 0x4fa1(%rip), %rsi      # 0x9088
movq %r14, %rdi
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %rdi
callq 0x3140
leaq 0x4f8b(%rip), %rsi      # 0x9094
movq %r14, %rdi
callq 0x3260
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3420
movq %rax, %rdi
callq 0x3140
leaq 0x4f7c(%rip), %rsi      # 0x90a9
movq %r14, %rdi
callq 0x3260
movq %rax, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x3190
movq %rax, %rdi
callq 0x3140
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x3190
leaq 0x4f70(%rip), %r14      # 0x90d1
movq %rax, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3420
movq %rax, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %rdi
movq %r14, %rsi
callq 0x3260
movq %rax, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3140