// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %r9, 0x40(%rsp)
movq %r8, %r15
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, 0x30(%rsp)
movl %edx, (%rsp)
movslq %ecx, %rsi
leaq 0x48(%rsp), %rbx
leaq 0x60(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x635c
xorl %eax, %eax
movl %r13d, %esi
movl %eax, 0x60(%rsp)
cmpl %ebp, %eax
jge 0x675a
xorl %eax, %eax
movl 0x60(%rsp), %ecx
cmpl %esi, %eax
jge 0x6752
imull %ecx, %esi
addl %eax, %esi
movslq %esi, %rdx
movsbl (%r15,%rdx), %edx
testl %edx, %edx
jns 0x672f
subl %edx, %eax
movl %r13d, %esi
jmp 0x6713
movl %eax, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
callq 0x698a
movl 0x10(%rsp), %eax
incl %eax
movl (%rsp), %r13d
movl %r13d, %esi
jmp 0x670f
movl 0x60(%rsp), %eax
incl %eax
jmp 0x6705
xorps %xmm0, %xmm0
movq 0x30(%rsp), %rax
andq $0x0, 0x10(%rax)
movups %xmm0, (%rax)
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x60(%rsp), %r13
movb $-0x1, %r12b
movl %ebp, %edi
movq %r15, %rdx
callq 0x2aee
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x6798
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rax, %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
cmpl %edx, %ebx
jge 0x68b0
movslq %ebx, %rdx
movl (%rcx,%rdx,8), %r14d
movl %r14d, 0xc(%rsp)
movl 0x4(%rcx,%rdx,8), %edi
movl %edi, 0x8(%rsp)
movl (%rsp), %esi
movl %esi, %r8d
imull %r14d, %r8d
addl %edi, %r8d
movslq %r8d, %rdi
movsbl (%r15,%rdi), %edi
movl %edi, 0x3c(%rsp)
testl %edi, %edi
js 0x6884
movl %esi, 0x4(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0xc(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rsp, %rax
movq %rax, 0x70(%rsp)
movq %r15, 0x78(%rsp)
leaq 0x3c(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x4(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpl %ebp, %r14d
jge 0x6854
movq %r13, %rdi
movl %r14d, %esi
callq 0x78c0
movl 0x4(%rsp), %eax
incl %r14d
cmpl 0x8(%rsp), %eax
jne 0x6837
movl (%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0xc(%rsp), %r14d
testl %r14d, %r14d
js 0x687d
movq %r13, %rdi
movl %r14d, %esi
callq 0x78c0
movl 0x4(%rsp), %eax
decl %r14d
cmpl 0x8(%rsp), %eax
jne 0x6860
incl %ebx
jmp 0x679a
leaq -0x8(%rax), %rsi
movl (%rcx,%rdx,8), %edi
movl -0x8(%rax), %r8d
movl %r8d, (%rcx,%rdx,8)
movl %edi, -0x8(%rax)
movl 0x4(%rcx,%rdx,8), %edi
movl -0x4(%rax), %r8d
movl %r8d, 0x4(%rcx,%rdx,8)
movl %edi, -0x4(%rax)
movq %rsi, 0x50(%rsp)
jmp 0x679a
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdi
je 0x692a
movq 0x40(%rsp), %rdx
callq 0x7974
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbx
cmpq %rbx, %r14
je 0x6922
movslq (%r14), %rax
movslq 0x4(%r14), %rdx
movslq (%rsp), %rcx
imulq %rcx, %rax
addq %rdx, %rax
cmpb $0x0, (%r15,%rax)
js 0x691c
movslq 0xc(%r14), %rsi
movslq 0x8(%r14), %rdx
imulq %rcx, %rdx
addq %rsi, %rdx
cmpb $0x0, (%r15,%rdx)
js 0x691c
movb %r12b, (%r15,%rax)
movb %r12b, (%r15,%rdx)
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x7aae
addq $0x10, %r14
jmp 0x68d3
movl (%rsp), %esi
jmp 0x677d
leaq 0x10(%rsp), %rdi
callq 0x4cd0
leaq 0x48(%rsp), %rdi
callq 0x6b9c
movq 0x30(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x6977
jmp 0x6960
jmp 0x6960
jmp 0x6960
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x4cd0
movq 0x30(%rsp), %rdi
callq 0x4cd0
leaq 0x48(%rsp), %rdi
callq 0x6b9c
movq %r14, %rdi
callq 0x22a0
nop