// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdx, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
callq 0x3060
cmpq $0x0, (%r13)
movq %rbx, 0x30(%rsp)
movq %r14, 0x48(%rsp)
movq %rax, 0x40(%rsp)
jle 0x4466
leaq -0x1(%rbx), %rax
movq %rax, 0x60(%rsp)
leaq -0x2(%rbx), %rax
movq %rax, 0x58(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebx, %ebx
movl $0x10, %edi
callq 0x32a0
movq %rax, %r14
movq $0x1, (%rax)
movl $0x8, %edi
callq 0x3030
movq %r14, 0x20(%rsp)
movq %rax, 0x8(%r14)
movq %rbx, (%rax)
movq 0x10(%rsp), %rax
movq %rbx, %r15
movq 0x10(%rax), %rbx
movq 0x8(%rbx,%r15,8), %rbp
subq (%rbx,%r15,8), %rbp
movl $0x10, %edi
callq 0x32a0
movq %rax, %r14
movq %rbp, (%rax)
leaq (,%rbp,8), %rdi
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %rbp
movq $-0x1, %rax
cmovaq %rax, %rdi
callq 0x3030
movq %rax, 0x8(%r14)
movq (%rbx,%r15,8), %rsi
movq 0x8(%rbx,%r15,8), %rdx
shlq $0x3, %rsi
shlq $0x3, %rdx
subq %rsi, %rdx
je 0x4105
movq 0x10(%rsp), %rcx
addq 0x18(%rcx), %rsi
movq %rax, %rdi
callq 0x3430
incq %r15
movq %r15, 0x50(%rsp)
movl $0x10, %edi
callq 0x32a0
movq %rax, %rbx
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
movq %r14, 0x8(%rbx)
movl $0x10, %edi
callq 0x32a0
movq %rbx, (%rax)
movq %rax, 0x38(%rsp)
movq $0x0, 0x8(%rax)
movl $0x1, %eax
xorl %esi, %esi
leaq (%rax,%rbp), %rcx
movq 0x30(%rsp), %rdx
cmpq %rdx, %rcx
jb 0x43bd
cmpq %rdx, %rax
jne 0x4175
movsd (%rsp), %xmm0
addsd 0x5e9d(%rip), %xmm0     # 0xa008
movsd %xmm0, (%rsp)
jmp 0x43ba
cmpq 0x60(%rsp), %rax
jne 0x4197
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
jmp 0x43ba
cmpq 0x58(%rsp), %rax
jne 0x4217
cmpq $0x2, %rbp
jl 0x43ba
movq %rsi, 0x8(%rsp)
leaq -0x2(%rbp), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %ebx
xorl %r12d, %r12d
leaq 0x1(%r12), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %r15
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rsi
movq (%rax,%r15,8), %rdx
movq %r13, %rdi
callq 0x648e
movsd (%rsp), %xmm0
addsd 0x5e1c(%rip), %xmm0     # 0xa008
cmpl $-0x1, %eax
je 0x41f6
movsd %xmm0, (%rsp)
incq %r15
cmpq %r15, %rbp
jg 0x41cb
incq %rbx
cmpq 0x18(%rsp), %r12
movq 0x28(%rsp), %r12
jne 0x41be
movq 0x8(%rsp), %rsi
jmp 0x43ba
testq %rbp, %rbp
jle 0x43ba
leaq 0x1(%rax), %rdx
movabsq $0x1fffffffffffffff, %rcx # imm = 0x1FFFFFFFFFFFFFFF
movq %rdx, 0x70(%rsp)
cmpq %rcx, %rdx
leaq 0x8(,%rax,8), %rcx
movq $-0x1, %rax
cmovaq %rax, %rcx
movq %rcx, 0x68(%rsp)
xorl %ecx, %ecx
movq %rsi, 0x8(%rsp)
movq 0x8(%r14), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r15
movl $0x10, %edi
callq 0x32a0
movq %rax, %rbx
movq 0x70(%rsp), %rax
movq %rax, (%rbx)
movq 0x68(%rsp), %rdi
callq 0x3030
movq %rbx, 0x18(%rsp)
movq %rax, 0x8(%rbx)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rbx
movq 0x8(%rbx,%r15,8), %rax
subq (%rbx,%r15,8), %rax
cmpl %eax, %ebp
cmovlq %rbp, %rax
movslq %eax, %r12
movl $0x10, %edi
callq 0x32a0
movq %rax, %rcx
movq %r12, (%rax)
leaq (,%r12,8), %rdi
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
cmpq %rax, %r12
movq %rcx, %r12
movq $-0x1, %rax
cmovaq %rax, %rdi
callq 0x3030
movq %rax, 0x8(%r12)
movq (%r14), %rdx
testq %rdx, %rdx
jle 0x437c
movslq (%rbx,%r15,8), %rcx
shlq $0x3, %rcx
movq 0x10(%rsp), %rsi
addq 0x18(%rsi), %rcx
xorl %edi, %edi
xorl %esi, %esi
movq 0x8(%rbx,%r15,8), %r8
subq (%rbx,%r15,8), %r8
jle 0x4335
movq 0x8(%r14), %r9
movq (%r9,%rdi,8), %r9
xorl %r10d, %r10d
cmpq (%rcx,%r10,8), %r9
je 0x4329
incq %r10
cmpq %r10, %r8
jne 0x4319
jmp 0x4335
movslq %esi, %rsi
movq %r9, (%rax,%rsi,8)
incl %esi
movq (%r14), %rdx
incq %rdi
cmpq %rdi, %rdx
jg 0x4303
testl %esi, %esi
je 0x437c
movslq %esi, %rax
movq %rax, (%r12)
movl $0x10, %edi
callq 0x32a0
movq %rax, %r15
movq 0x18(%rsp), %rax
movq %rax, (%r15)
movq %r12, 0x8(%r15)
movl $0x10, %edi
callq 0x32a0
movq %r15, (%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq %rax, %rsi
jmp 0x43a9
movq %rax, %rdi
callq 0x3340
movl $0x10, %esi
movq %r12, %rdi
callq 0x32b0
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x43a4
callq 0x3340
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rcx
incq %rcx
cmpq %rbp, %rcx
jne 0x4250
movq (%r14), %rbp
movq %rsi, %rbx
testq %rbp, %rbp
jle 0x43d3
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x43d3
callq 0x3340
movl $0x10, %esi
movq %r14, %rdi
callq 0x32b0
movq 0x20(%rsp), %r14
cmpq $0x0, (%r14)
jle 0x43f9
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x43f9
callq 0x3340
movl $0x10, %esi
movq %r14, %rdi
callq 0x32b0
movq 0x38(%rsp), %r14
movq (%r14), %rdi
movl $0x10, %esi
callq 0x32b0
movl $0x10, %esi
movq %r14, %rdi
callq 0x32b0
testq %rbx, %rbx
je 0x4455
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
movq $0x0, 0x8(%rbx)
movq (%rax), %rcx
movq 0x8(%rax), %r14
movq %rcx, 0x20(%rsp)
movq (%rcx), %rax
movq (%r14), %rbp
movq %rbx, 0x38(%rsp)
jmp 0x4147
movq 0x50(%rsp), %rbx
cmpq (%r13), %rbx
jl 0x4066
jmp 0x446f
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
callq 0x3060
subq 0x40(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rbx
shrq $0x3f, %rbx
sarq $0x7, %rdx
addq %rdx, %rbx
movq 0x9b35(%rip), %r14      # 0xdfd0
leaq 0x5be6(%rip), %rsi      # 0xa088
movl $0xb, %edx
movq %r14, %rdi
callq 0x32f0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3260
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
leaq 0x5ba9(%rip), %rsi      # 0xa094
movl $0x14, %edx
movq %r14, %rdi
callq 0x32f0
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x3420
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
leaq 0x5b73(%rip), %rsi      # 0xa0a9
movl $0xe, %edx
movq %r14, %rdi
callq 0x32f0
movq %r14, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x31a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x31a0
movq %rax, %r15
leaq 0x5b3d(%rip), %r14      # 0xa0d1
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32f0
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x3420
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3260
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31b0