// File: /workspace/github_cpp_makefile_star2_O3/SNUCSE-CTA[P]GI/src/backtrack.cpp
// Function: Backtrack::buildCS()
CS* Backtrack::buildCS()
{
	#ifdef DEBUG
	cout << __PRETTY_FUNCTION__ << endl;
	#endif

	int32_t i, j, size, prevColor, currColor;
	int32_t* color = coloring->color;
	int32_t* perm = coloring->perm;
	int32_t* inv = coloring->inv;
	int32_t numNode = coloring->numNode;

	int32_t* neigh = NULL;
	int32_t* d2 = g2->d;
	char* one1 = g1->one;
	char* one2 = g2->one;
	int32_t* adjPos1 = g1->adjPos;
	int32_t* adjPos2 = g2->adjPos;

	//allocate memory for CS
	CS* cs = new CS;
	cs->candArr = new int32_t[n];
	cs->P = new int32_t[e2];
	cs->S = new int32_t[e2];
	cs->dagColorIndex = new int32_t[e2];
	int32_t* candArr = cs->candArr;
	int32_t* P = cs->P;
	int32_t* S = cs->S;
	int32_t* dagColorIndex = cs->dagColorIndex;

	//A. build candidate set
	int32_t idx = 0;
	for(i = 0; i < numNode; ++i) {
		if( perm[i] >= n ) {
			candArr[idx] = perm[i];
			++idx;
		}
	}

	//B. build edges
	
	//Step B-1. sort adjacency list of aG2
	int32_t* NC = global_memory->getLLArray(e2);
	int32_t* NCSize = global_memory->getLLArray(n);
	for(i = 0; i < n; ++i) {
		if( one2[i] == 1 || d2[i] == 0 ) //nodes of coreness-1
			continue;

		neigh = g2->e[i];
		int32_t padn = n;
		sort(neigh, neigh + d2[i],
			[color, inv, padn](const int32_t& a, const int32_t& b) -> bool {
				return color[inv[a + padn]] < color[inv[b + padn]];
			}
		);

		//store start position for each color in neigh
		idx = 0; //color index
		P[adjPos2[i]] = 0;
		size = 1;
		prevColor = color[inv[ neigh[0] + n ]];
		NC[adjPos2[i]] = prevColor;
		for(j = 1; j < d2[i]; ++j) {
			currColor = color[inv[ neigh[j] + n ]];
			if( currColor != prevColor ) {
				S[adjPos2[i] + idx] = size;
				++idx;
				size = 1;
				P[adjPos2[i] + idx] = j;
				NC[adjPos2[i] + idx] = currColor;
			}
			else {
				++size;
			}
			prevColor = currColor;
		}
		S[adjPos2[i] + idx] = size;
		NCSize[i] = idx + 1;
	}

	//Step B-2. compute color index
	int32_t adj, adjc, c, ci, cand;
	int32_t* parentSize = dag->parentSize;
	int32_t* dagArr = dag->dagArr;
	for(i = 0; i < n; ++i) { //for each vertex ui in G1
		if( isBinary[i] == 1 ) //binary cells are already mapped.
			continue;

		if( one1[i] == 1 ) //coreness-1 vertices.
			continue;

		c = color[inv[i]]; //note that ID of a vertex in aG1 is in [0, n).
		for(j = 0; j < parentSize[i]; ++j) {
			//for each parent adj of ui
			adj = dagArr[ adjPos1[i] + j ];
			adjc = color[inv[ adj ]];
			cand = candArr[(adjc >> 1)] - n; //a candidate of adj
			ci = binarySearch(NC + adjPos2[cand], NCSize[cand], c);
			dagColorIndex[ adjPos1[i] + j ] = ci;
		}
	}

	global_memory->returnLLArray(NC, e2);
	global_memory->returnLLArray(NCSize, n);

	return cs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbp
movq 0x20(%rdi), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x78(%rsp)
movq 0x10(%rax), %r14
movq 0x18(%rax), %rcx
movq %rcx, 0x58(%rsp)
movl (%rax), %r13d
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x30(%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x30(%rax), %rdx
movq %rdx, 0xa8(%rsp)
movq 0x40(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x40(%rcx), %rax
movq %rax, 0x80(%rsp)
movl $0x20, %edi
callq 0x2180
movq %rax, %r15
movq %rax, %rdi
callq 0x9004
movslq 0x2c(%rbp), %r12
leaq (,%r12,4), %rdi
testq %r12, %r12
movq $-0x1, %rbx
cmovsq %rbx, %rdi
callq 0x2030
movq %rax, 0x88(%rsp)
movq %rax, (%r15)
movslq 0x38(%rbp), %rax
movq %r15, 0x28(%rsp)
leaq (,%rax,4), %r15
movq %rax, 0x40(%rsp)
testq %rax, %rax
cmovsq %rbx, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rcx, 0x8(%rax)
movq %r15, %rdi
callq 0x2030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x38(%rsp)
movq %rcx, 0x10(%rax)
movq %r15, %rdi
callq 0x2030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x98(%rsp)
movq %rcx, 0x18(%rax)
testl %r13d, %r13d
jle 0x6855
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x88(%rsp), %rsi
movl (%r14,%rax,4), %edx
cmpl %r12d, %edx
jl 0x684d
movslq %ecx, %rcx
movl %edx, (%rsi,%rcx,4)
incl %ecx
incq %rax
cmpq %rax, %r13
jne 0x683c
movq 0xb0(%rbp), %rdi
movq 0x40(%rsp), %rsi
callq 0x4aba
movq %rax, 0x50(%rsp)
movq 0xb0(%rbp), %rdi
movl 0x2c(%rbp), %esi
callq 0x4aba
movq %rax, %rdi
movq %rbp, 0x48(%rsp)
movl 0x2c(%rbp), %eax
testl %eax, %eax
movq 0x58(%rsp), %r10
jle 0x6be2
xorl %r14d, %r14d
movq 0x48(%rsp), %rbx
movq 0x78(%rsp), %rbp
movq 0x20(%rsp), %r13
movq 0x30(%rsp), %rcx
movq %rdi, 0x40(%rsp)
cmpb $0x1, (%rcx,%r14)
je 0x6a6f
movslq (%r13,%r14,4), %r13
testq %r13, %r13
je 0x6968
movq 0x18(%rbx), %rcx
movq 0x38(%rcx), %rcx
movq (%rcx,%r14,8), %r12
leaq (%r12,%r13,4), %r15
movq %rbp, 0x60(%rsp)
movq %r10, 0x68(%rsp)
movl %eax, 0x70(%rsp)
bsrq %r13, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x83f1
cmpl $0x11, %r13d
jl 0x6972
shlq $0x2, %r13
leaq 0x40(%r12), %r15
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x86fa
addq $-0x40, %r13
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x87b0
addq $0x4, %r15
addq $-0x4, %r13
jne 0x6941
jmp 0x6990
movq 0x20(%rsp), %r13
jmp 0x6a6f
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x86fa
movq 0x80(%rsp), %r11
movslq (%r11,%r14,4), %rax
movq 0x18(%rsp), %rcx
movl $0x0, (%rcx,%rax,4)
movslq (%r12), %rcx
movslq 0x2c(%rbx), %rdx
addq %rcx, %rdx
movq 0x58(%rsp), %r10
movslq (%r10,%rdx,4), %rcx
movl (%rbp,%rcx,4), %esi
movq 0x50(%rsp), %r15
movl %esi, (%r15,%rax,4)
movq 0x20(%rsp), %r13
movl (%r13,%r14,4), %edi
cmpl $0x2, %edi
jl 0x6a44
xorl %eax, %eax
movl $0x1, %edx
movl $0x1, %ecx
movl %esi, %r8d
movslq (%r12,%rcx,4), %rsi
movslq 0x2c(%rbx), %r9
addq %rsi, %r9
movslq (%r10,%r9,4), %rsi
movl (%rbp,%rsi,4), %esi
cmpl %r8d, %esi
jne 0x6a03
incl %edx
jmp 0x6a37
movl (%r11,%r14,4), %edi
leal (%rdi,%rax), %r8d
movslq %r8d, %r8
movq 0x38(%rsp), %r9
movl %edx, (%r9,%r8,4)
leal (%rdi,%rax), %edx
incl %edx
incl %eax
movslq %edx, %rdx
movq 0x18(%rsp), %rdi
movl %ecx, (%rdi,%rdx,4)
movl %esi, (%r15,%rdx,4)
movl (%r13,%r14,4), %edi
movl $0x1, %edx
incq %rcx
movslq %edi, %r8
cmpq %r8, %rcx
jl 0x69e4
jmp 0x6a4b
movl $0x1, %edx
xorl %eax, %eax
movslq (%r11,%r14,4), %rcx
cltq
addq %rax, %rcx
movq 0x38(%rsp), %rsi
movl %edx, (%rsi,%rcx,4)
incl %eax
movq 0x40(%rsp), %rdi
movl %eax, (%rdi,%r14,4)
movl 0x2c(%rbx), %eax
movq 0x30(%rsp), %rcx
incq %r14
movslq %eax, %r11
cmpq %r11, %r14
jl 0x68ae
testl %eax, %eax
jle 0x6be2
movq 0x48(%rsp), %rdx
movq 0x40(%rdx), %rcx
movq 0x18(%rcx), %rsi
movq 0x20(%rcx), %rcx
movq %rcx, 0x90(%rsp)
movq 0x90(%rdx), %rcx
movq %rcx, 0xb8(%rsp)
movl %eax, %eax
movq %rax, 0xb0(%rsp)
movq $0x0, 0x18(%rsp)
movq %rsi, 0xc0(%rsp)
movq 0x78(%rsp), %rbp
movq 0x88(%rsp), %r15
movq 0x50(%rsp), %r14
movq 0xb8(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpb $0x1, (%rax,%rcx)
movq 0x80(%rsp), %r8
je 0x6bc7
movq 0xa8(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpb $0x1, (%rax,%rcx)
je 0x6bc7
movq 0x18(%rsp), %rax
cmpl $0x0, (%rsi,%rax,4)
jle 0x6bc7
movq 0x18(%rsp), %rcx
movslq (%r10,%rcx,4), %rax
movl (%rbp,%rax,4), %eax
movl %eax, 0x38(%rsp)
movq 0xa0(%rsp), %rax
movslq (%rax,%rcx,4), %rax
movq 0x98(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
movq %rcx, 0x20(%rsp)
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %rax
movslq (%rax,%r12,4), %rax
movslq (%r10,%rax,4), %rax
movl (%rbp,%rax,4), %eax
sarl %eax
cltq
movslq (%r15,%rax,4), %rax
subq %r11, %rax
movslq (%r8,%rax,4), %rcx
leaq (%r14,%rcx,4), %rsi
movl (%rdi,%rax,4), %edx
movl 0x38(%rsp), %ecx
movq %r8, %rbx
movq %r11, %r13
callq 0x7756
movq %r13, %r11
movq %rbx, %r8
movq 0xc0(%rsp), %rsi
movq 0x40(%rsp), %rdi
movq 0x58(%rsp), %r10
movq 0x20(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movq 0x18(%rsp), %rax
cmpl (%rsi,%rax,4), %r12d
jl 0x6b62
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0xb0(%rsp), %rcx
jne 0x6adb
movq 0x48(%rsp), %rbx
movq %rdi, %r14
movq 0xb0(%rbx), %rdi
movl 0x38(%rbx), %edx
movq 0x50(%rsp), %rsi
callq 0x4c64
movq 0xb0(%rbx), %rdi
movl 0x2c(%rbx), %edx
movq %r14, %rsi
callq 0x4c64
movq 0x28(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22f0
nop