// File: 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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x140(%rbp)
movq %rdi, %rax
movq %rax, -0x138(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x11540
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x11560
jmp 0x120b5
leaq -0x41(%rbp), %rdi
callq 0x115d0
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x12183
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x12173
movq -0x20(%rbp), %rdi
movl -0x58(%rbp), %eax
imull -0x10(%rbp), %eax
addl -0x5c(%rbp), %eax
movslq %eax, %rsi
callq 0x72a0
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x1213f
movq -0x20(%rbp), %rdi
movl -0x58(%rbp), %eax
imull -0x10(%rbp), %eax
addl -0x5c(%rbp), %eax
movslq %eax, %rsi
callq 0x72a0
movsbl (%rax), %ecx
movl -0x5c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1216e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x115d0
jmp 0x12602
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x12670
jmp 0x12152
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1216e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x125f9
jmp 0x120d8
jmp 0x12175
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x120c5
movq -0x140(%rbp), %rdi
movb $0x0, -0x5d(%rbp)
callq 0x7810
leaq -0x78(%rbp), %rdi
callq 0x7810
movl $0x0, -0x7c(%rbp)
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movq -0x20(%rbp), %rdx
callq 0x34d0
jmp 0x121b4
leaq -0x78(%rbp), %rdi
callq 0x167c0
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x144(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x114a0
movq %rax, %rcx
movl -0x144(%rbp), %eax
cmpl %ecx, %eax
jge 0x123da
movslq -0x80(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x115f0
movq %rax, -0x150(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x84(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x7380
movq -0x150(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x73c0
movq -0x20(%rbp), %rdi
movl -0x84(%rbp), %eax
imull -0x10(%rbp), %eax
addl -0x88(%rbp), %eax
movslq %eax, %rsi
callq 0x72a0
movsbl (%rax), %eax
movl %eax, -0x9c(%rbp)
cmpl $0x0, -0x9c(%rbp)
jge 0x122bf
movslq -0x80(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x115f0
movq %rax, -0x158(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x167e0
movq -0x158(%rbp), %rdi
movq %rax, %rsi
callq 0x14890
leaq -0x40(%rbp), %rdi
callq 0x16820
movl -0x80(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x123cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x70c0
movq -0x140(%rbp), %rdi
callq 0x70c0
jmp 0x125f9
movl -0x10(%rbp), %eax
movl %eax, -0xa0(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x84(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x9c(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1236f
movl -0xdc(%rbp), %esi
leaq -0xd8(%rbp), %rdi
callq 0x16850
jmp 0x1234c
movl -0xa0(%rbp), %eax
cmpl -0x88(%rbp), %eax
jne 0x1235c
jmp 0x1236f
jmp 0x1235e
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x1232d
movl -0x10(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
subl $0x1, %eax
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0xe0(%rbp)
jl 0x123ca
movl -0xe0(%rbp), %esi
leaq -0xd8(%rbp), %rdi
callq 0x16850
jmp 0x123a7
movl -0xa0(%rbp), %eax
cmpl -0x88(%rbp), %eax
jne 0x123b7
jmp 0x123ca
jmp 0x123b9
movl -0xe0(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0x1238a
jmp 0x123cc
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x121c4
leaq -0x78(%rbp), %rdi
callq 0x113a0
testb $0x1, %al
jne 0x123e9
jmp 0x123ee
jmp 0x125c1
leaq -0x78(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x7ae0
movq -0x160(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0x7b10
movq %rax, -0xf0(%rbp)
movq -0x28(%rbp), %rdx
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x169b0
jmp 0x12431
leaq -0x78(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x6fb0
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x6fe0
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x7010
testb $0x1, %al
jne 0x1247e
jmp 0x125b1
leaq -0x100(%rbp), %rdi
callq 0x7050
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16c20
movl (%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0x16c20
movl (%rax), %eax
movl %eax, -0x118(%rbp)
movq -0x110(%rbp), %rdi
movl $0x2, %esi
callq 0x16c20
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
movq -0x110(%rbp), %rdi
movl $0x3, %esi
callq 0x16c20
movl (%rax), %eax
movl %eax, -0x120(%rbp)
movq -0x20(%rbp), %rdi
movl -0x114(%rbp), %eax
imull -0x10(%rbp), %eax
addl -0x118(%rbp), %eax
movslq %eax, %rsi
callq 0x72a0
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x1251d
jmp 0x125a0
movq -0x20(%rbp), %rdi
movl -0x11c(%rbp), %eax
imull -0x10(%rbp), %eax
addl -0x120(%rbp), %eax
movslq %eax, %rsi
callq 0x72a0
movsbl (%rax), %eax
cmpl $0x0, %eax
jge 0x12543
jmp 0x125a0
movq -0x20(%rbp), %rdi
movl -0x114(%rbp), %eax
movl -0x10(%rbp), %ecx
imull %ecx, %eax
movl -0x118(%rbp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x72a0
movb $-0x1, (%rax)
movq -0x20(%rbp), %rdi
movl -0x11c(%rbp), %eax
movl -0x10(%rbp), %ecx
imull %ecx, %eax
movl -0x120(%rbp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x72a0
movq -0x140(%rbp), %rdi
movb $-0x1, (%rax)
movq -0x110(%rbp), %rsi
callq 0x16c50
jmp 0x1259e
jmp 0x125a0
leaq -0x100(%rbp), %rdi
callq 0x70a0
jmp 0x12462
jmp 0x125b3
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x121a3
movb $0x1, -0x5d(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x70c0
testb $0x1, -0x5d(%rbp)
jne 0x125e0
movq -0x140(%rbp), %rdi
callq 0x70c0
leaq -0x40(%rbp), %rdi
callq 0x119b0
movq -0x138(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x119b0
movq -0x50(%rbp), %rdi
callq 0x2340
nopl (%rax,%rax)