// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
callq 0x3060
movq %rax, 0x28(%rsp)
movl $0x10, %edi
callq 0x32a0
movq %rax, %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
cmpq $0x0, (%rbp)
movq %rbx, 0x30(%rsp)
jle 0x4534
leaq -0x1(%r13), %rax
movq %rax, 0x48(%rsp)
leaq -0x2(%r13), %rax
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r15d, %r15d
movq %r13, 0x50(%rsp)
movq %rdi, 0x20(%rsp)
movq %r14, 0x10(%rsp)
movl $0x10, %edi
callq 0x32a0
movq %rax, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq $0x1, (%rax)
movl $0x8, %edi
callq 0x3030
movq %rbx, 0x8(%rsp)
movq %rax, 0x8(%rbx)
movq %r15, (%rax)
movq 0x10(%r14), %rbx
movq 0x8(%rbx,%r15,8), %r12
subq (%rbx,%r15,8), %r12
movl $0x10, %edi
callq 0x32a0
movq %rax, %r14
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 %rax, 0x8(%r14)
movq (%rbx,%r15,8), %rsi
movq 0x8(%rbx,%r15,8), %rdx
shlq $0x3, %rsi
shlq $0x3, %rdx
subq %rsi, %rdx
je 0x41a6
movq 0x10(%rsp), %rcx
addq 0x18(%rcx), %rsi
movq %rax, %rdi
callq 0x3430
incq %r15
movq %r15, 0x38(%rsp)
movl $0x10, %edi
callq 0x32a0
movq %rax, %r15
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movq %r14, 0x8(%r15)
movl $0x10, %edi
callq 0x32a0
movq %r15, (%rax)
movq 0x20(%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 (%rax), %rcx
movq (%rcx), %rsi
movq 0x8(%rcx), %rbx
movq (%rsi), %rdx
movq (%rbx), %r14
leaq (%r14,%rdx), %rcx
cmpq %r13, %rcx
movq %rax, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
jb 0x4482
cmpq %r13, %rdx
jne 0x423a
movsd (%rsp), %xmm0
addsd 0x5dd8(%rip), %xmm0     # 0xa008
movsd %xmm0, (%rsp)
jmp 0x4482
cmpq 0x48(%rsp), %rdx
jne 0x425c
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
jmp 0x4482
cmpq 0x40(%rsp), %rdx
jne 0x42d2
cmpq $0x2, %r14
jl 0x4482
leaq -0x2(%r14), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x1(%r13), %rax
movq %rax, 0x8(%rsp)
movq %r15, %r12
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq (%rax,%r12,8), %rdx
movq %rbp, %rdi
callq 0x6586
movsd (%rsp), %xmm0
addsd 0x5d5c(%rip), %xmm0     # 0xa008
cmpl $-0x1, %eax
je 0x42b6
movsd %xmm0, (%rsp)
incq %r12
cmpq %r12, %r14
jg 0x428b
incq %r15
cmpq 0x18(%rsp), %r13
movq 0x8(%rsp), %r13
jne 0x427f
jmp 0x4482
testq %r14, %r14
jle 0x4482
leaq 0x1(%rdx), %rax
movabsq $0x1fffffffffffffff, %rcx # imm = 0x1FFFFFFFFFFFFFFF
movq %rax, 0x60(%rsp)
cmpq %rcx, %rax
leaq 0x8(,%rdx,8), %rcx
movq $-0x1, %rax
cmovaq %rax, %rcx
movq %rcx, 0x58(%rsp)
xorl %ecx, %ecx
movq 0x8(%rbx), %rax
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %r15
movl $0x10, %edi
callq 0x32a0
movq %rax, %r12
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq %rax, (%r12)
movq 0x58(%rsp), %rdi
callq 0x3030
movq %r12, 0x18(%rsp)
movq %rax, 0x8(%r12)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r13
movq 0x8(%r13,%r15,8), %rax
subq (%r13,%r15,8), %rax
cmpl %eax, %r14d
cmovlq %r14, %rax
movslq %eax, %r12
movl $0x10, %edi
callq 0x32a0
movq %rax, %rcx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
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)
cmpq $0x0, (%rbx)
jle 0x4402
movslq (%r13,%r15,8), %rdx
shlq $0x3, %rdx
movq 0x10(%rsp), %rcx
addq 0x18(%rcx), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x8(%r13,%r15,8), %rdi
subq (%r13,%r15,8), %rdi
jle 0x43f8
movq 0x8(%rbx), %r8
movq (%r8,%rsi,8), %r8
xorl %r9d, %r9d
cmpq (%rdx,%r9,8), %r8
je 0x43ef
incq %r9
cmpq %r9, %rdi
jne 0x43df
jmp 0x43f8
movslq %ecx, %rcx
movq %r8, (%rax,%rcx,8)
incl %ecx
incq %rsi
cmpq %rsi, (%rbx)
jg 0x43c8
jmp 0x4404
xorl %ecx, %ecx
testl %ecx, %ecx
je 0x4449
movslq %ecx, %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 0x20(%rsp), %rdx
movq (%rdx), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rdx)
incl 0x8(%rdx)
jmp 0x4471
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 0x4471
callq 0x3340
movq 0x8(%rsp), %rcx
incq %rcx
cmpq %r14, %rcx
jne 0x430b
cmpq $0x0, (%rbx)
movq 0x70(%rsp), %r14
jle 0x449b
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x449b
callq 0x3340
movl $0x10, %esi
movq %rbx, %rdi
callq 0x32b0
movq 0x68(%rsp), %rbx
cmpq $0x0, (%rbx)
jle 0x44c1
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x44c1
callq 0x3340
movl $0x10, %esi
movq %rbx, %rdi
callq 0x32b0
movq (%r14), %rdi
testq %rdi, %rdi
movq 0x50(%rsp), %r13
je 0x44e5
movl $0x10, %esi
callq 0x32b0
movl $0x10, %esi
movq %r14, %rdi
callq 0x32b0
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x4513
movq 0x8(%rax), %rcx
movq %rcx, (%rdi)
movq $0x0, 0x8(%rax)
decl 0x8(%rdi)
jmp 0x4515
xorl %eax, %eax
testq %rax, %rax
jne 0x41f7
movq 0x38(%rsp), %r15
cmpq (%rbp), %r15
movq 0x10(%rsp), %r14
jl 0x40ff
jmp 0x453d
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq $0x0, (%rdi)
movl $0x10, %esi
callq 0x32b0
callq 0x3060
subq 0x28(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rbx
shrq $0x3f, %rbx
sarq $0x7, %rdx
addq %rdx, %rbx
movq 0x9a56(%rip), %r14      # 0xdfd0
leaq 0x5b17(%rip), %rsi      # 0xa098
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 0x5ada(%rip), %rsi      # 0xa0a4
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 0x5aa4(%rip), %rsi      # 0xa0b9
movl $0xe, %edx
movq %r14, %rdi
callq 0x32f0
movq %r14, %rdi
movq %r13, %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 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x31a0
movq %rax, %r15
leaq 0x5a73(%rip), %r14      # 0xa0e1
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