// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdx, %r14
movq %rsi, %r15
movslq (%rsi), %rbp
testq %rbp, %rbp
je 0x95c2
movq 0x10(%r15), %r12
leaq (%r12,%rbp,4), %r13
bsrq %rbp, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rcx
movq %rbx, %r8
callq 0xad30
cmpl $0x11, %ebp
jl 0x95c8
shlq $0x2, %rbp
leaq 0x40(%r12), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb185
addq $-0x40, %rbp
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xb20e
addq $0x4, %r13
addq $-0x4, %rbp
jne 0x95a8
jmp 0x95d9
movq 0x10(%r15), %rcx
jmp 0x95fb
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb185
cmpl $0x0, (%r15)
movq 0x10(%r15), %rcx
jle 0x95fb
movq 0x18(%r15), %rax
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
movl %edx, (%rax,%rsi,4)
incq %rdx
movslq (%r15), %rsi
cmpq %rsi, %rdx
jl 0x95e9
movl (%rcx), %esi
movl 0xc(%r14), %edi
movl $0x1, %eax
movl %eax, 0x4(%r15)
movq 0x8(%r15), %rdx
movq %rdx, 0x8(%rsp)
movl $0x0, (%rdx)
movq 0x20(%r15), %rdx
movq %rdx, (%rsp)
movl %eax, (%rdx)
xorl %eax, %eax
cmpl $0x2, (%r15)
jl 0x9752
xorl %r8d, %r8d
cmpl %edi, %esi
setl %r8b
leal -0x1(,%r8,2), %edi
movq %r14, %rdx
leaq 0x20(%rbx), %r14
addq $0x28, %rbx
movq %rbx, 0x18(%rsp)
leaq 0x20(%rdx), %r9
movq %rdx, 0x20(%rsp)
addq $0x28, %rdx
movq %rdx, 0x10(%rsp)
movl $0x1, %r11d
movl -0x4(%rcx,%r11,4), %edx
movl (%rcx,%r11,4), %ebx
movq 0x20(%rsp), %rsi
movl 0xc(%rsi), %r12d
cmpl %r12d, %ebx
movl %r12d, %esi
cmovll %eax, %esi
movq %r14, %r10
cmovlq %r9, %r10
movl %ebx, %ebp
subl %esi, %ebp
movq (%r10), %r10
movslq %ebp, %rsi
movl (%r10,%rsi,4), %ebp
cmpl %r12d, %edx
movl %r12d, %r10d
cmovll %eax, %r10d
movq %r14, %r8
cmovlq %r9, %r8
movl %edx, %r13d
subl %r10d, %r13d
movq (%r8), %r8
movslq %r13d, %r10
cmpl (%r8,%r10,4), %ebp
jne 0x970a
cmpl %r12d, %edx
movq 0x18(%rsp), %r8
movq %r8, %rdx
movq 0x10(%rsp), %r13
cmovlq %r13, %rdx
cmpl %r12d, %ebx
cmovlq %r13, %r8
movq (%r8), %r8
movl (%r8,%rsi,4), %esi
movq (%rdx), %rdx
cmpl (%rdx,%r10,4), %esi
jne 0x970a
xorl %edx, %edx
cmpl %r12d, %ebx
setl %dl
leal (%rdi,%rdx,2), %edi
decl %edi
movq 0x8(%rsp), %rsi
movslq -0x4(%rsi,%r11,4), %rdx
movl %edx, (%rsi,%r11,4)
movq (%rsp), %rsi
incl (%rsi,%rdx,4)
jmp 0x9736
testl %edi, %edi
jne 0x9749
xorl %edx, %edx
cmpl %r12d, %ebx
setl %dl
incl 0x4(%r15)
leal -0x1(,%rdx,2), %edi
movq 0x8(%rsp), %rdx
movl %r11d, (%rdx,%r11,4)
movq (%rsp), %rdx
movl $0x1, (%rdx,%r11,4)
incq %r11
movslq (%r15), %rdx
cmpq %rdx, %r11
jl 0x9668
movb $0x1, %cl
jmp 0x974b
xorl %ecx, %ecx
testl %edi, %edi
sete %al
andb %cl, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop