// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rdi, %r12
pushq $0x28
popq %rdi
callq 0x2180
movq %rax, %r14
movq %rax, %rdi
callq 0x3b86
movq 0x2c(%r12), %rax
movq %rax, (%r14)
movslq %eax, %rbp
leaq (,%rbp,4), %rbx
testl %ebp, %ebp
pushq $-0x1
popq %rax
movq %rbx, %r15
cmovsq %rax, %r15
movq %r15, %rdi
callq 0x2030
movq %r12, 0x8(%rsp)
movq %rax, %r13
movq %rax, 0x10(%r14)
movq %r15, %rdi
callq 0x2030
movq %rax, %r15
movq %rax, 0x18(%r14)
movslq 0x38(%r12), %rax
leaq (,%rax,4), %rdi
testq %rax, %rax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %r12
movq %r14, 0x30(%rsp)
movq %rax, 0x20(%r14)
movl $0x0, 0x14(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2100
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2100
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0xb0(%rcx), %rdi
movq 0x40(%rax), %r14
movq 0x20(%rax), %rbx
movq 0x30(%rax), %rax
movq %rax, 0x38(%rsp)
movl %ebp, %esi
callq 0x3f1a
movq %rax, %rbp
movq 0x8(%rsp), %rax
movslq 0x2c(%rax), %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x3f1a
movq 0x8(%rsp), %rcx
movslq 0x2c(%rcx), %rdx
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x3f1a
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rdi
movslq 0x2c(%rax), %rdx
xorl %esi, %esi
callq 0x2100
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rdx
movb $0x1, %al
xorl %ecx, %ecx
cmpl (%rdx), %ecx
jge 0x571a
movq 0x20(%rdx), %rdi
movslq %ecx, %rsi
movl (%rdi,%rsi,4), %edi
cmpl $0x2, %edi
jne 0x5716
movq 0x10(%rdx), %rdi
movl (%rdi,%rsi,4), %edx
movq 0x8(%rsp), %r8
cmpl 0x2c(%r8), %edx
jl 0x56d8
movl 0x4(%rdi,%rsi,4), %edx
movl 0x14(%rsp), %r9d
movslq %r9d, %rdi
movq 0x18(%rsp), %r8
movl %edx, (%r8,%rdi,4)
incl %r9d
movl %r9d, 0x14(%rsp)
movslq %edx, %rdx
movq 0x20(%rsp), %rdi
movb %al, (%rdi,%rdx)
movq 0x8(%rsp), %r8
movq 0x90(%r8), %rdi
movb %al, (%rdi,%rdx)
movq 0x20(%r8), %rdx
movq 0x20(%rdx), %rdi
movl (%rdi,%rsi,4), %edi
addl %edi, %ecx
jmp 0x56af
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0x574b
movq 0x8(%rsp), %rdi
callq 0x665c
movq 0x30(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq 0x18(%rsp), %rdx
movl %eax, (%rdx)
movslq 0x8(%rcx), %rax
movq 0x20(%rsp), %rcx
movb $0x1, (%rcx,%rax)
pushq $0x1
popq %rax
xorl %ecx, %ecx
movl %eax, %edx
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movslq %ecx, %rcx
movq %rcx, 0x28(%rsp)
movq 0x18(%rsp), %rax
leaq (%rax,%rcx,4), %rsi
movslq %edx, %rcx
movq %rcx, 0x40(%rsp)
leaq (%rax,%rcx,4), %rdx
callq 0x3aea
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %r8
movl 0x14(%rsp), %eax
cmpq 0x40(%rsp), %rdx
jge 0x5841
movq 0x18(%rsp), %rcx
movq %rdx, 0x28(%rsp)
movl (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movb $0x1, (%rbp,%rcx)
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
movq 0x38(%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
xorl %esi, %esi
movslq (%rbx,%rcx,4), %rdi
cmpq %rdi, %rsi
jge 0x5834
movl (%rdx,%rsi,4), %edi
movslq %edi, %rdi
cmpb $0x0, (%rbp,%rdi)
jne 0x582f
movslq (%r14,%rdi,4), %r8
movslq (%r15,%rdi,4), %r9
addq %r9, %r8
movl %ecx, (%r12,%r8,4)
movl (%rbx,%rcx,4), %r8d
addl (%r14,%rcx,4), %r8d
movl (%r13,%rcx,4), %r10d
movl %r10d, %r11d
notl %r11d
addl %r8d, %r11d
movslq %r11d, %r8
movl %edi, (%r12,%r8,4)
leal 0x1(%r9), %r8d
movl %r8d, (%r15,%rdi,4)
movq 0x20(%rsp), %r8
incl %r10d
movl %r10d, (%r13,%rcx,4)
cmpb $0x0, (%r8,%rdi)
jne 0x582f
movb $0x1, (%r8,%rdi)
cltq
movq 0x18(%rsp), %r9
movl %edi, (%r9,%rax,4)
incl %eax
incq %rsi
jmp 0x57bf
movq 0x28(%rsp), %rdx
incq %rdx
jmp 0x578c
movl 0x14(%rsp), %edx
movl %edx, %ecx
cmpl %eax, %edx
jne 0x574d
xorl %ebx, %ebx
movq 0x8(%rsp), %r13
movq 0x38(%rsp), %rcx
movslq 0x2c(%r13), %rdx
cmpq %rdx, %rbx
jge 0x589b
movq 0x90(%r13), %rax
cmpb $0x0, (%rax,%rbx)
jne 0x5896
cmpb $0x0, (%rcx,%rbx)
jne 0x5896
movslq (%r14,%rbx,4), %rax
leaq (%r12,%rax,4), %rdi
movslq (%r15,%rbx,4), %rax
leaq (%rdi,%rax,4), %rsi
callq 0x7839
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %rcx
incq %rbx
jmp 0x585b
movq 0xb0(%r13), %rdi
movq %rbp, %rsi
movq %r8, %rbx
callq 0x3fee
movq 0xb0(%r13), %rdi
movl 0x2c(%r13), %edx
movq %rbx, %rsi
callq 0x3fee
movq 0xb0(%r13), %rdi
movl 0x2c(%r13), %edx
movq 0x18(%rsp), %rsi
callq 0x3fee
movq 0x30(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22e0
nop