// File: /workspace/github_cpp_makefile_star2_O2/sjain12[P]CliqueCounting/tests/../Escape/CliquesColorCoding.h
// Function: cliques_color_coding(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>>, int)
void cliques_color_coding(CGraph &CG, CDAG &DG, ofstream &of, string gname, string fname, int clique_size)
{    
    high_resolution_clock::time_point t_elim_s = high_resolution_clock::now();
    
    Stack* stack = newStack();
    VertexSet *path = NULL, *nbrs = NULL;
    vector<VertexIdx> color;
    color.resize(CG.nVertices);
    double num_cliques = 0, est = 0;
    std::random_device rd{}; // use to seed the rng 
    std::mt19937 rng{rd()}; // rng

    std::uniform_int_distribution<int> distribution(0,clique_size-1);
    int col = 0;
    for (VertexIdx i=0; i<CG.nVertices; i++)
    {
        col = distribution(rng); 
        color[i] = col;
    }

    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;

            if (psize + nsize < clique_size)
            {
            }
            else if (psize == clique_size)
            {   
                num_cliques++;
            }
            else
            {
                for (VertexIdx i=0; i<nsize; i++)
                {
                    VertexIdx v = nbrs->vertices[i];
                    int flag = 0;
                    for (int j=0; j<psize; j++)
                    {
                        if (color[path->vertices[j]] == color[v])
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1) continue;

                    VertexSet *new_path = newVertexSet(psize+1);
                    std::copy(path->vertices, path->vertices + psize, new_path->vertices);
                    new_path->vertices[path->nVertices] = v;   //added i to path

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

                    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();
    
    double kfac = 1;

    for (int i=1; i<=clique_size; i++)
        kfac *= i;
    est = num_cliques * pow(clique_size, clique_size) / kfac; 

    cout << "time reqd: " << duration_program << endl;
	cout << "number of cliques = " << num_cliques << endl;
	cout << "clique size = " << clique_size << endl;
    cout << "estimated number of cliques = " << est << endl;

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x27c8, %rsp           # imm = 0x27C8
movq %r9, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
callq 0x3050
movq %rax, 0x50(%rsp)
callq 0x3c67
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq %r14, 0x38(%rsp)
movq (%r14), %rsi
callq 0x4b36
leaq 0xb8(%rsp), %rdi
callq 0x4b68
movq %rbx, 0x48(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3350
movl %eax, %esi
leaq 0x1440(%rsp), %rdi
callq 0x5714
leaq 0xb0(%rsp), %rbx
andl $0x0, (%rbx)
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x4(%rbx)
xorl %r15d, %r15d
leaq 0x1440(%rsp), %r14
movq 0x38(%rsp), %rax
movq (%rax), %rax
cmpq %rax, %r15
jge 0x3e4c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4bbe
cltq
movq 0x90(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
jmp 0x3e21
movslq 0x8(%rsp), %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
movq %r15, 0x58(%rsp)
cmpq %rax, %r13
jge 0x4143
pushq $0x1
popq %rdi
callq 0x3a88
movq %rax, %rbx
movq 0x8(%rax), %rax
movq %r13, (%rax)
movq 0x10(%r12), %rax
movq 0x8(%rax,%r13,8), %rdi
subq (%rax,%r13,8), %rdi
callq 0x3a88
movq %rax, %r14
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rsi
incq %r13
shlq $0x3, %rsi
movq (%rax,%r13,8), %rdx
shlq $0x3, %rdx
subq %rsi, %rdx
je 0x3ebf
addq 0x18(%r12), %rsi
movq 0x8(%r14), %rdi
callq 0x3440
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3c01
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x3c86
movq %r13, 0x60(%rsp)
movq 0x20(%rsp), %rdi
callq 0x3cb2
testq %rax, %rax
je 0x4136
movq %rax, 0x68(%rsp)
movq (%rax), %rax
movq (%rax), %r14
movq 0x8(%rax), %r13
movq (%r14), %rcx
movq (%r13), %rax
movq %rax, 0x28(%rsp)
addq %rcx, %rax
cmpq %r15, %rax
jl 0x40c9
cmpq %r15, %rcx
jne 0x3f34
movsd 0x10(%rsp), %xmm0
addsd 0x50df(%rip), %xmm0     # 0x9008
movsd %xmm0, 0x10(%rsp)
jmp 0x40c9
leaq 0x1(%rcx), %rax
movq %rax, 0x80(%rsp)
leaq (,%rcx,8), %rax
movq %rax, 0x70(%rsp)
testq %rcx, %rcx
movl $0x0, %eax
cmovgq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rax
testq %rax, %rax
movl $0x0, %edx
cmovgq %rax, %rdx
movq %rdx, 0x88(%rsp)
xorl %edx, %edx
movq %rcx, 0x78(%rsp)
cmpq 0x88(%rsp), %rdx
je 0x40c0
movq 0x8(%r13), %rax
movq %rdx, 0x30(%rsp)
movq (%rax,%rdx,8), %r15
movq 0x90(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, 0x18(%rsp)
je 0x3fc4
movq 0x8(%r14), %rdx
movq (%rdx,%rcx,8), %rdx
movq (%rax,%rdx,8), %rdx
incq %rcx
cmpq (%rax,%r15,8), %rdx
jne 0x3fa3
jmp 0x40b3
movq 0x80(%rsp), %rdi
callq 0x3a88
cmpq $0x0, 0x78(%rsp)
movq %rax, 0x40(%rsp)
je 0x3ff5
movq 0x8(%r14), %rsi
movq 0x8(%rax), %rdi
movq 0x70(%rsp), %rdx
callq 0x3440
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq (%r14), %rcx
movq %r15, (%rax,%rcx,8)
movq 0x10(%r12), %rax
movq 0x8(%rax,%r15,8), %rcx
subq (%rax,%r15,8), %rcx
movq 0x28(%rsp), %rax
cmpl %ecx, %eax
cmovlq %rax, %rcx
movslq %ecx, %rdi
callq 0x3a88
movq 0x10(%r12), %rcx
movslq (%rcx,%r15,8), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
xorl %edi, %edi
movslq %edi, %r8
cmpq %rsi, (%r13)
jle 0x4096
movq %r14, %rbx
movq 0x8(%rcx,%r15,8), %r9
subq (%rcx,%r15,8), %r9
testq %r9, %r9
movl $0x0, %r10d
cmovleq %r10, %r9
movq %r12, %r14
movq 0x18(%r12), %r10
addq %rdx, %r10
xorl %r11d, %r11d
cmpq %r11, %r9
je 0x408b
movq 0x8(%r13), %rbp
movq (%rbp,%rsi,8), %rbp
leaq 0x1(%r11), %r12
cmpq (%r10,%r11,8), %rbp
movq %r12, %r11
jne 0x4062
movq 0x8(%rax), %rdi
movq %rbp, (%rdi,%r8,8)
incl %r8d
movl %r8d, %edi
incq %rsi
movq %r14, %r12
movq %rbx, %r14
jmp 0x4032
movq %r8, (%rax)
movq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x3c01
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x3c86
movq 0x30(%rsp), %rdx
incq %rdx
jmp 0x3f7e
movq (%r13), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
js 0x40df
movq 0x8(%r13), %rdi
testq %rdi, %rdi
je 0x40df
callq 0x3310
movq %r13, %rdi
pushq $0x10
popq %rbx
movq %rbx, %rsi
callq 0x32a0
cmpq $0x0, (%r14)
jle 0x4101
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x4101
callq 0x3310
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32a0
movq 0x68(%rsp), %r14
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x32a0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32a0
movq 0x58(%rsp), %r15
movq 0x60(%rsp), %r13
jmp 0x3edc
movq 0x38(%rsp), %rax
movq (%rax), %rax
jmp 0x3e63
movq 0x20(%rsp), %rdi
callq 0x3c78
callq 0x3050
subq 0x50(%rsp), %rax
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
testl %edx, %edx
cmovgl %edx, %ecx
movsd 0x4e9d(%rip), %xmm0     # 0x9008
movsd %xmm0, 0x18(%rsp)
subl $0x1, %ecx
jb 0x4190
movsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
addsd 0x4e7a(%rip), %xmm0     # 0x9008
jmp 0x4171
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rbx
cvtsi2sdl 0x8(%rsp), %xmm0
movapd %xmm0, %xmm1
callq 0x3490
movsd %xmm0, 0x30(%rsp)
movq 0x9e17(%rip), %rdi      # 0xdfd0
leaq 0x4ec8(%rip), %rsi      # 0x9088
callq 0x3270
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %rdi
callq 0x3140
movq 0x9df1(%rip), %rdi      # 0xdfd0
leaq 0x4ec7(%rip), %rsi      # 0x90ad
callq 0x3270
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3140
movq 0x9dc8(%rip), %rdi      # 0xdfd0
leaq 0x4e85(%rip), %rsi      # 0x9094
callq 0x3270
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x3480
movq %rax, %rdi
callq 0x3140
movq 0x9da0(%rip), %rdi      # 0xdfd0
leaq 0x4e6c(%rip), %rsi      # 0x90a3
callq 0x3270
movsd 0x10(%rsp), %xmm0
mulsd 0x30(%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
movq %rax, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x3430
movq %rax, %rdi
callq 0x3140
movq 0x48(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3480
leaq 0x4e61(%rip), %rsi      # 0x90db
movq %rax, %rdi
callq 0x3270
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3430
leaq 0x4e44(%rip), %rsi      # 0x90db
movq %rax, %rdi
callq 0x3270
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
leaq 0x4e2a(%rip), %rsi      # 0x90db
movq %rax, %rdi
callq 0x3270
movq %rax, %rdi
callq 0x3140
leaq 0xb8(%rsp), %rdi
callq 0x4bc6
leaq 0x90(%rsp), %rdi
callq 0x5526
addq $0x27c8, %rsp           # imm = 0x27C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x42ef
movq %rax, %rbx
jmp 0x4312
jmp 0x4302
jmp 0x4302
jmp 0x4302
jmp 0x4302
jmp 0x4302
jmp 0x4302
jmp 0x4302
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4bc6
leaq 0x90(%rsp), %rdi
callq 0x5526
movq %rbx, %rdi
callq 0x34a0