// File: /workspace/github_cpp_makefile_star2_O1/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 0x4740
movslq 0x2c(%r14), %r13
leaq (,%r13,4), %r15
movq %r15, 0x10(%rsp)
testq %r13, %r13
movl %r13d, (%rbp)
movl 0x30(%r14), %eax
movl %eax, 0x4(%rbp)
movq $-0x1, %rbx
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, 0x18(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x20(%rbp)
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x20e0
movq %r15, 0x38(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20e0
movq 0x10(%r14), %rax
movq 0xb0(%r14), %rdi
movq 0x40(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x20(%rax), %rbp
movq 0x30(%rax), %rax
movq %rax, 0x20(%rsp)
movl %r13d, %esi
callq 0x4b34
movq %rax, %r13
movslq 0x2c(%r14), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %esi
callq 0x4b34
movq %rax, %r12
movslq 0x2c(%r14), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%r14), %rdi
movl 0x2c(%r14), %esi
callq 0x4b34
movq %rax, 0x8(%rsp)
movq 0x90(%r14), %rdi
movslq 0x2c(%r14), %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x20(%r14), %rdx
cmpl $0x0, (%rdx)
movq %r14, %rdi
movq %r14, (%rsp)
jle 0x6579
xorl %esi, %esi
movb $0x1, %al
xorl %ebx, %ebx
movq 0x20(%rdx), %rdi
movslq %esi, %rcx
cmpl $0x2, (%rdi,%rcx,4)
jne 0x6564
movq 0x10(%rdx), %rsi
movl (%rsi,%rcx,4), %edx
movq (%rsp), %rdi
cmpl 0x2c(%rdi), %edx
jl 0x6544
movl 0x4(%rsi,%rcx,4), %edx
movslq %ebx, %rbx
movq 0x8(%rsp), %rsi
movl %edx, (%rsi,%rbx,4)
incl %ebx
movslq %edx, %rdx
movb %al, (%r12,%rdx)
movq 0x90(%rdi), %rsi
movb %al, (%rsi,%rdx)
jmp 0x6568
movq (%rsp), %rdi
movq 0x20(%rdi), %rdx
movq 0x20(%rdx), %rsi
movl (%rsi,%rcx,4), %esi
addl %ecx, %esi
cmpl (%rdx), %esi
jl 0x6523
testl %ebx, %ebx
jne 0x65a7
movq (%rsp), %rdi
callq 0x77c6
movq (%rsp), %rdi
movq 0x18(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq 0x8(%rsp), %rdx
movl %eax, (%rdx)
movslq 0x8(%rcx), %rax
movb $0x1, (%r12,%rax)
movl $0x1, %ebx
xorl %ecx, %ecx
movq 0x10(%rdi), %rdi
movslq %ecx, %r14
movq 0x8(%rsp), %rax
leaq (%rax,%r14,4), %rsi
movslq %ebx, %r15
leaq (%rax,%r15,4), %rdx
callq 0x4674
movq %r14, %rdx
movq %rbx, 0x28(%rsp)
movl %ebx, %eax
movq %r15, 0x40(%rsp)
cmpl %r15d, %edx
jge 0x66b5
movq 0x28(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rdx, 0x48(%rsp)
movl (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%r13,%rcx)
cmpl $0x0, (%rbp,%rcx,4)
jle 0x66a2
movq (%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 0x6691
movq 0x10(%rsp), %r10
movslq (%r10,%rdi,4), %r8
movq %r12, %r14
movq 0x38(%rsp), %r12
movslq (%r12,%rdi,4), %r9
addq %r9, %r8
movq 0x30(%rsp), %rbx
movl %ecx, (%rbx,%r8,4)
movl (%rbp,%rcx,4), %r8d
addl (%r10,%rcx,4), %r8d
movq 0x50(%rsp), %r15
movl (%r15,%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, (%r15,%rcx,4)
cmpb $0x0, (%r14,%rdi)
jne 0x6691
movb $0x1, (%r12,%rdi)
cltq
movq 0x8(%rsp), %r8
movl %edi, (%r8,%rax,4)
incl %eax
incq %rsi
movslq (%rbp,%rcx,4), %rdi
cmpq %rdi, %rsi
jl 0x6615
movq 0x48(%rsp), %rdx
incq %rdx
cmpq 0x40(%rsp), %rdx
jne 0x65e2
movq 0x28(%rsp), %rdx
movl %edx, %ecx
cmpl %eax, %edx
movl %eax, %ebx
movq (%rsp), %rdi
jne 0x65a9
movl 0x2c(%rdi), %edx
testl %edx, %edx
movq 0x18(%rsp), %rbp
jle 0x674f
xorl %r15d, %r15d
movq 0x20(%rsp), %rcx
movq 0x90(%rdi), %rax
cmpb $0x0, (%rax,%r15)
jne 0x6743
cmpb $0x0, (%rcx,%r15)
jne 0x6743
movq 0x38(%rsp), %rax
movslq (%rax,%r15,4), %rax
testq %rax, %rax
je 0x6743
movq 0x10(%rsp), %rcx
movslq (%rcx,%r15,4), %rcx
movq 0x30(%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 0x8a95
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8b70
movq 0x20(%rsp), %rcx
movq (%rsp), %rdi
incq %r15
movslq 0x2c(%rdi), %rdx
cmpq %rdx, %r15
jl 0x66de
movq %rdi, %rbx
movq 0xb0(%rdi), %rdi
movq %r13, %rsi
callq 0x4cf0
movq 0xb0(%rbx), %rdi
movl 0x2c(%rbx), %edx
movq %r12, %rsi
callq 0x4cf0
movq 0xb0(%rbx), %rdi
movl 0x2c(%rbx), %edx
movq 0x8(%rsp), %rsi
callq 0x4cf0
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
nop