// File: /workspace/github_cpp_makefile_star2_O1/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, 0x80(%rsp)
movq 0x10(%rax), %r13
movq 0x18(%rax), %rcx
movq %rcx, 0x40(%rsp)
movl (%rax), %r14d
movq 0x10(%rdi), %rax
movq 0x18(%rdi), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq 0x30(%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x30(%rax), %rdx
movq %rdx, 0x98(%rsp)
movq 0x40(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x40(%rcx), %rax
movq %rax, 0x88(%rsp)
movl $0x20, %edi
callq 0x2180
movq %rax, %rbx
movq %rax, %rdi
callq 0x90f4
movslq 0x2c(%rbp), %r15
leaq (,%r15,4), %rdi
testq %r15, %r15
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, 0x58(%rsp)
movq %rax, (%rbx)
movslq 0x38(%rbp), %rax
leaq (,%rax,4), %r12
movq %rax, 0x28(%rsp)
testq %rax, %rax
movq $-0x1, %rax
cmovsq %rax, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, 0x50(%rsp)
movq %rax, 0x8(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x18(%rbx)
testl %r14d, %r14d
jle 0x68d5
xorl %edi, %edi
xorl %ecx, %ecx
movq 0x58(%rsp), %rsi
movl (%r13,%rdi,4), %edx
cmpl %r15d, %edx
jl 0x68cd
movslq %ecx, %rcx
movl %edx, (%rsi,%rcx,4)
incl %ecx
incq %rdi
cmpq %rdi, %r14
jne 0x68bb
movq %rax, 0xb0(%rsp)
movq %rbx, 0x90(%rsp)
movq 0xb0(%rbp), %rdi
movq 0x28(%rsp), %rsi
callq 0x4b34
movq %rax, 0x28(%rsp)
movq 0xb0(%rbp), %rdi
movl 0x2c(%rbp), %esi
callq 0x4b34
movq %rax, 0x48(%rsp)
movl 0x2c(%rbp), %eax
testl %eax, %eax
jle 0x6af8
xorl %r13d, %r13d
movq 0x80(%rsp), %rbx
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r11
movq 0x30(%rsp), %rcx
cmpb $0x1, (%rcx,%r13)
je 0x6ae6
movslq (%r11,%r13,4), %r15
testq %r15, %r15
je 0x6ae6
movq 0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq (%rcx,%r13,8), %r14
leaq (%r14,%r15,4), %r12
movq %rbx, 0x60(%rsp)
movq %r9, 0x68(%rsp)
movl %eax, 0x70(%rsp)
bsrq %r15, %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 %r14, %rdi
movq %r12, %rsi
callq 0x84e5
cmpl $0x11, %r15d
jl 0x69ec
shlq $0x2, %r15
leaq 0x40(%r14), %r12
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x87f9
addq $-0x40, %r15
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0x88a3
addq $0x4, %r12
addq $-0x4, %r15
jne 0x69c5
jmp 0x6a0a
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x60(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x87f9
movq 0x88(%rsp), %r10
movslq (%r10,%r13,4), %rax
movq 0x50(%rsp), %r12
movl $0x0, (%r12,%rax,4)
movslq (%r14), %rax
movslq 0x2c(%rbp), %rcx
addq %rax, %rcx
movq 0x40(%rsp), %r9
movslq (%r9,%rcx,4), %rax
movl (%rbx,%rax,4), %esi
movslq (%r10,%r13,4), %rax
movq 0x28(%rsp), %r15
movl %esi, (%r15,%rax,4)
movq 0x38(%rsp), %r11
cmpl $0x2, (%r11,%r13,4)
jl 0x6abe
xorl %eax, %eax
movl $0x1, %edx
movl $0x1, %ecx
movl %esi, %edi
movslq (%r14,%rcx,4), %rsi
movslq 0x2c(%rbp), %r8
addq %rsi, %r8
movslq (%r9,%r8,4), %rsi
movl (%rbx,%rsi,4), %esi
cmpl %edi, %esi
jne 0x6a7a
incl %edx
jmp 0x6ab0
movslq (%r10,%r13,4), %rdi
cltq
addq %rax, %rdi
movq 0x20(%rsp), %r8
movl %edx, (%r8,%rdi,4)
movslq (%r10,%r13,4), %rdx
addq %rax, %rdx
incq %rdx
incq %rax
movl %ecx, (%r12,%rdx,4)
movslq (%r10,%r13,4), %rdx
movslq %eax, %rdi
addq %rdx, %rdi
movl %esi, (%r15,%rdi,4)
movl $0x1, %edx
incq %rcx
movslq (%r11,%r13,4), %rdi
cmpq %rdi, %rcx
jl 0x6a5e
jmp 0x6ac5
movl $0x1, %edx
xorl %eax, %eax
movslq (%r10,%r13,4), %rcx
cltq
addq %rax, %rcx
movq 0x20(%rsp), %rsi
movl %edx, (%rsi,%rcx,4)
incl %eax
movq 0x48(%rsp), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x30(%rsp), %rcx
incq %r13
movslq 0x2c(%rbp), %rax
cmpq %rax, %r13
jl 0x6934
jmp 0x6afd
movq 0x40(%rsp), %r9
movq %rbp, 0x78(%rsp)
testl %eax, %eax
jle 0x6c7d
movq 0x78(%rsp), %rdx
movq 0x40(%rdx), %rcx
movq 0x18(%rcx), %rsi
movq 0x20(%rcx), %rcx
movq 0x90(%rdx), %rdx
movq %rdx, 0xa8(%rsp)
movl %eax, %edx
movq %rdx, 0xa0(%rsp)
movq $0x0, 0x20(%rsp)
movslq %eax, %rdx
movq %rcx, 0x30(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0xc0(%rsp)
movq 0xa8(%rsp), %rax
movq 0x20(%rsp), %rdi
cmpb $0x1, (%rax,%rdi)
movq 0x80(%rsp), %rdi
movq 0xb8(%rsp), %r10
movq 0x88(%rsp), %r11
movq 0x58(%rsp), %r14
movq 0xb0(%rsp), %r15
movq 0x28(%rsp), %r13
movq 0x48(%rsp), %rbp
je 0x6c62
movq 0x98(%rsp), %rax
movq 0x20(%rsp), %r8
cmpb $0x1, (%rax,%r8)
je 0x6c62
movq 0x20(%rsp), %rax
cmpl $0x0, (%rsi,%rax,4)
jle 0x6c62
movq 0x20(%rsp), %rax
movslq (%r9,%rax,4), %rax
movl (%rdi,%rax,4), %eax
movl %eax, 0x38(%rsp)
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movslq (%r10,%rax,4), %r12
addq %rbx, %r12
movslq (%rcx,%r12,4), %rax
movslq (%r9,%rax,4), %rax
movl (%rdi,%rax,4), %eax
sarl %eax
cltq
movslq (%r14,%rax,4), %rax
subq %rdx, %rax
movslq (%r11,%rax,4), %rcx
leaq (,%rcx,4), %rsi
addq %r13, %rsi
movl (%rbp,%rax,4), %edx
movl 0x38(%rsp), %ecx
movq %rdi, %r13
movq %r10, %rbp
movq %r15, %r14
movq %r11, %r15
callq 0x785e
movq %r15, %r11
movq %r14, %r15
movq %rbp, %r10
movq 0x48(%rsp), %rbp
movq %r13, %rdi
movq 0x28(%rsp), %r13
movq 0xc0(%rsp), %rdx
movq 0x58(%rsp), %r14
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rsi
movq 0x40(%rsp), %r9
movl %eax, (%r15,%r12,4)
incq %rbx
movq 0x20(%rsp), %rax
cmpl (%rsi,%rax,4), %ebx
jl 0x6bd1
movq 0x20(%rsp), %rdi
incq %rdi
movq %rdi, 0x20(%rsp)
cmpq 0xa0(%rsp), %rdi
jne 0x6b52
movq 0x78(%rsp), %rbx
movq 0xb0(%rbx), %rdi
movl 0x38(%rbx), %edx
movq 0x28(%rsp), %rsi
callq 0x4cf0
movq 0xb0(%rbx), %rdi
movl 0x2c(%rbx), %edx
movq 0x48(%rsp), %rsi
callq 0x4cf0
movq 0x90(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x20, %esi
movq %rbx, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x22f0