// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %ecx, %r15d
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movq %rcx, %rdi
subq %rax, %rdi
sarq $0x3, %rdi
movabsq $-0x5555555555555555, %r9 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r9, %rdi
movq %rdi, -0x58(%rbp)
cmpq %rax, %rcx
je 0x1671
cmpl %r8d, %r15d
je 0x163c
movl %r8d, -0x64(%rbp)
movq 0x8(%rax), %rbx
subq (%rax), %rbx
leaq -0x60(%rbp), %rax
movl %esi, (%rax)
movl %edx, 0x4(%rax)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x16ac
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x162b
shrq $0x2, %rbx
movq -0x58(%rbp), %rcx
decl %ecx
movq %rcx, -0x58(%rbp)
movl %ebx, %ecx
negl %ecx
movq %rcx, -0x78(%rbp)
movq %rbx, -0x70(%rbp)
movq -0x8(%rax), %rcx
addq $-0x8, %rax
movq %rcx, -0x60(%rbp)
movq %rax, -0x48(%rbp)
testl %ecx, %ecx
js 0x13f9
movq %rcx, %rsi
sarq $0x20, %rsi
movq (%r12), %r9
movq 0x8(%r12), %rdx
subq %r9, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rdx
cmpq %rsi, %rdx
jbe 0x1663
leaq (%rsi,%rsi,2), %rdx
movq (%r9,%rdx,8), %r8
movq 0x8(%r9,%rdx,8), %rdx
subq %r8, %rdx
sarq $0x2, %rdx
movl %ecx, %esi
cmpq %rsi, %rdx
jbe 0x1663
cmpl %r15d, (%r8,%rsi,4)
jne 0x13f9
leal -0x1(%rcx), %esi
testl %ecx, %ecx
movl %esi, %ecx
jg 0x13da
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leal 0x1(%rcx), %edx
cmpl %ebx, %edx
jge 0x1622
movslq %ecx, %rbx
incq %rbx
xorl %r13d, %r13d
xorl %r14d, %r14d
movslq -0x5c(%rbp), %rsi
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x1663
leaq (%rsi,%rsi,2), %rsi
movq (%rax,%rsi,8), %rcx
movq 0x8(%rax,%rsi,8), %rdi
subq %rcx, %rdi
sarq $0x2, %rdi
cmpq %rbx, %rdi
jbe 0x164b
cmpl %r15d, (%rcx,%rbx,4)
jne 0x1616
movl -0x64(%rbp), %esi
movl %esi, (%rcx,%rbx,4)
movl -0x5c(%rbp), %ecx
testl %ecx, %ecx
setle %sil
orb %r14b, %sil
testb $0x1, %sil
jne 0x14c7
leal -0x1(%rcx), %esi
cmpq %rsi, %rdx
jbe 0x1663
leaq (%rsi,%rsi,2), %r8
movq (%rax,%r8,8), %rdi
movq 0x8(%rax,%r8,8), %r8
subq %rdi, %r8
sarq $0x2, %r8
cmpq %rbx, %r8
jbe 0x1658
cmpl %r15d, (%rdi,%rbx,4)
jne 0x14c7
movl %ebx, -0x30(%rbp)
movl %esi, -0x2c(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x1508
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x1515
testl %ecx, %ecx
setg %sil
andb %r14b, %sil
cmpb $0x1, %sil
jne 0x1518
decl %ecx
cmpq %rcx, %rdx
jbe 0x1660
leaq (%rcx,%rcx,2), %rdx
movq (%rax,%rdx,8), %rcx
movq 0x8(%rax,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x1653
cmpl %r15d, (%rcx,%rbx,4)
sete %r14b
jmp 0x1518
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x17ec
movb $0x1, %r14b
movslq -0x5c(%rbp), %rsi
testb $0x1, %r13b
je 0x1582
movb $0x1, %r13b
cmpl -0x58(%rbp), %esi
jge 0x1607
incq %rsi
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x1663
leaq (%rsi,%rsi,2), %rdx
movq (%rax,%rdx,8), %rcx
movq 0x8(%rax,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x1653
cmpl %r15d, (%rcx,%rbx,4)
sete %r13b
jmp 0x1607
cmpl -0x58(%rbp), %esi
jge 0x15f2
incq %rsi
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x1663
leaq (%rsi,%rsi,2), %rdx
movq (%rax,%rdx,8), %rcx
movq 0x8(%rax,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x1653
cmpl %r15d, (%rcx,%rbx,4)
jne 0x15f2
movl %ebx, -0x30(%rbp)
movl %esi, -0x2c(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x15f7
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x1604
xorl %r13d, %r13d
jmp 0x1607
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x17ec
movb $0x1, %r13b
incq %rbx
movq -0x78(%rbp), %rax
addl %ebx, %eax
jne 0x1410
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rbx
cmpq %rax, %rdi
jne 0x1384
testq %rdi, %rdi
je 0x163c
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x10c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rsi
movq %rdi, %rdx
jmp 0x1663
movq %rbx, %rsi
jmp 0x1663
movq %rbx, %rsi
movq %r8, %rdx
jmp 0x1663
movq %rcx, %rsi
leaq 0x199a(%rip), %rdi      # 0x3004
xorl %eax, %eax
callq 0x10e0
leaq 0x198c(%rip), %rdi      # 0x3004
xorl %esi, %esi
movq -0x58(%rbp), %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x168b
jmp 0x168b
jmp 0x168b
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x16a3
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x10c0
movq %rbx, %rdi
callq 0x1120
nop