// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
movl $0x28, %edi
callq 0x2180
movq %rax, %rbp
movq %rax, %rdi
callq 0x46c6
movq 0x2c(%r14), %rax
movq %rax, (%rbp)
cltq
movq %rax, (%rsp)
leaq (,%rax,4), %r13
testl %eax, %eax
movq $-0x1, %rbx
movq %r13, %r15
cmovsq %rbx, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %rax, 0x10(%rbp)
movq %r15, %rdi
callq 0x2030
movq %rax, %r15
movq %rax, 0x18(%rbp)
movslq 0x38(%r14), %rax
leaq (,%rax,4), %rdi
testq %rax, %rax
cmovsq %rbx, %rdi
callq 0x2030
movq %rbp, 0x10(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rbp)
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20e0
movq %r15, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20e0
movq 0x10(%r14), %rax
movq 0xb0(%r14), %rdi
movq 0x40(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x20(%rax), %rbp
movq 0x30(%rax), %rax
movq %rax, 0x18(%rsp)
movq (%rsp), %rsi
callq 0x4aba
movq %rax, %r13
movslq 0x2c(%r14), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %esi
callq 0x4aba
movq %rax, %r12
movslq 0x2c(%r14), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %esi
callq 0x4aba
movq %rax, (%rsp)
movq 0x90(%r14), %rdi
movslq 0x2c(%r14), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%r14), %rcx
movl (%rcx), %edx
testl %edx, %edx
movq %r14, 0x8(%rsp)
jle 0x64ce
movq 0x20(%rcx), %rsi
movb $0x1, %al
xorl %r9d, %r9d
movslq %ebx, %rdi
movl (%rsi,%rdi,4), %r8d
cmpl $0x2, %r8d
jne 0x64bb
movq 0x10(%rcx), %rdx
movl (%rdx,%rdi,4), %ecx
movq 0x8(%rsp), %rsi
cmpl 0x2c(%rsi), %ecx
jl 0x648e
movl 0x4(%rdx,%rdi,4), %ecx
movslq %r9d, %r9
movq (%rsp), %rdx
movl %ecx, (%rdx,%r9,4)
incl %r9d
movslq %ecx, %rcx
movb %al, (%r12,%rcx)
movq 0x90(%rsi), %rdx
movb %al, (%rdx,%rcx)
movq 0x20(%rsi), %rcx
movl (%rcx), %edx
movq 0x20(%rcx), %rsi
movl (%rsi,%rdi,4), %r8d
addl %r8d, %ebx
cmpl %edx, %ebx
jl 0x646c
movq %r9, %rax
testl %eax, %eax
movq 0x8(%rsp), %r14
jne 0x64f7
movq %r14, %rdi
callq 0x76be
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rdx
movl %eax, (%rdx)
movslq 0x8(%rcx), %rax
movb $0x1, (%r12,%rax)
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x10(%r14), %rdi
movslq %ecx, %r14
movq %rax, %rbx
movq (%rsp), %rax
leaq (%rax,%r14,4), %rsi
movslq %ebx, %r15
leaq (%rax,%r15,4), %rdx
callq 0x45fa
movq %r14, %rdx
movq %rbx, 0x38(%rsp)
movl %ebx, %eax
movq %r15, 0x40(%rsp)
cmpl %r15d, %edx
jge 0x6609
movq (%rsp), %rcx
movq %rdx, 0x48(%rsp)
movl (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%r13,%rcx)
cmpl $0x0, (%rbp,%rcx,4)
jle 0x65f6
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
movq 0x38(%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movslq %edi, %rdi
cmpb $0x0, (%r13,%rdi)
jne 0x65e5
movq %r13, %r15
movq 0x20(%rsp), %r10
movslq (%r10,%rdi,4), %r8
movq %r12, %r14
movq 0x30(%rsp), %r12
movslq (%r12,%rdi,4), %r9
addq %r9, %r8
movq 0x28(%rsp), %rbx
movl %ecx, (%rbx,%r8,4)
movl (%rbp,%rcx,4), %r8d
addl (%r10,%rcx,4), %r8d
movq 0x50(%rsp), %r13
movl (%r13,%rcx,4), %r10d
movl %r10d, %r11d
notl %r11d
addl %r8d, %r11d
movslq %r11d, %r8
movl %edi, (%rbx,%r8,4)
leal 0x1(%r9), %r8d
movl %r8d, (%r12,%rdi,4)
movq %r14, %r12
incl %r10d
movl %r10d, (%r13,%rcx,4)
cmpb $0x0, (%r14,%rdi)
jne 0x65e2
movb $0x1, (%r12,%rdi)
cltq
movq (%rsp), %r8
movl %edi, (%r8,%rax,4)
incl %eax
movq %r15, %r13
incq %rsi
movslq (%rbp,%rcx,4), %rdi
cmpq %rdi, %rsi
jl 0x6562
movq 0x48(%rsp), %rdx
incq %rdx
cmpq 0x40(%rsp), %rdx
jne 0x652f
movq 0x38(%rsp), %rdx
movl %edx, %ecx
cmpl %eax, %edx
movq 0x8(%rsp), %r14
jne 0x64f9
movl 0x2c(%r14), %edx
testl %edx, %edx
jle 0x66ad
xorl %r15d, %r15d
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rcx
movq 0x90(%r14), %rax
cmpb $0x0, (%rax,%r15)
jne 0x66a0
cmpb $0x0, (%rcx,%r15)
jne 0x66a0
movq 0x30(%rsp), %rax
movslq (%rax,%r15,4), %rax
testq %rax, %rax
je 0x66a0
movq 0x20(%rsp), %rcx
movslq (%rcx,%r15,4), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,4), %r14
leaq (%r14,%rax,4), %rbx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x89b7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8a92
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %r14
movl 0x2c(%r14), %edx
incq %r15
movslq %edx, %rax
cmpq %rax, %r15
jl 0x6636
jmp 0x66b2
movq 0x10(%rsp), %rbp
movq 0xb0(%r14), %rdi
movq %r13, %rsi
callq 0x4c64
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %edx
movq %r12, %rsi
callq 0x4c64
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %edx
movq (%rsp), %rsi
callq 0x4c64
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x28, %esi
movq %rbp, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22f0