// File: kmyk[P]topcoder-marathon-match-100-same-color-pairs/./SameColorPairs.cpp
// Function: void apply_rects_with_preceding_cells<__gnu_cxx::__normal_iterator<std::array<int, 4ul>*, std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>>>, std::back_insert_iterator<std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>>>>(int, std::array<signed char, 10000ul>&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>&, __gnu_cxx::__normal_iterator<std::array<int, 4ul>*, std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>>>, __gnu_cxx::__normal_iterator<std::array<int, 4ul>*, std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>>>, int, std::back_insert_iterator<std::vector<std::array<int, 4ul>, std::allocator<std::array<int, 4ul>>>>)
void apply_rects_with_preceding_cells(int W, array<int8_t, MAX_H * MAX_W> & board, vector<pair<int, int> > & preciding, InputIterator first, InputIterator last, int skip_i, OutputIterator dest) {
    auto check_use = [&](int y1, int x1, int y2, int x2) {
        if (not is_valid_pair(W, board, make_pair(y1, x1), make_pair(y2, x2))) return false;
        board[y1 * W + x1] = -1;
        board[y2 * W + x2] = -1;
        *(dest ++) = { y1, x1, y2, x2 };
        return true;
    };
    for (; first != last; ++ first) {
        if ((skip_i --) == 0) continue;
        int y1 = (*first)[0];
        int x1 = (*first)[1];
        int y2 = (*first)[2];
        int x2 = (*first)[3];
        for (auto const & p : preciding) {
            int y, x; tie(y, x) = p;
            if (check_use(y, x, y1, x1) or check_use(y, x, y2, x2)) {
                preciding.clear();
                goto next;
            }
        }
        check_use(y1, x1, y2, x2);
next: ;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rcx, -0x8(%rbp)
movq %r8, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movl %edi, -0x1c(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
leaq -0x1c(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x7010
testb $0x1, %al
jne 0x11818
jmp 0x11998
movl -0x34(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x34(%rbp)
cmpl $0x0, %eax
jne 0x1182d
jmp 0x1198a
leaq -0x8(%rbp), %rdi
callq 0x7050
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x7070
movl (%rax), %eax
movl %eax, -0x54(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x7050
movq %rax, %rdi
movl $0x1, %esi
callq 0x7070
movl (%rax), %eax
movl %eax, -0x58(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x7050
movq %rax, %rdi
movl $0x2, %esi
callq 0x7070
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
leaq -0x8(%rbp), %rdi
callq 0x7050
movq %rax, %rdi
movl $0x3, %esi
callq 0x7070
movl (%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x151d0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x151a0
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x15200
testb $0x1, %al
jne 0x118d0
jmp 0x11972
leaq -0x70(%rbp), %rdi
callq 0x141c0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x84(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x7380
movq -0xa0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x73c0
movl -0x84(%rbp), %esi
movl -0x88(%rbp), %edx
movl -0x54(%rbp), %ecx
movl -0x58(%rbp), %r8d
leaq -0x50(%rbp), %rdi
callq 0x15240
testb $0x1, %al
jne 0x11957
movl -0x84(%rbp), %esi
movl -0x88(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x60(%rbp), %r8d
leaq -0x50(%rbp), %rdi
callq 0x15240
testb $0x1, %al
jne 0x11957
jmp 0x11962
movq -0x30(%rbp), %rdi
callq 0x12650
jmp 0x11988
jmp 0x11964
leaq -0x70(%rbp), %rdi
callq 0x14400
jmp 0x118ba
movl -0x54(%rbp), %esi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
movl -0x60(%rbp), %r8d
leaq -0x50(%rbp), %rdi
callq 0x15240
jmp 0x1198a
leaq -0x8(%rbp), %rdi
callq 0x70a0
jmp 0x11802
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)