// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %rdi, %rbp
movq 0x20(%rdi), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x10(%rax), %rbx
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, 0x30(%rsp)
movq 0x30(%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq 0x30(%rax), %rdx
movq %rdx, 0x90(%rsp)
movq 0x40(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x40(%rcx), %rax
movq %rax, 0x38(%rsp)
pushq $0x20
popq %rdi
callq 0x2180
movq %rax, %r12
movq %rax, %rdi
callq 0x7fdc
movslq 0x2c(%rbp), %r14
leaq (,%r14,4), %rdi
testq %r14, %r14
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x80(%rsp)
movq %rax, (%r12)
movslq 0x38(%rbp), %rax
leaq (,%rax,4), %r15
movq %rax, 0x20(%rsp)
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, 0x48(%rsp)
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x40(%rsp)
movq %rax, 0x10(%r12)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x98(%rsp)
movq %rax, 0x18(%r12)
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
xorl %ecx, %ecx
movq 0x80(%rsp), %rsi
cmpq %rax, %r13
je 0x5a27
movl (%rbx,%rax,4), %edx
cmpl %r14d, %edx
jl 0x5a22
movslq %ecx, %rcx
movl %edx, (%rsi,%rcx,4)
incl %ecx
incq %rax
jmp 0x5a0d
movq %r12, 0x88(%rsp)
movq 0xb0(%rbp), %rdi
movq 0x20(%rsp), %rsi
callq 0x3f1a
movq %rax, 0x20(%rsp)
movq 0xb0(%rbp), %rdi
movl 0x2c(%rbp), %esi
callq 0x3f1a
movq %rax, 0x50(%rsp)
xorl %ebx, %ebx
movq 0x60(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x30(%rsp), %r11
movq 0x28(%rsp), %rax
movl 0x2c(%rbp), %ecx
movslq %ecx, %r13
cmpq %r13, %rbx
jge 0x5c24
cmpb $0x1, (%rax,%rbx)
je 0x5c1c
movslq (%r11,%rbx,4), %r12
testq %r12, %r12
je 0x5c1c
movq 0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq (%rax,%rbx,8), %r14
leaq (%r14,%r12,4), %r15
movq %r9, 0x68(%rsp)
movq %r10, 0x70(%rsp)
movl %ecx, 0x78(%rsp)
bsrq %r12, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x719a
cmpl $0x11, %r12d
pushq $0x1
popq %r13
jl 0x5b3c
leaq 0x40(%r14), %r15
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7547
leaq -0x40(,%r12,4), %r12
testq %r12, %r12
je 0x5b5a
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x75d8
addq $0x4, %r15
addq $-0x4, %r12
jmp 0x5b12
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x7547
movq 0x38(%rsp), %rax
movslq (%rax,%rbx,4), %rax
movq 0x48(%rsp), %r15
andl $0x0, (%r15,%rax,4)
movslq (%r14), %rcx
movslq 0x2c(%rbp), %rdx
addq %rcx, %rdx
movq 0x58(%rsp), %r10
movslq (%r10,%rdx,4), %rcx
movq 0x60(%rsp), %r9
movl (%r9,%rcx,4), %edi
movq 0x20(%rsp), %rcx
movl %edi, (%rcx,%rax,4)
xorl %ecx, %ecx
pushq $0x1
popq %rax
movl %r13d, %edx
movq 0x30(%rsp), %r11
movq 0x40(%rsp), %r12
movslq (%r11,%rbx,4), %rsi
cmpq %rsi, %rax
jge 0x5bfa
movslq (%r14,%rax,4), %rsi
movslq 0x2c(%rbp), %r8
addq %rsi, %r8
movslq (%r10,%r8,4), %rsi
movl (%r9,%rsi,4), %esi
cmpl %edi, %esi
jne 0x5bc7
incl %edx
jmp 0x5bf3
movq 0x38(%rsp), %rdi
movl (%rdi,%rbx,4), %edi
leal (%rdi,%rcx), %r8d
movslq %r8d, %r8
movl %edx, (%r12,%r8,4)
leal (%rdi,%rcx), %edx
incl %edx
incl %ecx
movslq %edx, %rdx
movl %eax, (%r15,%rdx,4)
movq 0x20(%rsp), %rdi
movl %esi, (%rdi,%rdx,4)
movl %r13d, %edx
incq %rax
movl %esi, %edi
jmp 0x5ba3
movq 0x38(%rsp), %rax
movslq (%rax,%rbx,4), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movl %edx, (%r12,%rax,4)
incl %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax,%rbx,4)
movq 0x28(%rsp), %rax
incq %rbx
jmp 0x5a6f
movq 0x40(%rbp), %rax
movq %rbp, 0x40(%rsp)
movq 0x90(%rbp), %rdx
movq %rdx, 0x48(%rsp)
movq 0x18(%rax), %rbx
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
testl %ecx, %ecx
cmovlel %ebp, %ecx
movq %rcx, 0x28(%rsp)
cmpq %rcx, %rbp
je 0x5cff
movq 0x48(%rsp), %rax
cmpb $0x1, (%rax,%rbp)
je 0x5c74
movq 0x90(%rsp), %rax
cmpb $0x1, (%rax,%rbp)
jne 0x5c7e
incq %rbp
movq 0x28(%rsp), %rcx
jmp 0x5c52
movslq (%r10,%rbp,4), %rax
movl (%r9,%rax,4), %r14d
xorl %r15d, %r15d
cmpl (%rbx,%rbp,4), %r15d
jge 0x5c74
movq 0xa0(%rsp), %rax
movslq (%rax,%rbp,4), %r12
addq %r15, %r12
movq 0x30(%rsp), %rax
movslq (%rax,%r12,4), %rax
movslq (%r10,%rax,4), %rax
movl (%r9,%rax,4), %eax
sarl %eax
cltq
movq 0x80(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
subq %r13, %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,4), %rsi
movq 0x50(%rsp), %rcx
movl (%rcx,%rax,4), %edx
movl %r14d, %ecx
callq 0x66f4
movq 0x58(%rsp), %r10
movq 0x60(%rsp), %r9
movq 0x98(%rsp), %rcx
movl %eax, (%rcx,%r12,4)
incq %r15
jmp 0x5c89
movq 0x40(%rsp), %rbx
movq 0xb0(%rbx), %rdi
movl 0x38(%rbx), %edx
movq 0x20(%rsp), %rsi
callq 0x3fee
movq 0xb0(%rbx), %rdi
movl 0x2c(%rbx), %edx
movq 0x50(%rsp), %rsi
callq 0x3fee
movq 0x88(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22e0