// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movslq (%rax), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x166f0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1409e
movl -0x3c(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x3c(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1406a
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl %esi, %edx
cmovll %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl $0x1, (%rax)
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1430f
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x14147
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
jmp 0x14162
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movq -0x20(%rbp), %rdx
subl 0xc(%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x48(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x14188
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
jmp 0x141a3
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x48(%rbp), %ecx
movq -0x20(%rbp), %rdx
subl 0xc(%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x4c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x141c9
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
jmp 0x141e4
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movq -0x20(%rbp), %rdx
subl 0xc(%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x4c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1420a
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
jmp 0x14225
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x4c(%rbp), %ecx
movq -0x20(%rbp), %rdx
subl 0xc(%rdx), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x58(%rbp), %eax
jne 0x142a5
movl -0x54(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jne 0x142a5
movl -0x48(%rbp), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl %esi, %edx
cmovll %ecx, %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x44(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x44(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x142ff
cmpl $0x0, -0x40(%rbp)
je 0x142b1
movb $0x0, -0x1(%rbp)
jmp 0x1431b
movl -0x48(%rbp), %edx
movq -0x20(%rbp), %rax
movl 0xc(%rax), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl %esi, %edx
cmovll %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movl -0x44(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x44(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x44(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x14301
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x140ef
cmpl $0x0, -0x40(%rbp)
sete %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)