// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movslq (%rsi), %rbp
testq %rbp, %rbp
je 0x96c3
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 0xadfe
cmpl $0x11, %ebp
jl 0x96b2
shlq $0x2, %rbp
leaq 0x40(%r12), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb241
addq $-0x40, %rbp
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xb2d5
addq $0x4, %r13
addq $-0x4, %rbp
jne 0x9698
jmp 0x96c3
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xb241
cmpl $0x0, (%r15)
jle 0x96e5
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
xorl %edx, %edx
movslq (%rax,%rdx,4), %rsi
movl %edx, (%rcx,%rsi,4)
incq %rdx
movslq (%r15), %rsi
cmpq %rsi, %rdx
jl 0x96d3
movq 0x8(%r15), %rax
movq 0x10(%r15), %rcx
movl (%rcx), %edi
movl 0xc(%r14), %r8d
movl $0x1, %esi
movl %esi, 0x4(%r15)
movq %rax, 0x10(%rsp)
movl $0x0, (%rax)
movq 0x20(%r15), %rax
movq %rax, 0x8(%rsp)
movl %esi, (%rax)
cmpl $0x2, (%r15)
setl %sil
jge 0x9723
xorl %eax, %eax
jmp 0x980b
xorl %r9d, %r9d
cmpl %r8d, %edi
setl %r9b
leal -0x1(,%r9,2), %r8d
movl $0x1, %edi
movl -0x4(%rcx,%rdi,4), %edx
movl (%rcx,%rdi,4), %r9d
movl 0xc(%r14), %r10d
movl %r9d, %r12d
subl %r10d, %r12d
cmovll %r9d, %r12d
movq %rbx, %r11
cmovlq %r14, %r11
movslq %r12d, %r12
movq 0x20(%r11), %r13
movl %edx, %ebp
subl %r10d, %ebp
cmovll %edx, %ebp
movl (%r13,%r12,4), %edx
movq %rbx, %r13
cmovlq %r14, %r13
movq 0x20(%r13), %rax
movslq %ebp, %rbp
cmpl (%rax,%rbp,4), %edx
jne 0x97b5
movq 0x28(%r11), %rax
movl (%rax,%r12,4), %eax
movq 0x28(%r13), %rdx
cmpl (%rdx,%rbp,4), %eax
jne 0x97b5
xorl %eax, %eax
cmpl %r10d, %r9d
setl %al
leal (%r8,%rax,2), %r8d
decl %r8d
movq 0x10(%rsp), %rdx
movslq -0x4(%rdx,%rdi,4), %rax
movl %eax, (%rdx,%rdi,4)
movq 0x8(%rsp), %rdx
incl (%rdx,%rax,4)
jmp 0x97e7
testl %r8d, %r8d
je 0x97bf
xorl %r9d, %r9d
jmp 0x97ea
xorl %eax, %eax
cmpl %r10d, %r9d
setl %al
incl 0x4(%r15)
leal -0x1(,%rax,2), %r8d
movq 0x10(%rsp), %rax
movl %edi, (%rax,%rdi,4)
movq 0x8(%rsp), %rax
movl $0x1, (%rax,%rdi,4)
movb $0x1, %r9b
testb %r9b, %r9b
je 0x9802
incq %rdi
movslq (%r15), %rsi
cmpq %rsi, %rdi
setge %sil
jl 0x973a
testl %r8d, %r8d
sete %al
andb %sil, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq