// File: /workspace/github_cpp_makefile_star2_O2/SNUCSE-CTA[P]GI/src/refine.cpp
// Function: Refinement::colorByDegreeAndLabel(Coloring*, Graph*, Graph*)
bool Refinement::colorByDegreeAndLabel(Coloring* coloring, Graph* aG1, Graph* aG2)
{
	#ifdef DEBUG
	cout << __PRETTY_FUNCTION__ << endl;
	#endif

	sort(coloring->perm, coloring->perm + coloring->numNode,
			[aG1, aG2](const int32_t& i, const int32_t& j) -> bool {
				const int32_t di = (i < aG1->numNode) ? aG1->d[i] : aG2->d[i - aG1->numNode];
				const int32_t li = (i < aG1->numNode) ? aG1->l[i] : aG2->l[i - aG1->numNode];

				const int32_t dj = (j < aG1->numNode) ? aG1->d[j] : aG2->d[j - aG1->numNode];
				const int32_t lj = (j < aG1->numNode) ? aG1->l[j] : aG2->l[j - aG1->numNode];

				return (di == dj && li < lj) || di < dj;
			}
		);

	for (int32_t i = 0; i < coloring->numNode; ++i)
		coloring->inv[coloring->perm[i]] = i;

	int32_t acc = (coloring->perm[0] < aG1->numNode) ? 1 : -1;

	coloring->numCell = 1;
	coloring->color[0] = 0;
	coloring->cellSize[0] = 1;
	for (int32_t i = 1; i < coloring->numNode; ++i) {
		const int32_t u = coloring->perm[i];
		const int32_t v = coloring->perm[i-1];

		const int32_t du = (u < aG1->numNode) ? aG1->d[u] : aG2->d[u - aG1->numNode];
		const int32_t lu = (u < aG1->numNode) ? aG1->l[u] : aG2->l[u - aG1->numNode];

		const int32_t dv = (v < aG1->numNode) ? aG1->d[v] : aG2->d[v - aG1->numNode];
		const int32_t lv = (v < aG1->numNode) ? aG1->l[v] : aG2->l[v - aG1->numNode];

		if (du == dv && lu == lv) {
			acc += (u < aG1->numNode) ? 1 : -1;

			coloring->color[i] = coloring->color[i-1];
			++coloring->cellSize[coloring->color[i-1]];
		} else {
			if (acc != 0)
				return false;

			acc = (u < aG1->numNode) ? 1 : -1;

			++coloring->numCell;
			coloring->color[i] = i;
			coloring->cellSize[i] = 1;
		}
	}

	return (acc == 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r15
movslq (%rsi), %rbp
testq %rbp, %rbp
je 0x8571
movq %r13, %rbx
movq 0x10(%r15), %r13
leaq (,%rbp,4), %r12
addq %r13, %r12
bsrq %rbp, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rcx
movq %r14, %r8
callq 0x9af6
cmpl $0x11, %ebp
jl 0x855d
leaq 0x40(%r13), %r12
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x9ea5
movq %rbx, %r13
leaq -0x40(,%rbp,4), %rbx
testq %rbx, %rbx
je 0x8571
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x9f3f
addq $0x4, %r12
addq $-0x4, %rbx
jmp 0x8540
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x9ea5
movq %rbx, %r13
xorl %eax, %eax
movslq (%r15), %rcx
cmpq %rcx, %rax
jge 0x858f
movq 0x10(%r15), %rcx
movq 0x18(%r15), %rdx
movslq (%rcx,%rax,4), %rcx
movl %eax, (%rdx,%rcx,4)
incq %rax
jmp 0x8573
movq 0x10(%r15), %rax
movl (%rax), %ecx
xorl %edx, %edx
cmpl 0xc(%r13), %ecx
movq 0x8(%r15), %rcx
setl %dl
leal -0x1(,%rdx,2), %edi
pushq $0x1
popq %rdx
movl %edx, 0x4(%r15)
movq %rcx, 0x8(%rsp)
andl $0x0, (%rcx)
movq 0x20(%r15), %rcx
movq %rcx, (%rsp)
movl %edx, (%rcx)
leaq 0x20(%r14), %r10
addq $0x28, %r14
movq %r14, 0x18(%rsp)
leaq 0x20(%r13), %r8
movq %r13, 0x20(%rsp)
leaq 0x28(%r13), %rcx
movq %rcx, 0x10(%rsp)
pushq $0x1
popq %r9
movslq (%r15), %r12
cmpq %r12, %r9
jge 0x86c8
movl -0x4(%rax,%r9,4), %ecx
movl (%rax,%r9,4), %r11d
movq 0x20(%rsp), %rdx
movl 0xc(%rdx), %r13d
cmpl %r13d, %r11d
movl %r13d, %edx
movl $0x0, %esi
cmovll %esi, %edx
movq %r10, %r14
cmovlq %r8, %r14
movl %r11d, %ebp
subl %edx, %ebp
movq (%r14), %rdx
movslq %ebp, %r14
movl (%rdx,%r14,4), %ebp
cmpl %r13d, %ecx
movl %r13d, %edx
cmovll %esi, %edx
movq %r10, %rsi
cmovlq %r8, %rsi
movl %ecx, %ebx
subl %edx, %ebx
movq (%rsi), %rsi
movslq %ebx, %rdx
cmpl (%rsi,%rdx,4), %ebp
jne 0x8694
cmpl %r13d, %ecx
movq 0x18(%rsp), %rsi
movq %rsi, %rcx
movq 0x10(%rsp), %rbx
cmovlq %rbx, %rcx
cmpl %r13d, %r11d
cmovlq %rbx, %rsi
movq (%rsi), %rsi
movl (%rsi,%r14,4), %esi
movq (%rcx), %rcx
cmpl (%rcx,%rdx,4), %esi
jne 0x8694
xorl %ecx, %ecx
cmpl %r13d, %r11d
setl %cl
leal (%rdi,%rcx,2), %edi
decl %edi
movq 0x8(%rsp), %rdx
movslq -0x4(%rdx,%r9,4), %rcx
movl %ecx, (%rdx,%r9,4)
movq (%rsp), %rdx
incl (%rdx,%rcx,4)
jmp 0x86c0
testl %edi, %edi
jne 0x86c8
xorl %ecx, %ecx
cmpl %r13d, %r11d
setl %cl
incl 0x4(%r15)
leal -0x1(,%rcx,2), %edi
movq 0x8(%rsp), %rcx
movl %r9d, (%rcx,%r9,4)
movq (%rsp), %rcx
movl $0x1, (%rcx,%r9,4)
incq %r9
jmp 0x85e5
cmpq %r12, %r9
setge %cl
testl %edi, %edi
sete %al
andb %cl, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq