// File: 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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1680
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x16a0
movq %rax, -0x78(%rbp)
jmp 0x12e8
movq -0x78(%rbp), %rdi
callq 0x16e0
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1700
movl %eax, -0x48(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x132e
movl $0x1, -0x4c(%rbp)
jmp 0x165d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x18e0
jmp 0x166f
leaq -0x30(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x1730
movq -0x80(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x1750
jmp 0x1356
jmp 0x1358
leaq -0x30(%rbp), %rdi
callq 0x17c0
xorb $-0x1, %al
testb $0x1, %al
jne 0x136c
jmp 0x1656
leaq -0x30(%rbp), %rdi
callq 0x1800
movq (%rax), %rax
movq %rax, -0x5c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1840
movl -0x5c(%rbp), %eax
movl %eax, -0x50(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x50(%rbp)
movb %al, -0x81(%rbp)
jl 0x13dd
movq -0x8(%rbp), %rdi
movslq -0x58(%rbp), %rsi
callq 0x16a0
movq %rax, -0x90(%rbp)
jmp 0x13af
movq -0x90(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0x98(%rbp)
jmp 0x13c8
movq -0x98(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
sete %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x13e9
jmp 0x13f4
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x138b
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movb $0x0, -0x52(%rbp)
movb $0x0, -0x51(%rbp)
movl -0x50(%rbp), %ecx
xorl %eax, %eax
cmpl -0x34(%rbp), %ecx
movb %al, -0x99(%rbp)
jge 0x1459
movq -0x8(%rbp), %rdi
movslq -0x58(%rbp), %rsi
callq 0x16a0
movq %rax, -0xa8(%rbp)
jmp 0x142b
movq -0xa8(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0xb0(%rbp)
jmp 0x1444
movq -0xb0(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
sete %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x1468
jmp 0x1651
movl -0x18(%rbp), %eax
movl %eax, -0xbc(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x58(%rbp), %rsi
callq 0x16a0
movq %rax, -0xb8(%rbp)
jmp 0x1487
movq -0xb8(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0xc8(%rbp)
jmp 0x14a0
movq -0xc8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
testb $0x1, -0x51(%rbp)
jne 0x151f
cmpl $0x0, -0x58(%rbp)
jle 0x151f
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x16a0
movq %rax, -0xd0(%rbp)
jmp 0x14d5
movq -0xd0(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0xd8(%rbp)
jmp 0x14ee
movq -0xd8(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x151f
movl -0x50(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x58(%rbp), %eax
decl %eax
movl %eax, -0x60(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0x18b0
jmp 0x1519
movb $0x1, -0x51(%rbp)
jmp 0x1572
testb $0x1, -0x51(%rbp)
je 0x1570
cmpl $0x0, -0x58(%rbp)
jle 0x1570
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x16a0
movq %rax, -0xe0(%rbp)
jmp 0x1545
movq -0xe0(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0xe8(%rbp)
jmp 0x155e
movq -0xe8(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
je 0x1570
movb $0x0, -0x51(%rbp)
jmp 0x1572
testb $0x1, -0x52(%rbp)
jne 0x15e9
movl -0x58(%rbp), %eax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x15e9
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x16a0
movq %rax, -0xf0(%rbp)
jmp 0x159f
movq -0xf0(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0xf8(%rbp)
jmp 0x15b8
movq -0xf8(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x15e9
movl -0x50(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x58(%rbp), %eax
incl %eax
movl %eax, -0x68(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
callq 0x18b0
jmp 0x15e3
movb $0x1, -0x52(%rbp)
jmp 0x1643
testb $0x1, -0x52(%rbp)
je 0x1641
movl -0x58(%rbp), %eax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1641
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x16a0
movq %rax, -0x100(%rbp)
jmp 0x1616
movq -0x100(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x1870
movq %rax, -0x108(%rbp)
jmp 0x162f
movq -0x108(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
je 0x1641
movb $0x0, -0x52(%rbp)
jmp 0x1643
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1405
jmp 0x1358
movl $0x0, -0x4c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x18e0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x1120
nopl (%rax,%rax)