// File: /workspace/github_cpp_makefile_star2_O2/lioncedric[P]cimpressGridCoverage/src/squares.cc
// Function: clear_squares(int*, int, int*, int*)
int clear_squares(int * puzzle, const int width, int * area, int * squares_removed) {
    int nb_squares_removed = 0;
    int first_col = area[0];
    int first_row = area[1];
    int last_col = area[2];
    int last_row = area[3];

    const int * ptrLineMax = puzzle + last_row * width;
    int row = first_row;
    for (int* ptrLine = puzzle + (first_row) * width; ptrLine <= ptrLineMax; ptrLine += width) {
        const int * ptrColMax = ptrLine + last_col;
        int col = first_col;
        for (int* ptrCol = ptrLine + first_col; ptrCol <= ptrColMax; ++ptrCol) {

            if (*ptrCol > 1) {
                const int id = *ptrCol;

                int size = 1;
                int xoffset = 0;
                int yoffset = 0;
                int* gridPos = ptrCol - width;

                while (*gridPos == id) {
                    gridPos -= width;
                    yoffset -= 1;
                }
                gridPos = ptrCol - 1;
                while (*gridPos == id) {
                    gridPos -= 1;
                    xoffset -= 1;
                }

                gridPos = ptrCol + 1 + width;
                while (*gridPos == id) {
                    gridPos += width + 1;
                    ++size;
                }

                size += (min(xoffset, yoffset)*-1);
                if (col + xoffset < area[0]) {
                    area[0] = col + xoffset;
                }
                if (row + yoffset < area[1]) {
                    area[1] = row + yoffset;
                }
                if (col + xoffset + size - 1 > area[2]) {
                    area[2] = col + xoffset + size - 1;
                }
                if (row + yoffset + size - 1 > area[3]) {
                    area[3] = row + yoffset + size - 1;
                }
                *squares_removed = col + xoffset;
                *(squares_removed + 1) = row + yoffset;
                *(squares_removed + 2) = size;
                clear_square(puzzle, width, squares_removed);
                ++nb_squares_removed;
                squares_removed += 3;
            }
            ++col;
        }
        ++row;
    }
    *squares_removed = -1;
    return nb_squares_removed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x18(%rsp)
movl (%rdx), %ecx
movl 0x4(%rdx), %r13d
movslq 0x8(%rdx), %rax
movq %rax, 0x48(%rsp)
movslq 0xc(%rdx), %rax
movslq %esi, %r8
imulq %r8, %rax
leaq (%rdi,%rax,4), %rax
movq %rax, 0x50(%rsp)
movl %r13d, %eax
movl %esi, 0x28(%rsp)
imull %esi, %eax
cltq
leaq (%rdi,%rax,4), %rsi
movl %ecx, 0x24(%rsp)
movslq %ecx, %r9
movq %r8, %rbp
negq %rbp
leal 0x1(%r8), %ecx
movslq %ecx, %r15
movq %r9, 0x38(%rsp)
leaq (,%r9,4), %rcx
leaq (%rcx,%r8,4), %rcx
leaq (%rcx,%rax,4), %rax
movq %r8, 0x40(%rsp)
leaq (,%r8,4), %rcx
movq %rcx, 0x30(%rsp)
movq %rdi, 0x60(%rsp)
addq %rdi, %rax
addq $0x4, %rax
movq %rax, 0x10(%rsp)
shlq $0x2, %r15
xorl %ecx, %ecx
cmpq 0x50(%rsp), %rsi
ja 0x3511
movq 0x48(%rsp), %rax
leaq (%rsi,%rax,4), %rdi
movq %rsi, 0x58(%rsp)
movq 0x38(%rsp), %rax
leaq (%rsi,%rax,4), %r12
movq 0x10(%rsp), %rbx
movl 0x24(%rsp), %r9d
movq %rdi, 0x68(%rsp)
cmpq %rdi, %r12
ja 0x34ec
movl (%r12), %r14d
cmpl $0x2, %r14d
jl 0x34dc
movl %ecx, 0x2c(%rsp)
xorl %edi, %edi
movl %r13d, %r8d
movq %r12, %rsi
movl %edi, %eax
movl %r8d, %ecx
leaq (%rsi,%rbp,4), %rsi
leal -0x1(%rax), %edi
leal -0x1(%rcx), %r8d
cmpl %r14d, (%rsi)
je 0x3402
xorl %r8d, %r8d
movl %r9d, 0xc(%rsp)
movq %r12, %r10
movl %r8d, %esi
movl %r9d, %edi
leaq -0x4(%r10), %r11
leal -0x1(%rsi), %r8d
leal -0x1(%rdi), %r9d
cmpl %r14d, -0x4(%r10)
movq %r11, %r10
je 0x3422
cmpl %esi, %eax
movl %esi, %r9d
cmovll %eax, %r9d
subl %r9d, %ecx
subl %r9d, %edi
pushq $0x1
popq %r8
subl %r9d, %r8d
movq %rbx, %r9
cmpl %r14d, (%r9)
jne 0x3467
incl %ecx
incl %edi
incl %r8d
addq %r15, %r9
jmp 0x3456
addl 0xc(%rsp), %esi
cmpl (%rdx), %esi
jge 0x3471
movl %esi, (%rdx)
addl %r13d, %eax
cmpl 0x4(%rdx), %eax
jge 0x347c
movl %eax, 0x4(%rdx)
movq %rbx, 0x70(%rsp)
cmpl 0x8(%rdx), %edi
jle 0x3489
movl %edi, 0x8(%rdx)
movl %r13d, %ebx
cmpl 0xc(%rdx), %ecx
jle 0x3494
movl %ecx, 0xc(%rdx)
movq %rdx, %r13
movq 0x18(%rsp), %r14
movl %esi, (%r14)
movl %eax, 0x4(%r14)
movl %r8d, 0x8(%r14)
movq 0x60(%rsp), %rdi
movl 0x28(%rsp), %esi
movq %r14, %rdx
callq 0x32c6
movl 0x2c(%rsp), %ecx
incl %ecx
addq $0xc, %r14
movq %r14, 0x18(%rsp)
movq %r13, %rdx
movl %ebx, %r13d
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rbx
movl 0xc(%rsp), %r9d
incl %r9d
addq $0x4, %r12
addq $0x4, %rbx
jmp 0x33df
incl %r13d
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %rsi
leaq (%rsi,%rax,4), %rsi
movq 0x10(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x33ae
movq 0x18(%rsp), %rax
orl $-0x1, (%rax)
movl %ecx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq