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

	int32_t w = 0;
	int32_t i, adjCand, ci;
	int32_t* adjPos1 = g1->adjPos;
	int32_t* adjPos2 = g2->adjPos;
	int32_t* parentSize = dag->parentSize;
	int32_t* dagArr = dag->dagArr;
	
	//compute the number of mapped parent
	int32_t numMapped = 0;
	int32_t* mappedParent = global_memory->getLLArray(n);
	for(i = 0; i < parentSize[aVertex]; ++i) {
		if( mapping[ dagArr[adjPos1[aVertex] + i] ] != -1 ) {
			mappedParent[numMapped] = i; //keep the index of the parent
			++numMapped;
		}
	}

	//Case 1: there is no mapped parent of aVertex
	if( numMapped == 0 ) {
		w = (coloring->cellSize[ coloring->color[ coloring->inv[aVertex] ] ] >> 1);
	}
	else {
		adjCand = mapping[ dagArr[ adjPos1[aVertex] + mappedParent[0] ] ] - n;
		ci = cs->dagColorIndex[ adjPos1[aVertex] + mappedParent[0] ];

		if( numMapped == 1 ) {
			w = cs->S[ adjPos2[adjCand] + ci ];
		}
		else {
			if( global_mark > INFINITY ) {
				memset(markNode, 0, sizeof(int32_t) * n2);
				global_mark = 0;
			}
			++global_mark;
	
			//Step 1. the first array
			int32_t j, v;
			int32_t* neigh = g2->e[adjCand];
			int32_t p = cs->P[ adjPos2[adjCand] + ci ];
			int32_t s = cs->S[ adjPos2[adjCand] + ci ];
			for(i = p; i < p + s; ++i) {
				markNode[ neigh[i] ] = global_mark;
			}
			++global_mark;

			//Step 2. the second to the second last array
			for(j = 1; j < numMapped - 1; ++j) {
				adjCand = mapping[ dagArr[ adjPos1[aVertex] + mappedParent[j] ] ] - n;
				neigh = g2->e[adjCand];
				ci = cs->dagColorIndex[ adjPos1[aVertex] + mappedParent[j] ];
				p = cs->P[ adjPos2[adjCand] + ci ];
				s = cs->S[ adjPos2[adjCand] + ci ];
				for(i = p; i < p + s; ++i) {
					v = neigh[i];
					if( markNode[v] == global_mark - 1 )
						++(markNode[v]);
				}
				++global_mark;
			}

			//Step 3. the last array
			w = 0;
			adjCand = mapping[ dagArr[ adjPos1[aVertex] + mappedParent[numMapped - 1] ] ] - n;
			neigh = g2->e[adjCand];
			ci = cs->dagColorIndex[ adjPos1[aVertex] + mappedParent[numMapped - 1] ];
			p = cs->P[ adjPos2[adjCand] + ci ];
			s = cs->S[ adjPos2[adjCand] + ci ];
			for(i = p; i < p + s; ++i) {
				v = neigh[i];
				if( markNode[v] == global_mark - 1 )
					++w;
			}
		} //else of (if numMapped == 1)
	} //else of (if numMapped == 0)

	global_memory->returnLLArray(mappedParent, n);

	return w;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x10(%rbp)
movq 0x10(%rax), %rcx
movq 0x40(%rcx), %rcx
movq %rcx, -0x28(%rbp)
movq 0x18(%rax), %rcx
movq 0x40(%rcx), %rcx
movq %rcx, -0x30(%rbp)
movq 0x40(%rax), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x38(%rbp)
movq 0x40(%rax), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq 0xb0(%rax), %rdi
movl 0x2c(%rax), %esi
callq 0x77d0
movq %rax, -0x50(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x38(%rbp), %rcx
movslq -0xc(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0xe76a
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq -0xc(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
addl -0x14(%rbp), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0xe75d
movl -0x14(%rbp), %edx
movq -0x50(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xe75f
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xe70f
cmpl $0x0, -0x44(%rbp)
jne 0xe7a5
movq -0x70(%rbp), %rdx
movq 0x20(%rdx), %rax
movq 0x20(%rax), %rax
movq 0x20(%rdx), %rcx
movq 0x8(%rcx), %rcx
movq 0x20(%rdx), %rdx
movq 0x18(%rdx), %rdx
movslq -0xc(%rbp), %rsi
movslq (%rdx,%rsi,4), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
sarl %eax
movl %eax, -0x10(%rbp)
jmp 0xeb73
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rsi
movslq -0xc(%rbp), %rdi
movl (%rsi,%rdi,4), %esi
movq -0x50(%rbp), %rdi
addl (%rdi), %esi
movslq %esi, %rsi
movslq (%rdx,%rsi,4), %rdx
movl (%rcx,%rdx,4), %ecx
subl 0x2c(%rax), %ecx
movl %ecx, -0x18(%rbp)
movq 0x48(%rax), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movslq -0xc(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq -0x50(%rbp), %rdx
addl (%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x1, -0x44(%rbp)
jne 0xe822
movq -0x70(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x10(%rbp)
jmp 0xeb71
movq -0x70(%rbp), %rax
cmpl $0x77359400, 0xc8(%rax) # imm = 0x77359400
jle 0xe85a
movq -0x70(%rbp), %rax
movq 0xb8(%rax), %rdi
movslq 0x34(%rax), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x3110
movq -0x70(%rbp), %rax
movl $0x0, 0xc8(%rax)
movq -0x70(%rbp), %rax
movl 0xc8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc8(%rax)
movq 0x18(%rax), %rcx
movq 0x38(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x60(%rbp)
movq 0x48(%rax), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
movslq -0x18(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
addl -0x1c(%rbp), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x64(%rbp)
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x64(%rbp), %ecx
addl -0x68(%rbp), %ecx
cmpl %ecx, %eax
jge 0xe8fd
movq -0x70(%rbp), %rax
movl 0xc8(%rax), %edx
movq 0xb8(%rax), %rax
movq -0x60(%rbp), %rcx
movslq -0x14(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xe8c5
movq -0x70(%rbp), %rax
movl 0xc8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc8(%rax)
movl $0x1, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xea5a
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rsi
movslq -0xc(%rbp), %rdi
movl (%rsi,%rdi,4), %esi
movq -0x50(%rbp), %rdi
movslq -0x54(%rbp), %r8
addl (%rdi,%r8,4), %esi
movslq %esi, %rsi
movslq (%rdx,%rsi,4), %rdx
movl (%rcx,%rdx,4), %ecx
subl 0x2c(%rax), %ecx
movl %ecx, -0x18(%rbp)
movq 0x18(%rax), %rcx
movq 0x38(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x60(%rbp)
movq 0x48(%rax), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movslq -0xc(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq -0x50(%rbp), %rsi
movslq -0x54(%rbp), %rdi
addl (%rsi,%rdi,4), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x1c(%rbp)
movq 0x48(%rax), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
movslq -0x18(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
addl -0x1c(%rbp), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x64(%rbp)
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x64(%rbp), %ecx
addl -0x68(%rbp), %ecx
cmpl %ecx, %eax
jge 0xea39
movq -0x70(%rbp), %rcx
movq -0x60(%rbp), %rax
movslq -0x14(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movl %eax, -0x58(%rbp)
movq 0xb8(%rcx), %rax
movslq -0x58(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movl 0xc8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xea2c
movq -0x70(%rbp), %rax
movq 0xb8(%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0xea2e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xe9da
movq -0x70(%rbp), %rax
movl 0xc8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc8(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xe917
movq -0x70(%rbp), %rax
movl $0x0, -0x10(%rbp)
movq 0x50(%rax), %rcx
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rsi
movslq -0xc(%rbp), %rdi
movl (%rsi,%rdi,4), %esi
movq -0x50(%rbp), %rdi
movl -0x44(%rbp), %r8d
subl $0x1, %r8d
movslq %r8d, %r8
addl (%rdi,%r8,4), %esi
movslq %esi, %rsi
movslq (%rdx,%rsi,4), %rdx
movl (%rcx,%rdx,4), %ecx
subl 0x2c(%rax), %ecx
movl %ecx, -0x18(%rbp)
movq 0x18(%rax), %rcx
movq 0x38(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x60(%rbp)
movq 0x48(%rax), %rcx
movq 0x18(%rcx), %rcx
movq -0x28(%rbp), %rdx
movslq -0xc(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq -0x50(%rbp), %rsi
movl -0x44(%rbp), %edi
subl $0x1, %edi
movslq %edi, %rdi
addl (%rsi,%rdi,4), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x1c(%rbp)
movq 0x48(%rax), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
movslq -0x18(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
addl -0x1c(%rbp), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x64(%rbp)
movq 0x48(%rax), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x64(%rbp), %ecx
addl -0x68(%rbp), %ecx
cmpl %ecx, %eax
jge 0xeb6f
movq -0x70(%rbp), %rcx
movq -0x60(%rbp), %rax
movslq -0x14(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movl %eax, -0x58(%rbp)
movq 0xb8(%rcx), %rax
movslq -0x58(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movl 0xc8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xeb62
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xeb64
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xeb1f
jmp 0xeb71
jmp 0xeb73
movq -0x70(%rbp), %rax
movq 0xb0(%rax), %rdi
movq -0x50(%rbp), %rsi
movl 0x2c(%rax), %edx
callq 0x7a30
movl -0x10(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)