// File: cmaureir[P]strip-packing/scattersearch.cpp
// Function: search_fit(int, int, int, int&, int&, int&, int**)
bool search_fit(int item, int item_w, int item_h, int &a, int &b, int &h, int **strip){
    //cout << "search_fit()" << endl;
    bool fit = true;

    for (int y = 0; y < strip_height; y++)
    {
        for (int x = 0; x < strip_width; x++)
        {
            if (strip[x][y]  == 0 && item_w + x - 1 < strip_width) // empty space
            {
                for (a = x; a < x + item_w; a++)
                {
                    if (strip[a][y] != 0 || strip[a][y+item_h-1] != 0)
                    {
                        fit = false;
                        break;
                    }
                }
                if(fit)
                {
                    for (b = y; b < y + item_h; b++)
                    {
                        if (strip[x][b] != 0 || strip[x+item_w-1][b] != 0)
                        {
                            fit = false;
                            break;
                        }
                    }
                }

                if (fit){
                    a = x;
                    b = y;
                    if (h < b + item_h){
                        h = b + item_h;
                    }
                    return fit;
                }
            }
            else
            {
                if (item_w + x >= strip_width)
                {
                    x = strip_width;
                }
                else
                {
                    x = x + bs[strip[x][y]-1].width - 1;
                }
            }
            fit = true;
        }
    }
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movb $0x1, -0x29(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0x18887(%rip), %eax     # 0x2241c
jge 0x9d75
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl 0x1886d(%rip), %eax     # 0x22418
jge 0x9d65
movq 0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x9cfe
movl -0xc(%rbp), %eax
addl -0x34(%rbp), %eax
subl $0x1, %eax
cmpl 0x1883e(%rip), %eax     # 0x22418
jge 0x9cfe
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl -0x34(%rbp), %ecx
addl -0xc(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9c48
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x9c33
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl -0x30(%rbp), %ecx
addl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x9c39
movb $0x0, -0x29(%rbp)
jmp 0x9c48
jmp 0x9c3b
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9be9
testb $0x1, -0x29(%rbp)
je 0x9cb8
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl -0x30(%rbp), %ecx
addl -0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9cb6
movq 0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x9ca1
movq 0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl -0xc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq -0x20(%rbp), %rcx
movslq (%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x9ca7
movb $0x0, -0x29(%rbp)
jmp 0x9cb6
jmp 0x9ca9
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9c57
jmp 0x9cb8
testb $0x1, -0x29(%rbp)
je 0x9cfc
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
addl -0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0x9cf2
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl -0x10(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movb -0x29(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x9d79
jmp 0x9d53
movl -0xc(%rbp), %eax
addl -0x34(%rbp), %eax
cmpl 0x1870e(%rip), %eax     # 0x22418
jl 0x9d17
movl 0x18706(%rip), %eax     # 0x22418
movl %eax, -0x34(%rbp)
jmp 0x9d51
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
movq 0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x186c3(%rip), %rdi     # 0x22400
callq 0xbce0
movq %rax, %rcx
movl -0x38(%rbp), %eax
addl 0x8(%rcx), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9d53
movb $0x1, -0x29(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9ba2
jmp 0x9d67
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x9b8c
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)