// File: /workspace/github_cpp_makefile_star2_O1/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 %r8d, %ebx
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, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rdi
movq %rdi, -0x60(%rbp)
cmpq %rax, %rcx
je 0x168f
cmpl %ebx, %r15d
je 0x1645
movq 0x8(%rax), %r14
subq (%rax), %r14
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x134f
movq %rax, -0x48(%rbp)
movl %esi, -0x30(%rbp)
movl %edx, -0x2c(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x136d
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x137a
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x16ca
movq -0x48(%rbp), %rcx
cmpq %rcx, -0x50(%rbp)
je 0x1645
shrq $0x2, %r14
movq -0x60(%rbp), %rax
decl %eax
movq %rax, -0x60(%rbp)
movq %r14, -0x70(%rbp)
negl %r14d
movq %r14, -0x78(%rbp)
movl %ebx, -0x64(%rbp)
movq -0x8(%rcx), %rax
addq $-0x8, %rcx
movq %rax, -0x30(%rbp)
movq %rcx, -0x48(%rbp)
testl %eax, %eax
js 0x1416
movslq -0x2c(%rbp), %rsi
movq (%r12), %rdi
movq 0x8(%r12), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x1681
leaq (%rsi,%rsi,2), %rdx
movq (%rdi,%rdx,8), %rcx
movq 0x8(%rdi,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movl %eax, %esi
cmpq %rsi, %rdx
jbe 0x1681
cmpl %r15d, (%rcx,%rsi,4)
jne 0x1416
leal -0x1(%rax), %esi
testl %eax, %eax
movl %esi, %eax
jg 0x13f7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leal 0x1(%rax), %ecx
cmpl -0x70(%rbp), %ecx
jge 0x1637
movslq %eax, %rbx
incq %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq -0x2c(%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 0x1681
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 0x1669
cmpl %r15d, (%rcx,%rbx,4)
jne 0x1637
movl -0x64(%rbp), %esi
movl %esi, (%rcx,%rbx,4)
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
setle %sil
orb %r13b, %sil
testb $0x1, %sil
jne 0x14e5
leal -0x1(%rcx), %esi
cmpq %rsi, %rdx
jbe 0x1681
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 0x1676
cmpl %r15d, (%rdi,%rbx,4)
jne 0x14e5
movl %ebx, -0x58(%rbp)
movl %esi, -0x54(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x152f
movq -0x58(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x153c
testl %ecx, %ecx
setg %sil
andb %r13b, %sil
cmpb $0x1, %sil
jne 0x153f
decl %ecx
cmpq %rcx, %rdx
jbe 0x167e
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 0x1671
cmpl %r15d, (%rcx,%rbx,4)
movzbl %r13b, %r13d
movl $0x0, %eax
cmovnel %eax, %r13d
jmp 0x153f
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x180a
movb $0x1, %r13b
movslq -0x2c(%rbp), %rsi
testb $0x1, %r14b
je 0x15ac
cmpl -0x60(%rbp), %esi
jge 0x1628
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 0x1681
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 0x1671
cmpl %r15d, (%rcx,%rbx,4)
movzbl %r14b, %r14d
movl $0x0, %eax
cmovnel %eax, %r14d
jmp 0x1628
cmpl -0x60(%rbp), %esi
jge 0x1628
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 0x1681
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 0x1671
cmpl %r15d, (%rcx,%rbx,4)
jne 0x1628
movl %ebx, -0x58(%rbp)
movl %esi, -0x54(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x1618
movq -0x58(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x1625
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x180a
movb $0x1, %r14b
incq %rbx
movq -0x78(%rbp), %rax
addl %ebx, %eax
jne 0x142e
movq -0x48(%rbp), %rcx
cmpq %rcx, -0x50(%rbp)
jne 0x13a4
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x165a
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 0x1681
movq %rbx, %rsi
jmp 0x1681
movq %rbx, %rsi
movq %r8, %rdx
jmp 0x1681
movq %rcx, %rsi
leaq 0x197c(%rip), %rdi      # 0x3004
xorl %eax, %eax
callq 0x10e0
leaq 0x196e(%rip), %rdi      # 0x3004
xorl %esi, %esi
movq -0x60(%rbp), %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x16a9
jmp 0x16a9
jmp 0x16a9
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x16c1
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x10c0
movq %rbx, %rdi
callq 0x1120
nop