// File: /workspace/github_cpp_makefile_star2_O1/kmyk[P]topcoder-marathon-match-100-same-color-pairs/./SameColorPairs.cpp
// Function: std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>> solve_greedy_once<xor_shift_128>(int, int, int, std::array<signed char, 10000ul>&, xor_shift_128&)
vector<array<int, 4> > solve_greedy_once(int H, int W, int C, array<int8_t, MAX_H * MAX_W> & board, RandomEngine & gen) {
    // prepare lists of living cells
    vector<pair<int, int> > color(C);
    REP (y, H) {
        for (int x = 0; x < W; ) {
            if (board[y * W + x] < 0) {
                x -= board[y * W + x];
            } else {
                color.emplace_back(y, x);
                ++ x;
            }
        }
    }

    // remove greedily
    vector<array<int, 4> > rects;
    vector<array<int, 4> > removable;
    for (int iteration = 0; ; ++ iteration) {
        update_compression(H, W, board);
        removable.clear();
        for (int i = 0; i < int(color.size()); ++ i) {
            int y1, x1; tie(y1, x1) = color[i];
            int c = board[y1 * W + x1];
            if (c < 0) {
                swap(color[i], color.back());
                color.pop_back();
                -- i;
                continue;
            }
            int min_x2 = W;
            auto func = [&](int y2) {
                int x2 = x1 + (y2 == y1);
                while (x2 < W and board[y2 * W + x2] < 0) {
                    x2 -= board[y2 * W + x2];
                }
                if (x2 == W) return;
                if (board[y2 * W + x2] == c) {
                    if (x2 < min_x2) {
                        removable.push_back((array<int, 4>) { y1, x1, y2, x2 });
                    }
                    chmin(min_x2, x2 + 1);
                } else {
                    chmin(min_x2, x2);
                }
            };
            REP3 (y2, y1, H) {
                func(y2);
                if (min_x2 == x1) break;
            }
            min_x2 = W;
            REP_R (y2, y1 + 1) {
                func(y2);
                if (min_x2 == x1) break;
            }
        }
        if (removable.empty()) break;
        shuffle(ALL(removable), gen);
        for (auto const & rect : removable) {
            int y1 = rect[0];
            int x1 = rect[1];
            int y2 = rect[2];
            int x2 = rect[3];
            if (board[y1 * W + x1] < 0) continue;
            if (board[y2 * W + x2] < 0) continue;
            board[y1 * W + x1] = -1;
            board[y2 * W + x2] = -1;
            rects.push_back(rect);
        }
    }
    return rects;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x60(%rsp)
movq %r8, %r15
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, 0x38(%rsp)
movslq %ecx, %rsi
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x6d50
movl $0x0, (%rbx)
movl %r14d, 0xc(%rsp)
testl %r14d, %r14d
jle 0x734d
leaq 0x48(%rsp), %r14
leaq 0x10(%rsp), %r13
leaq 0x28(%rsp), %rbx
movl $0x0, 0x28(%rsp)
testl %ebp, %ebp
jle 0x733d
xorl %eax, %eax
movl 0x10(%rsp), %ecx
imull %ebp, %ecx
addl %eax, %ecx
movslq %ecx, %rcx
movsbl (%r15,%rcx), %ecx
testl %ecx, %ecx
js 0x7333
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x76e6
movl 0x28(%rsp), %eax
incl %eax
jmp 0x7335
subl %ecx, %eax
movl %eax, 0x28(%rsp)
cmpl %ebp, %eax
jl 0x7308
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
cmpl 0xc(%rsp), %eax
jl 0x72fa
xorps %xmm0, %xmm0
movq 0x38(%rsp), %rcx
movups %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
leaq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
movl 0xc(%rsp), %edi
movl %ebp, %esi
movq %r15, %rdx
callq 0x2d51
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x7389
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
testl %edx, %edx
jle 0x75bd
xorl %r9d, %r9d
movslq %r9d, %rdx
movl (%rax,%rdx,8), %r8d
movl 0x4(%rax,%rdx,8), %r14d
movl %r8d, %esi
imull %ebp, %esi
addl %r14d, %esi
movslq %esi, %rsi
movb (%r15,%rsi), %sil
movb %sil, 0x7(%rsp)
testb %sil, %sil
js 0x757d
movl %r9d, 0x44(%rsp)
movl %r8d, %r13d
movl %ebp, %ebx
movl %r8d, 0x8(%rsp)
cmpl 0xc(%rsp), %r8d
jge 0x74a8
xorl %r12d, %r12d
cmpl %r13d, 0x8(%rsp)
sete %r12b
addl %r14d, %r12d
cmpl %ebp, %r12d
jge 0x741e
movl %r13d, %eax
imull %ebp, %eax
leal (%r12,%rax), %ecx
movslq %ecx, %rcx
movsbl (%r15,%rcx), %ecx
testl %ecx, %ecx
jns 0x741e
subl %ecx, %r12d
cmpl %ebp, %r12d
jl 0x7406
cmpl %ebp, %r12d
je 0x7495
movl %r13d, %eax
imull %ebp, %eax
addl %r12d, %eax
cltq
movb 0x7(%rsp), %cl
cmpb (%r15,%rax), %cl
jne 0x7470
cmpl %ebx, %r12d
jge 0x7488
movl 0x8(%rsp), %eax
movl %eax, 0x28(%rsp)
movl %r14d, 0x2c(%rsp)
movl %r13d, 0x30(%rsp)
movl %r12d, 0x34(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7479
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x7488
cmpl %ebx, %r12d
cmovll %r12d, %ebx
jmp 0x7495
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x8060
incl %r12d
cmpl %ebx, %r12d
cmovgel %ebx, %r12d
movl %r12d, %ebx
cmpl %r14d, %ebx
je 0x74a8
incl %r13d
cmpl 0xc(%rsp), %r13d
jl 0x73ec
cmpl $0x0, 0x8(%rsp)
js 0x7573
movl 0x8(%rsp), %eax
movl %eax, %ebx
movl %ebp, %r13d
xorl %r12d, %r12d
cmpl %ebx, 0x8(%rsp)
sete %r12b
addl %r14d, %r12d
cmpl %ebp, %r12d
jge 0x74ec
movl %ebx, %eax
imull %ebp, %eax
leal (%r12,%rax), %ecx
movslq %ecx, %rcx
movsbl (%r15,%rcx), %ecx
testl %ecx, %ecx
jns 0x74ec
subl %ecx, %r12d
cmpl %ebp, %r12d
jl 0x74d4
cmpl %ebp, %r12d
je 0x7561
movl %ebx, %eax
imull %ebp, %eax
addl %r12d, %eax
cltq
movb 0x7(%rsp), %cl
cmpb (%r15,%rax), %cl
jne 0x753c
cmpl %r13d, %r12d
jge 0x7554
movl 0x8(%rsp), %eax
movl %eax, 0x28(%rsp)
movl %r14d, 0x2c(%rsp)
movl %ebx, 0x30(%rsp)
movl %r12d, 0x34(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7545
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x7554
cmpl %r13d, %r12d
cmovll %r12d, %r13d
jmp 0x7561
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x8060
incl %r12d
cmpl %r13d, %r12d
cmovgel %r13d, %r12d
movl %r12d, %r13d
cmpl %r14d, %r13d
je 0x7573
leal -0x1(%rbx), %eax
testl %ebx, %ebx
movl %eax, %ebx
jg 0x74bc
movl 0x44(%rsp), %r9d
incl %r9d
jmp 0x75a0
leaq -0x8(%rcx), %rsi
movl -0x8(%rcx), %edi
movl %edi, (%rax,%rdx,8)
movl %r8d, -0x8(%rcx)
movl 0x4(%rax,%rdx,8), %edi
movl -0x4(%rcx), %r8d
movl %r8d, 0x4(%rax,%rdx,8)
movl %edi, -0x4(%rcx)
movq %rsi, 0x50(%rsp)
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
cmpl %edx, %r9d
jl 0x73a8
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdi
je 0x764b
movq 0x60(%rsp), %rdx
callq 0x819f
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x736a
movslq (%rbx), %rax
movslq 0x4(%rbx), %rdx
movslq %ebp, %rcx
imulq %rcx, %rax
addq %rdx, %rax
cmpb $0x0, (%r15,%rax)
js 0x7645
movslq 0xc(%rbx), %rsi
movslq 0x8(%rbx), %rdx
imulq %rcx, %rdx
addq %rsi, %rdx
cmpb $0x0, (%r15,%rdx)
js 0x7645
movb $-0x1, %cl
movb %cl, (%r15,%rax)
movb %cl, (%r15,%rdx)
movq 0x38(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x763d
movups (%rbx), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rdi)
jmp 0x7645
movq %rbx, %rdx
callq 0x84d6
addq $0x10, %rbx
jmp 0x75e0
testq %rdi, %rdi
je 0x765d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x7674
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x38(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7693
jmp 0x7693
movq %rax, %r14
jmp 0x76c6
jmp 0x7693
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x76ad
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x38(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x76c6
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x76dd
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x2190
movq %r14, %rdi
callq 0x2260
nop