// File: /workspace/github_cpp_makefile_star2_O2/SNUCSE-CTA[P]GI/src/algorithm.cpp
// Function: Algorithm::computeIso(Graph*, Graph*, int*, Coloring*)
void Algorithm::computeIso(Graph* aG1, Graph* aG2, int32_t* aMapping, Coloring* aColoring)
{
	Memory* global_memory = &(cont.global_memory);
	numNode = aG1->numNode;
	mapping = new int32_t[numNode];
	int32_t* stack = global_memory->getLLArray(numNode);
	int32_t stackSize = 0;
	int32_t* array1 = global_memory->getLLArray(numNode);
	int32_t* array2 = global_memory->getLLArray(numNode);
	int32_t ind1 = 0;
	int32_t ind2 = 0;
	for(int32_t vid = 0; vid < numNode; ++vid) {
		mapping[vid] = aMapping[vid] - numNode;

		if(aG1->one[vid] == 0 && aG1->od[vid] != aG1->d[vid]) { //root
			stack[stackSize] = vid;
			++stackSize;
		}
	}

	//process coreness-1 nodes
	while(stackSize > 0) {
		int32_t r = stack[stackSize - 1];
		--stackSize;
		
		ind1 = 0;
		ind2 = 0;
		for(int32_t i = 0; i < aG1->od[r]; ++i) {
			if(aMapping[ aG1->e[r][i] ] == -1 ) { //unmapped neighbors of r
				array1[ind1] = aG1->e[r][i];
				++ind1;
			}
		}
	
		int32_t mr = aMapping[r] - numNode;
		for(int32_t i = 0; i < aG2->od[mr]; ++i) { //unmapped neighbors of mr
			if(aMapping[ aG2->e[mr][i] + numNode ] == -1) {
				array2[ind2] = aG2->e[mr][i] + numNode;
				++ind2;
			}
		}
			
		//sort array1 and array2 by color
		sort(array1, array1 + ind1,
			[aColoring](const int32_t& a, const int32_t& b) -> bool {
				int32_t ca = aColoring->color[ aColoring->inv[a] ];
				int32_t cb = aColoring->color[ aColoring->inv[b] ];
				return ca < cb;
			}
		);
		sort(array2, array2 + ind2,
			[aColoring](const int32_t& a, const int32_t& b) -> bool {
				int32_t ca = aColoring->color[ aColoring->inv[a] ];
				int32_t cb = aColoring->color[ aColoring->inv[b] ];
				return ca < cb;
			}
		);

		for(int32_t i = 0; i < ind1; ++i) {
			aMapping[array1[i]] = array2[i]; //to check isMapped
			aMapping[array2[i]] = array1[i];
			mapping[array1[i]] = array2[i] - numNode; //output

			stack[stackSize] = array1[i];
			++stackSize;
		}
	}

	global_memory->returnLLArray(stack, numNode);
	global_memory->returnLLArray(array1, numNode);
	global_memory->returnLLArray(array2, numNode);

	//ASSERT
//	int32_t* check = global_memory->getLLArray(numNode);
//	memset(check, 0, sizeof(int32_t) * numNode);
//	for(int32_t vid = 0; vid < aG1->numNode; ++vid) {
//		if( aG1->l[vid] != aG2->l[mapping[vid]] )
//			cout << "ISOMORPHISM ERROR: label not same" << endl;
//		if( aG1->od[vid] != aG2->od[mapping[vid]] )
//			cout << "ISOMORPHISM ERROR: degree not same" << endl;
//		for(int32_t i = 0; i < aG1->od[vid]; ++i) {
//			int32_t adj = aG1->e[vid][i];
//			check[mapping[adj]] = 1;
//		}
//		for(int32_t i = 0; i < aG2->od[mapping[vid]]; ++i) {
//			int32_t adj = aG2->e[mapping[vid]][i];
//			if( check[adj] == 0 )
//				cout << "ISOMORPHISM ERROR: not isomorphic" << endl;
//		}
//	}
//	global_memory->returnLLArray(check, numNode);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, 0x28(%rsp)
movq %rsi, %r13
movq %rdi, %r15
movq (%rdi), %r14
addq $0x18, %r14
movslq 0xc(%rsi), %rbx
leaq (,%rbx,4), %rax
testq %rbx, %rbx
movl %ebx, 0x20(%rdi)
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x18(%r15)
movq %r14, %rdi
movl %ebx, %esi
callq 0x3f1a
movq %rax, %rbx
movl 0x20(%r15), %esi
movq %r14, %rdi
callq 0x3f1a
movq %rax, 0x18(%rsp)
movl 0x20(%r15), %esi
movq %r14, 0x38(%rsp)
movq %r14, %rdi
callq 0x3f1a
movq %r13, %r10
movq %r15, %r8
movq 0x18(%r15), %rdi
xorl %ecx, %ecx
xorl %r11d, %r11d
movslq 0x20(%r8), %rdx
cmpq %rdx, %rcx
jge 0x4627
movl (%r12,%rcx,4), %esi
subl %edx, %esi
movl %esi, (%rdi,%rcx,4)
movq 0x30(%r10), %rdx
cmpb $0x0, (%rdx,%rcx)
jne 0x4622
movq 0x18(%r10), %rdx
movq 0x20(%r10), %rsi
movl (%rdx,%rcx,4), %edx
cmpl (%rsi,%rcx,4), %edx
je 0x4622
movslq %r11d, %r11
movl %ecx, (%rbx,%r11,4)
incl %r11d
incq %rcx
jmp 0x45ec
movq 0x18(%rsp), %r15
leaq 0x40(%r15), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x40(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x28(%rsp), %r9
movq %r8, 0x58(%rsp)
movq %r10, 0x50(%rsp)
movq %rbx, 0x48(%rsp)
movq %rax, 0x30(%rsp)
testl %r11d, %r11d
jle 0x4856
movl %r11d, %ecx
movslq -0x4(%rbx,%rcx,4), %rdi
movq 0x18(%r10), %rcx
xorl %edx, %edx
xorl %ebp, %ebp
movslq (%rcx,%rdi,4), %rsi
movslq %ebp, %r13
cmpq %rsi, %rdx
jge 0x469a
movq 0x38(%r10), %rsi
movq (%rsi,%rdi,8), %rsi
movslq (%rsi,%rdx,4), %rsi
cmpl $-0x1, (%r12,%rsi,4)
jne 0x4695
movl %esi, (%r15,%r13,4)
incl %ebp
incq %rdx
jmp 0x4670
movq %r11, 0x60(%rsp)
movslq (%r12,%rdi,4), %r11
movslq 0x20(%r8), %rcx
subq %rcx, %r11
movq 0x18(%r9), %rcx
xorl %edx, %edx
xorl %r14d, %r14d
movslq (%rcx,%r11,4), %rsi
movslq %r14d, %r15
cmpq %rsi, %rdx
jge 0x46e5
movq 0x38(%r9), %rsi
movq (%rsi,%r11,8), %rsi
movslq (%rsi,%rdx,4), %rdi
movslq 0x20(%r8), %rsi
addq %rdi, %rsi
cmpl $-0x1, (%r12,%rsi,4)
jne 0x46e0
movl %esi, (%rax,%r15,4)
incl %r14d
incq %rdx
jmp 0x46b3
testl %ebp, %ebp
je 0x4760
movq 0x18(%rsp), %rbx
leaq (%rbx,%r13,4), %rsi
bsrq %r13, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %rsi, 0x10(%rsp)
movq 0x8(%rsp), %rcx
callq 0x4996
movq %rbx, %rdi
cmpl $0x11, %ebp
jl 0x4751
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x4bfb
leaq -0x40(,%r13,4), %rbx
movq 0x20(%rsp), %r13
testq %rbx, %rbx
je 0x4760
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x4c7e
addq $0x4, %r13
addq $-0x4, %rbx
jmp 0x4735
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x4bfb
testl %r14d, %r14d
je 0x47e1
movq 0x30(%rsp), %r13
leaq (,%r15,4), %rsi
addq %r13, %rsi
bsrq %r15, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rsi, 0x10(%rsp)
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x4cad
movq %r13, %rdi
cmpl $0x11, %r14d
jl 0x47d4
movq 0x40(%rsp), %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4f12
leaq -0x40(,%r15,4), %rbx
testq %rbx, %rbx
je 0x47e1
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x4f95
addq $0x4, %r13
addq $-0x4, %rbx
jmp 0x47b8
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x4f12
movq 0x60(%rsp), %r11
decl %r11d
movq 0x58(%rsp), %r8
movq 0x18(%r8), %r9
movl %r11d, %r11d
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
movq 0x48(%rsp), %rbx
leaq (%rbx,%r11,4), %rdx
xorl %ecx, %ecx
movq 0x50(%rsp), %r10
movq 0x18(%rsp), %r15
movq 0x30(%rsp), %rax
cmpq %rcx, %rbp
je 0x4849
movslq (%rax,%rcx,4), %rsi
movslq (%r15,%rcx,4), %rdi
movl %esi, (%r12,%rdi,4)
movslq (%r15,%rcx,4), %rdi
movl %edi, (%r12,%rsi,4)
movl (%rax,%rcx,4), %esi
subl 0x20(%r8), %esi
movl %esi, (%r9,%rdi,4)
movl (%r15,%rcx,4), %esi
movl %esi, (%rdx,%rcx,4)
incq %rcx
jmp 0x4819
addl %ecx, %r11d
movq 0x28(%rsp), %r9
jmp 0x4657
movl 0x20(%r8), %edx
movq %rbx, %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r8, %r14
movq %rax, %r12
callq 0x3fee
movl 0x20(%r14), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3fee
movl 0x20(%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3fee