// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x90(%rsp), %rax
leaq 0x50(%rsp), %r10
movq %rax, (%r10)
leaq 0x34(%rsp), %rax
movl %edi, (%rax)
leaq 0x38(%rsp), %rdi
movq %rax, (%rdi)
movq %rsi, 0x8(%rdi)
movq %r10, 0x10(%rdi)
movq %r8, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
cmpq %r8, %rcx
je 0x64e6
subl $0x1, %r9d
jb 0x64dd
movl %r9d, 0xc(%rsp)
movl (%rcx), %ebp
movl 0x4(%rcx), %r14d
movl 0x8(%rcx), %eax
movl %eax, 0x14(%rsp)
movq %rcx, 0x28(%rsp)
movl 0xc(%rcx), %eax
movl %eax, 0x10(%rsp)
movq (%rdx), %rbx
movq 0x8(%rdx), %r12
cmpq %r12, %rbx
je 0x64b1
movl (%rbx), %r15d
movl 0x4(%rbx), %r13d
leaq 0x38(%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %ebp, %ecx
movl %r14d, %r8d
callq 0x75ce
testb %al, %al
jne 0x648e
leaq 0x38(%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
callq 0x75ce
addq $0x8, %rbx
testb %al, %al
je 0x6448
movq 0x18(%rsp), %rdx
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
movl 0xc(%rsp), %r9d
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %rcx
je 0x64dd
movq %rax, 0x8(%rdx)
jmp 0x64dd
leaq 0x38(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
callq 0x75ce
movl 0xc(%rsp), %r9d
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rdx
addq $0x10, %rcx
jmp 0x6410
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop