// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq 0x20(%rax), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, -0x28(%rbp)
movq 0x20(%rax), %rcx
movq 0x10(%rcx), %rcx
movq %rcx, -0x30(%rbp)
movq 0x20(%rax), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x38(%rbp)
movq 0x20(%rax), %rcx
movl (%rcx), %ecx
movl %ecx, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movq 0x18(%rax), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0x50(%rbp)
movq 0x10(%rax), %rcx
movq 0x30(%rcx), %rcx
movq %rcx, -0x58(%rbp)
movq 0x18(%rax), %rcx
movq 0x30(%rcx), %rcx
movq %rcx, -0x60(%rbp)
movq 0x10(%rax), %rcx
movq 0x40(%rcx), %rcx
movq %rcx, -0x68(%rbp)
movq 0x18(%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x20, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x110(%rbp)
callq 0x13610
jmp 0xcdb9
movq -0x118(%rbp), %rax
movq -0x110(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movslq 0x2c(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq %rax, %rdx
movq -0x118(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, (%rcx)
movslq 0x38(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq %rax, %rdx
movq -0x118(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, 0x8(%rcx)
movslq 0x38(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq %rax, %rdx
movq -0x118(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, 0x10(%rcx)
movslq 0x38(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0xcf69
movq -0x118(%rbp), %rcx
movq -0x30(%rbp), %rax
movslq -0xc(%rbp), %rdx
movl (%rax,%rdx,4), %eax
cmpl 0x2c(%rcx), %eax
jl 0xcf5c
movq -0x30(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x90(%rbp), %rax
movslq -0xac(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xcf5c
movq -0x110(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x20, %esi
callq 0x31d0
jmp 0xd3d1
jmp 0xcf5e
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xceeb
movq -0x118(%rbp), %rax
movq 0xb0(%rax), %rdi
movl 0x38(%rax), %esi
callq 0x77d0
movq %rax, %rcx
movq -0x118(%rbp), %rax
movq %rcx, -0xb8(%rbp)
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x77d0
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x118(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jge 0xd1fe
movq -0x60(%rbp), %rax
movslq -0xc(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xcfdf
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0xcfe4
jmp 0xd1f0
movq -0x118(%rbp), %rax
movq 0x18(%rax), %rcx
movq 0x38(%rcx), %rcx
movslq -0xc(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x48(%rbp)
movl 0x2c(%rax), %eax
movl %eax, -0xc4(%rbp)
movq -0x48(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xd0(%rbp)
leaq -0xe0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0xe520
movq -0x118(%rbp), %rsi
movl $0x0, -0xac(%rbp)
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0x1, -0x14(%rbp)
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rdx
movl (%rdx), %edx
addl 0x2c(%rsi), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edx
movq -0xb8(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x1, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x50(%rbp), %rcx
movslq -0xc(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0xd1b8
movq -0x118(%rbp), %rsi
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rdx
movslq -0x10(%rbp), %rdi
movl (%rdx,%rdi,4), %edx
addl 0x2c(%rsi), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0xd19b
movl -0x14(%rbp), %edx
movq -0xa0(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
addl -0xac(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
movl $0x1, -0x14(%rbp)
movl -0x10(%rbp), %edx
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
addl -0xac(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %edx
movq -0xb8(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
addl -0xac(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xd1a4
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xd0da
movl -0x14(%rbp), %edx
movq -0xa0(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
addl -0xac(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0xac(%rbp), %edx
addl $0x1, %edx
movq -0xc0(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xcfad
movq -0x118(%rbp), %rax
movq 0x40(%rax), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x100(%rbp)
movq 0x40(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x118(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jge 0xd38a
movq -0x118(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0xc(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0xd25d
jmp 0xd37c
movq -0x58(%rbp), %rax
movslq -0xc(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0xd273
jmp 0xd37c
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xec(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x100(%rbp), %rcx
movslq -0xc(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0xd37a
movq -0x118(%rbp), %rdi
movq -0x108(%rbp), %rax
movq -0x68(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x10(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xe4(%rbp)
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0xe4(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xe8(%rbp)
movq -0x90(%rbp), %rax
movl -0xe8(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
subl 0x2c(%rdi), %eax
movl %eax, -0xf4(%rbp)
movq -0xb8(%rbp), %rsi
movq -0x70(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0xc0(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0xec(%rbp), %ecx
callq 0xe5c0
movl %eax, -0xf0(%rbp)
movl -0xf0(%rbp), %edx
movq -0xa8(%rbp), %rax
movq -0x68(%rbp), %rcx
movslq -0xc(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
addl -0x10(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xd293
jmp 0xd37c
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xd22a
movq -0x118(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0xb8(%rbp), %rsi
movl 0x38(%rax), %edx
callq 0x7a30
movq -0x118(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0xc0(%rbp), %rsi
movl 0x2c(%rax), %edx
callq 0x7a30
movq -0x78(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x3370
nopw (%rax,%rax)