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

	DAG* bfsdag = new DAG;
	bfsdag->numNode = n;
	bfsdag->numEdge = e;
	bfsdag->childSize = new int32_t[n];
	bfsdag->parentSize = new int32_t[n];
	bfsdag->dagArr = new int32_t[e2];
	int32_t* childSize = bfsdag->childSize;
	memset(childSize, 0, sizeof(int32_t) * n);
	int32_t* parentSize = bfsdag->parentSize;
	memset(parentSize, 0, sizeof(int32_t) * n);
	int32_t* dagArr = bfsdag->dagArr;
	int32_t* adjPos1 = g1->adjPos;
	int32_t* d1 = g1->d;
	char* one1 = g1->one;

	int32_t i, curr, child;
	char* popped = (char*)global_memory->getLLArray(n); 
	memset(popped, 0, sizeof(char) * n);
	char* visited = (char*)global_memory->getLLArray(n); 
	memset(visited, 0, sizeof(char) * n);
	int32_t* bfsQueue = global_memory->getLLArray(n);

	int32_t queueStart = 0;
	int32_t queueEnd = 0;
	int32_t nextQueueEnd = 0;

	//set binary cells as roots
	memset(isBinary, 0, sizeof(char) * n);
	for(i = 0; i < coloring->numNode; i += coloring->cellSize[i]) {
		if( coloring->cellSize[i] == 2 ) {
			curr = coloring->perm[i] < n ? coloring->perm[i] : coloring->perm[i + 1];
			bfsQueue[queueEnd] = curr;
			++queueEnd;
			visited[curr] = 1;
			isBinary[curr] = 1;
		}
	}

	//if there is no binary cells
	if( queueEnd == 0 ) {
		bfsdag->root = selectRoot();
		bfsQueue[queueEnd] = bfsdag->root;
		++queueEnd;
		visited[bfsdag->root] = 1;
	}

	//BFS
	nextQueueEnd = queueEnd;
	while( true ) {
		//start of a level
		g1->sortByDegreeDec(bfsQueue + queueStart, bfsQueue + queueEnd);
		
		while( queueStart < queueEnd ) {
			curr = bfsQueue[queueStart];
			++queueStart;
			popped[curr] = 1;

			//for each neighbor of curr
			int32_t* neigh = g1->e[curr];
			for(i = 0; i < d1[curr]; ++i) {
				child = neigh[i];
				if( popped[child] == 0 ) {
					dagArr[ adjPos1[child] + parentSize[child] ] = curr;
					dagArr[ adjPos1[curr] + d1[curr] - 1 - childSize[curr] ] = child;

					++(parentSize[child]);
					++(childSize[curr]);

					//note that visited[child] == 1 if popped[child] == 1
					if( visited[child] == 0 ) {
						visited[child] = 1;
						bfsQueue[nextQueueEnd] = child;
						++nextQueueEnd;
					}
				}
			} //for (i)
		} //while(queueStart < queueEnd)

		if( queueEnd == nextQueueEnd ) //no nodes have been pushed in
			break;

		queueStart = queueEnd;
		queueEnd = nextQueueEnd;
	} //while(true)

	//we sort parent(u) for each u, for the partial failing set
	for(i = 0; i < n; ++i) {
		//parent(u) is needed for only non-binary vertex
		if( isBinary[i] == 0 && one1[i] == 0 ) {
			sort( dagArr + adjPos1[i], dagArr + adjPos1[i] + parentSize[i] );
		}
	}

	global_memory->returnLLArray((int32_t*)popped, n);
	global_memory->returnLLArray((int32_t*)visited, n);
	global_memory->returnLLArray(bfsQueue, n);

	return bfsdag;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl $0x28, %edi
callq 0x31c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
callq 0x6770
jmp 0xc6f8
movq -0xa0(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, -0x10(%rbp)
movl 0x2c(%rax), %edx
movq -0x10(%rbp), %rcx
movl %edx, (%rcx)
movl 0x30(%rax), %edx
movq -0x10(%rbp), %rcx
movl %edx, 0x4(%rcx)
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 -0xa0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rdx, 0x10(%rcx)
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 -0xa0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rdx, 0x18(%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 -0xa0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rdx, 0x20(%rcx)
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movslq 0x2c(%rax), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x3110
movq -0xa0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movslq 0x2c(%rax), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x3110
movq -0xa0(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0x38(%rbp)
movq 0x10(%rax), %rcx
movq 0x40(%rcx), %rcx
movq %rcx, -0x40(%rbp)
movq 0x10(%rax), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rcx
movq 0x30(%rcx), %rcx
movq %rcx, -0x50(%rbp)
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x77d0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movslq 0x2c(%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x3110
movq -0xa0(%rbp), %rax
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x77d0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movslq 0x2c(%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x3110
movq -0xa0(%rbp), %rax
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x77d0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
movq %rcx, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movq 0x90(%rax), %rdi
movslq 0x2c(%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x3110
movl $0x0, -0x54(%rbp)
movq -0xa0(%rbp), %rcx
movl -0x54(%rbp), %eax
movq 0x20(%rcx), %rcx
cmpl (%rcx), %eax
jge 0xca14
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x20(%rax), %rax
movslq -0x54(%rbp), %rcx
cmpl $0x2, (%rax,%rcx,4)
jne 0xc9f1
movq -0xa0(%rbp), %rcx
movq 0x20(%rcx), %rax
movq 0x10(%rax), %rax
movslq -0x54(%rbp), %rdx
movl (%rax,%rdx,4), %eax
cmpl 0x2c(%rcx), %eax
jge 0xc96a
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xa4(%rbp)
jmp 0xc98b
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xa4(%rbp)
movq -0xa0(%rbp), %rax
movl -0xa4(%rbp), %ecx
movl %ecx, -0x58(%rbp)
movl -0x58(%rbp), %esi
movq -0x78(%rbp), %rcx
movslq -0x80(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movl -0x80(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x80(%rbp)
movq -0x70(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movb $0x1, (%rcx,%rdx)
movq 0x90(%rax), %rax
movslq -0x58(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0xc9f1
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x28, %esi
callq 0x31d0
jmp 0xcd01
jmp 0xc9f3
movq -0xa0(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x20(%rax), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0xc8fe
cmpl $0x0, -0x80(%rbp)
jne 0xca5a
movq -0xa0(%rbp), %rdi
callq 0xe3f0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x78(%rbp), %rax
movslq -0x80(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movb $0x1, (%rax,%rcx)
movl -0x80(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x78(%rbp), %rsi
movslq -0x7c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x78(%rbp), %rdx
movslq -0x80(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdx
callq 0x66b0
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0xcbe9
movq -0xa0(%rbp), %rax
movq -0x78(%rbp), %rcx
movslq -0x7c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x58(%rbp)
movl -0x7c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x7c(%rbp)
movq -0x68(%rbp), %rcx
movslq -0x58(%rbp), %rdx
movb $0x1, (%rcx,%rdx)
movq 0x10(%rax), %rax
movq 0x38(%rax), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x48(%rbp), %rcx
movslq -0x58(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0xcbe4
movq -0x90(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x5c(%rbp)
movq -0x68(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xcbd4
movl -0x58(%rbp), %edx
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x5c(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
movq -0x30(%rbp), %rsi
movslq -0x5c(%rbp), %rdi
addl (%rsi,%rdi,4), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x5c(%rbp), %edx
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x58(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
movq -0x48(%rbp), %rsi
movslq -0x58(%rbp), %rdi
addl (%rsi,%rdi,4), %ecx
subl $0x1, %ecx
movq -0x28(%rbp), %rsi
movslq -0x58(%rbp), %rdi
subl (%rsi,%rdi,4), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x70(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xcbd2
movq -0x70(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movl -0x5c(%rbp), %edx
movq -0x78(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xcbd4
jmp 0xcbd6
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xcae5
jmp 0xca91
movl -0x80(%rbp), %eax
cmpl -0x84(%rbp), %eax
jne 0xcbf6
jmp 0xcc0a
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0xca63
movl $0x0, -0x54(%rbp)
movq -0xa0(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jge 0xcca6
movq -0xa0(%rbp), %rax
movq 0x90(%rax), %rax
movslq -0x54(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xcc96
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xcc96
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
movslq -0x54(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x2, %rax
addq %rax, %rsi
callq 0x10940
jmp 0xcc98
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xcc11
movq -0xa0(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0x68(%rbp), %rsi
movl 0x2c(%rax), %edx
callq 0x7a30
movq -0xa0(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0x70(%rbp), %rsi
movl 0x2c(%rax), %edx
callq 0x7a30
movq -0xa0(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0x78(%rbp), %rsi
movl 0x2c(%rax), %edx
callq 0x7a30
movq -0x10(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x3370
nopw (%rax,%rax)