// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r8, %r15
movq %rcx, %r12
movq %rdx, 0x48(%rsp)
movq %rdi, %rbp
movq (%rdi), %r14
addq $0x18, %r14
movq %rsi, 0x10(%rsp)
movslq 0xc(%rsi), %rbx
leaq (,%rbx,4), %rax
testq %rbx, %rbx
movl %ebx, 0x20(%rdi)
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x18(%rbp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x4aba
movq %rax, 0x8(%rsp)
movl 0x20(%rbp), %esi
movq %r14, %rdi
callq 0x4aba
movq %rax, %r13
movl 0x20(%rbp), %esi
movq %r14, 0x28(%rsp)
movq %r14, %rdi
callq 0x4aba
movl 0x20(%rbp), %edx
testl %edx, %edx
jle 0x53f4
movq 0x18(%rbp), %r8
movq 0x10(%rsp), %r9
movq 0x30(%r9), %rcx
xorl %esi, %esi
xorl %r11d, %r11d
movl (%r12,%rsi,4), %edi
subl %edx, %edi
movl %edi, (%r8,%rsi,4)
cmpb $0x0, (%rcx,%rsi)
jne 0x517d
movq 0x18(%r9), %rdx
movq 0x20(%r9), %rdi
movl (%rdx,%rsi,4), %edx
cmpl (%rdi,%rsi,4), %edx
je 0x517d
movslq %r11d, %r11
movq 0x8(%rsp), %rdx
movl %esi, (%rdx,%r11,4)
incl %r11d
incq %rsi
movslq 0x20(%rbp), %rdx
cmpq %rdx, %rsi
jl 0x514e
testl %r11d, %r11d
jle 0x53f4
leaq 0x40(%r13), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x40(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq %rbp, 0x20(%rsp)
movq %r15, (%rsp)
movq %rax, 0x18(%rsp)
movl %r11d, %edx
movq 0x8(%rsp), %rcx
movslq -0x4(%rcx,%rdx,4), %r10
movq 0x18(%r9), %rcx
movl (%rcx,%r10,4), %edx
movl $0x0, %r15d
testl %edx, %edx
jle 0x5203
movq 0x38(%r9), %rsi
movq (%rsi,%r10,8), %rsi
xorl %edi, %edi
xorl %r15d, %r15d
movslq (%rsi,%rdi,4), %r8
cmpl $-0x1, (%r12,%r8,4)
jne 0x51f8
movslq %r15d, %r15
movl %r8d, (%r13,%r15,4)
incl %r15d
movl (%rcx,%r10,4), %edx
incq %rdi
movslq %edx, %r8
cmpq %r8, %rdi
jl 0x51de
movslq (%r12,%r10,4), %r10
movslq 0x20(%rbp), %rcx
subq %rcx, %r10
movq 0x48(%rsp), %rdx
movq 0x18(%rdx), %rcx
movl (%rcx,%r10,4), %edi
testl %edi, %edi
movq %r11, 0x50(%rsp)
jle 0x525e
movq 0x38(%rdx), %rdx
movq (%rdx,%r10,8), %rdx
xorl %esi, %esi
xorl %r14d, %r14d
movslq (%rdx,%rsi,4), %r9
movslq 0x20(%rbp), %r8
addq %r9, %r8
cmpl $-0x1, (%r12,%r8,4)
jne 0x5251
movslq %r14d, %r14
movl %r8d, (%rax,%r14,4)
incl %r14d
movl (%rcx,%r10,4), %edi
incq %rsi
movslq %edi, %r8
cmpq %r8, %rsi
jl 0x5231
jmp 0x5261
xorl %r14d, %r14d
testl %r15d, %r15d
movq (%rsp), %rax
je 0x52e9
movslq %r15d, %rbx
leaq (,%rbx,4), %rbp
addq %r13, %rbp
bsrq %rbx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rcx
callq 0x5605
cmpl $0x11, %r15d
jl 0x52d4
shlq $0x2, %rbx
movq %r13, %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
movq (%rsp), %rdx
callq 0x5892
addq $-0x40, %rbx
movq (%rsp), %rax
movq %rbp, %rdi
movq %rax, %rsi
callq 0x591e
movq (%rsp), %rax
addq $0x4, %rbp
addq $-0x4, %rbx
jne 0x52b9
jmp 0x52e9
movq %r13, %rdi
movq %rbp, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x5892
movq %rbx, %rax
testl %r14d, %r14d
je 0x5373
movslq %r14d, %rbx
movq 0x18(%rsp), %rbp
leaq (,%rbx,4), %rsi
addq %rbp, %rsi
bsrq %rbx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbp, %rdi
movq %rsi, 0x40(%rsp)
movq %rax, %rcx
callq 0x595d
cmpl $0x11, %r14d
jl 0x5362
shlq $0x2, %rbx
movq %rbp, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq (%rsp), %r14
movq %r14, %rdx
callq 0x5bea
addq $-0x40, %rbx
movq %r14, %rsi
movq %rbp, %rdi
callq 0x5c76
movq (%rsp), %rsi
addq $0x4, %rbp
addq $-0x4, %rbx
jne 0x534a
jmp 0x5373
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq (%rsp), %rdx
callq 0x5bea
movq 0x50(%rsp), %r11
decl %r11d
testl %r15d, %r15d
jle 0x53d9
movq 0x20(%rsp), %rbp
movq 0x18(%rbp), %r10
movl %r11d, %r11d
movl %r15d, %edx
movq 0x8(%rsp), %rcx
leaq (%rcx,%r11,4), %rsi
xorl %ecx, %ecx
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %rax
movslq (%rax,%rcx,4), %rdi
movslq (%r13,%rcx,4), %r8
movl %edi, (%r12,%r8,4)
movslq (%r13,%rcx,4), %r8
movl %r8d, (%r12,%rdi,4)
movl (%rax,%rcx,4), %edi
subl 0x20(%rbp), %edi
movl %edi, (%r10,%r8,4)
movl (%r13,%rcx,4), %edi
movl %edi, (%rsi,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x53a4
addl %ecx, %r11d
jmp 0x53e8
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %rax
testl %r11d, %r11d
jg 0x51b2
movl 0x20(%rbp), %edx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %r14
callq 0x4c64
movl 0x20(%rbp), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4c64
movl 0x20(%rbp), %edx
movq %rbx, %rdi
movq %r14, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c64
nop