// File: /workspace/github_cpp_makefile_star2_O1/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 $0x60, %rsp
movq %rcx, -0x58(%rsp)
movl 0x4(%rdx), %r8d
movslq 0xc(%rdx), %rax
movslq %esi, %rbx
imulq %rbx, %rax
movl %r8d, %r9d
imull %esi, %r9d
xorl %r11d, %r11d
cmpl %eax, %r9d
jg 0x3f1e
movl (%rdx), %r10d
movslq %r10d, %r11
leaq (%rdi,%rax,4), %rax
movq %rax, 0x18(%rsp)
movslq %r9d, %rax
leaq (%rdi,%rax,4), %rcx
movq %rcx, -0x68(%rsp)
movslq 0x8(%rdx), %rcx
movq %rcx, 0x10(%rsp)
leal 0x1(%rbx), %ecx
movslq %ecx, %r14
leaq (,%rbx,4), %rbp
leaq 0x4(%rdi), %rcx
movq %rcx, -0x28(%rsp)
movq %rdi, %rcx
notq %rcx
movq %rcx, 0x28(%rsp)
movq %rbp, %r15
negq %r15
movq %r8, -0x78(%rsp)
leal 0x1(%r8), %ecx
imull %esi, %ecx
movl %ecx, -0x80(%rsp)
shlq $0x2, %rax
leaq (%rax,%r11,4), %rcx
leaq (%rcx,%rbx,4), %rax
subq %rbp, %rcx
addq %rdi, %rcx
movq %rcx, -0x30(%rsp)
movq %r11, 0x8(%rsp)
leal 0x1(%r11), %ecx
movq %rcx, (%rsp)
leaq (%rax,%r14,4), %rax
shlq $0x2, %r14
movq %r14, 0x38(%rsp)
addq %rdi, %rax
addq $0x4, %rax
movq %rax, -0x38(%rsp)
movq %rdi, 0x30(%rsp)
leaq 0xc(%rdi), %rax
movq %rax, 0x20(%rsp)
movdqa 0x15c9(%rip), %xmm0     # 0x50a0
movdqa 0x15d1(%rip), %xmm1     # 0x50b0
movdqa 0x15d9(%rip), %xmm2     # 0x50c0
pcmpeqd %xmm3, %xmm3
xorl %r11d, %r11d
movq %rdx, -0x20(%rsp)
movq %rbx, -0x70(%rsp)
movl %r10d, -0x40(%rsp)
movq %r15, -0x8(%rsp)
movq 0x10(%rsp), %rax
cmpl %eax, %r10d
jg 0x3ee8
movq -0x68(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
movq 0x8(%rsp), %rax
leaq (%rcx,%rax,4), %r12
movq -0x38(%rsp), %rcx
movq (%rsp), %rax
movq -0x30(%rsp), %r14
movl %r10d, %r13d
movl %r9d, -0x3c(%rsp)
movq %rdi, 0x40(%rsp)
movl (%r12), %ebx
cmpl $0x2, %ebx
jl 0x3ec8
movq %rax, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
movq %r11, 0x58(%rsp)
movl $0x1, %eax
xorl %r11d, %r11d
movl %r9d, %r8d
movq %r14, 0x48(%rsp)
movq %r14, %r9
movl -0x80(%rsp), %ecx
movl %r8d, %edi
movl %r11d, %r10d
decl %eax
subl %esi, %ecx
leaq (%r9,%r15), %r14
subl %esi, %r8d
leal -0x1(%r10), %r11d
cmpl %ebx, (%r9)
movq %r14, %r9
je 0x3b70
movl $0x1, %r8d
movl %r13d, -0x7c(%rsp)
movl %r13d, %r11d
xorl %r14d, %r14d
movq %r14, %r9
movl %r11d, %r13d
decl %r8d
decq %r14
leal -0x1(%r13), %r11d
cmpl %ebx, -0x4(%r12,%r9,4)
je 0x3b9e
movl $0x1, %r11d
movq -0x70(%rsp), %rdx
movq %r12, 0x50(%rsp)
cmpl %ebx, 0x4(%r12,%rdx,4)
movq 0x38(%rsp), %rdx
jne 0x3be5
movq -0x18(%rsp), %r15
incl %r11d
leaq (%r15,%rdx), %r12
cmpl %ebx, (%r15)
movq %r12, %r15
je 0x3bd6
leaq 0x1(%r14), %r15
cmpl %r15d, %eax
movl %r15d, %edx
cmovll %eax, %edx
movl %edx, -0x50(%rsp)
movq -0x10(%rsp), %rdx
leaq (%rdx,%r14), %rbx
movq -0x20(%rsp), %rdx
cmpl (%rdx), %ebx
jge 0x3c0a
movl %ebx, (%rdx)
movq %rbx, -0x48(%rsp)
movq -0x78(%rsp), %rbx
addl %eax, %ebx
cmpl 0x4(%rdx), %ebx
jge 0x3c1e
movl %ebx, 0x4(%rdx)
movl %ebx, -0x60(%rsp)
movl %r11d, %edx
cmpl %r8d, %eax
cmovll %eax, %r8d
movl -0x7c(%rsp), %ebx
movl %ebx, %r12d
subl %r8d, %r12d
addq %r14, %rdx
addq %r12, %rdx
movq -0x20(%rsp), %rbx
cmpl 0x8(%rbx), %edx
jle 0x3c49
movl %edx, 0x8(%rbx)
cmpl %r15d, %eax
cmovll %eax, %r15d
movl %r11d, %r8d
subl %r15d, %r8d
addl -0x78(%rsp), %r8d
leal (%rax,%r8), %edx
decl %edx
cmpl 0xc(%rbx), %edx
movq -0x48(%rsp), %r14
jle 0x3c6e
movl %edx, 0xc(%rbx)
movl %r11d, %r15d
subl -0x50(%rsp), %r15d
addl %eax, %r8d
movq -0x58(%rsp), %rax
movl %r14d, (%rax)
movl -0x60(%rsp), %edx
movl %edx, 0x4(%rax)
movl %r15d, 0x8(%rax)
imull %esi, %r8d
cmpl %r8d, %ecx
movq -0x70(%rsp), %rbx
jge 0x3e8b
movslq %r14d, %rdx
movq 0x30(%rsp), %rbx
leaq (%rbx,%rdx,4), %r14
movslq %r8d, %rax
leaq (%r14,%rax,4), %rax
movslq %ecx, %rcx
leaq (%r14,%rcx,4), %r8
movslq %r15d, %r12
shlq $0x2, %rdx
leaq (%rdx,%rcx,4), %r14
leaq (%r14,%rbx), %rcx
leaq (%rcx,%r12,4), %rcx
movq %rcx, -0x48(%rsp)
movq 0x28(%rsp), %rdx
movq %rdx, %rcx
subq %r14, %rcx
movq %rcx, -0x50(%rsp)
addq -0x28(%rsp), %r14
movslq %edi, %rcx
cmpl %r9d, %r10d
cmovll %r10d, %r9d
leaq (,%rcx,4), %rdi
movq %rdi, -0x60(%rsp)
movslq %r13d, %r12
subl %r9d, %r11d
leaq (%rbx,%rcx,4), %rdi
movq -0x70(%rsp), %rbx
leaq (%rdi,%r12,4), %rdi
movslq %r11d, %r9
leaq (%rdi,%r9,4), %r10
movq %rdx, %rdi
subq -0x60(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,4), %rdx
leaq (%rdx,%r12,4), %r13
movq -0x28(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
leaq (%rcx,%r12,4), %rcx
leaq (,%r12,4), %rdx
subq %rdx, %rdi
xorl %r11d, %r11d
cmpq %rcx, %r10
movq %rcx, %r9
cmovaq %r10, %r9
testl %r15d, %r15d
jle 0x3e6f
addq %rdi, %r9
shrq $0x2, %r9
addq $0x4, %r9
andq $-0x4, %r9
movq %rbp, %rdx
imulq %r11, %rdx
movq -0x48(%rsp), %r12
addq %rdx, %r12
addq %r14, %rdx
cmpq %rdx, %r12
cmovaq %r12, %rdx
movq -0x8(%rsp), %r12
imulq %r11, %r12
addq -0x50(%rsp), %r12
addq %rdx, %r12
movq %r12, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
psrlq $0x2, %xmm4
pxor %xmm2, %xmm4
xorl %edx, %edx
movq %rdx, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm1, %xmm6
pxor %xmm2, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm4, %xmm7
pcmpeqd %xmm4, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %r12d
notl %r12d
testb $0x1, %r12b
je 0x3df5
movl $0x1, -0xc(%r13,%rdx,4)
pxor %xmm3, %xmm6
pextrw $0x4, %xmm6, %r12d
testb $0x1, %r12b
je 0x3e0e
movl $0x1, -0x8(%r13,%rdx,4)
por %xmm0, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm4, %xmm6
pcmpeqd %xmm4, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm3, %xmm5
pextrw $0x0, %xmm5, %r12d
testb $0x1, %r12b
je 0x3e4d
movl $0x1, -0x4(%r13,%rdx,4)
pextrw $0x4, %xmm5, %r12d
testb $0x1, %r12b
je 0x3e62
movl $0x1, (%r13,%rdx,4)
addq $0x4, %rdx
cmpq %rdx, %r9
jne 0x3da7
leaq (%r8,%rbx,4), %r8
incq %r11
addq %rbp, %r13
addq %rbp, %r10
addq %rbp, %rcx
subq %rbp, %rdi
cmpq %rax, %r8
jb 0x3d46
movq 0x58(%rsp), %r11
incl %r11d
addq $0xc, -0x58(%rsp)
movl -0x3c(%rsp), %r9d
movl -0x40(%rsp), %r10d
movq -0x8(%rsp), %r15
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %r12
movq -0x18(%rsp), %rcx
movq -0x10(%rsp), %rax
movq 0x48(%rsp), %r14
movl -0x7c(%rsp), %r13d
jmp 0x3ecd
movq -0x70(%rsp), %rbx
incl %r13d
addq $0x4, %r12
addq $0x4, %r14
incq %rax
addq $0x4, %rcx
cmpq %rdi, %r12
jbe 0x3b3d
movq -0x78(%rsp), %rax
incl %eax
movq %rax, -0x78(%rsp)
movq -0x68(%rsp), %rax
leaq (%rax,%rbx,4), %rax
addl %esi, -0x80(%rsp)
addq %rbp, -0x30(%rsp)
addq %rbp, -0x38(%rsp)
addl %esi, %r9d
movq %rax, -0x68(%rsp)
cmpq 0x18(%rsp), %rax
jbe 0x3b02
movq -0x58(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl %r11d, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq