// File: /workspace/github_cpp_makefile_star2_O2/hansfilipelo[P]floodfill/src/Utils.cpp
// Function: Utils::flood_fill(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>*, int, int, int, int)
void Utils::flood_fill(vector< vector<int> >* map, int start_x, int start_y, int old_color, int new_color)
{
    vector<coord_t> stack;
    int width = map->at(0).size();
    int height = map->size();

    if(old_color == new_color) return;
    stack.clear();

    int curr_x;
    bool traverse_above, traverse_below;
    coord_t current_coord = {start_x, start_y};
    
    // Push the start position on stack
    stack.push_back(current_coord);

    while(!stack.empty())
    {
        current_coord = stack.back();
        stack.pop_back();

        curr_x = current_coord.x;
        
        // Start "from left" in memory so that all writes occour in order
        while(curr_x >= 0 && map->at(current_coord.y).at(curr_x) == old_color) curr_x--;
        
        curr_x++;
        traverse_above = traverse_below = false;
        
        while(curr_x < width && map->at(current_coord.y).at(curr_x) == old_color )
        {
            map->at(current_coord.y).at(curr_x) = new_color;
            
            if(!traverse_above && current_coord.y > 0 && map->at(current_coord.y - 1).at(curr_x) == old_color)
            {
                stack.push_back({curr_x, current_coord.y - 1});
                traverse_above = true;
            }
            else if(traverse_above && current_coord.y > 0 && map->at(current_coord.y - 1).at(curr_x) != old_color)
            {
                traverse_above = false;
            }
            if(!traverse_below && current_coord.y < height - 1 && map->at(current_coord.y + 1).at(curr_x) == old_color)
            {   
                stack.push_back({curr_x, current_coord.y + 1});
                traverse_below = true;
            }
            else if(traverse_below && current_coord.y < height - 1 && map->at(current_coord.y + 1).at(curr_x) != old_color)
            {
                traverse_below = false;
            }
            curr_x++;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, %ebx
movl %ecx, %r14d
movl %edx, %r12d
movl %esi, %r13d
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
xorl %esi, %esi
callq 0x14d0
movq %rax, %rcx
movq 0x8(%r15), %rax
subq (%r15), %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq %rax, -0x40(%rbp)
cmpl %ebx, %r14d
jne 0x1302
leaq -0x60(%rbp), %rdi
callq 0x1542
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, -0x64(%rbp)
movq 0x8(%rcx), %rbx
subq (%rcx), %rbx
movq -0x60(%rbp), %rax
cmpq %rax, -0x58(%rbp)
je 0x131a
movq %rax, -0x58(%rbp)
leaq -0x30(%rbp), %rsi
movl %r13d, (%rsi)
movl %r12d, 0x4(%rsi)
leaq -0x60(%rbp), %rdi
callq 0x14ee
movq -0x40(%rbp), %rax
decl %eax
movq %rax, -0x40(%rbp)
shrq $0x2, %rbx
movslq %ebx, %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
cmpq %rax, -0x60(%rbp)
je 0x12ea
movq -0x8(%rax), %rcx
addq $-0x8, %rax
movq %rcx, -0x30(%rbp)
movq %rax, -0x58(%rbp)
movslq %ecx, %rbx
leaq 0x1(%rbx), %r13
testl %ebx, %ebx
js 0x138b
movslq -0x2c(%rbp), %rsi
movq %r15, %rdi
callq 0x14d0
movl %ebx, %esi
movq %rax, %rdi
callq 0x1512
cmpl %r14d, (%rax)
jne 0x138b
decq %rbx
decq %r13
jmp 0x1364
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq -0x70(%rbp), %r13
jge 0x1343
movslq -0x2c(%rbp), %rsi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
cmpl %r14d, (%rax)
jne 0x1343
movslq -0x2c(%rbp), %rsi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
movl -0x64(%rbp), %ecx
movl %ecx, (%rax)
movl -0x2c(%rbp), %esi
testl %esi, %esi
setle %al
orb %r12b, %al
testb $0x1, %al
jne 0x1415
decl %esi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
movl -0x2c(%rbp), %esi
cmpl %r14d, (%rax)
jne 0x1415
movl %r13d, -0x38(%rbp)
decl %esi
movl %esi, -0x34(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x172a
movb $0x1, %r12b
jmp 0x143d
testl %esi, %esi
setg %al
andb %r12b, %al
cmpb $0x1, %al
jne 0x143d
decl %esi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
cmpl %r14d, (%rax)
sete %r12b
movslq -0x2c(%rbp), %rsi
testb $0x1, %bl
je 0x146b
movb $0x1, %bl
cmpl -0x40(%rbp), %esi
jge 0x14aa
incq %rsi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
cmpl %r14d, (%rax)
sete %bl
jmp 0x14aa
cmpl -0x40(%rbp), %esi
jge 0x14a8
incq %rsi
movq %r15, %rdi
callq 0x14d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x1512
cmpl %r14d, (%rax)
jne 0x14a8
movl %r13d, -0x38(%rbp)
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x34(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x172a
movb $0x1, %bl
jmp 0x14aa
xorl %ebx, %ebx
incq %r13
jmp 0x1390
jmp 0x14bc
jmp 0x14bc
jmp 0x14bc
jmp 0x14bc
jmp 0x14bc
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x1542
movq %rbx, %rdi
callq 0x1120