// File: /workspace/github_cpp_makefile_star2_O3/kaiming[P]Chamber-Crawler-3k/Board/board.cc
// Function: Board::validDest(std::shared_ptr<WalkableTile>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::shared_ptr<WalkableTile> Board::validDest(std::shared_ptr<WalkableTile> centre, std::string direction) {
    
    // Set up variable to hold destination
    std::shared_ptr<Tile> destination;

    // Coordinate system with (0,0) in the top left
    //   first int is x value, increases left to right
    //   second int is y value, increases top to bottom

    // std::pair<int x, int y>
    if (direction == "no") {
        // no = North/Up
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first];
    } else if (direction == "so") {
        // so = South/Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first];
    } else if (direction == "ea") {
        // ea = East/Right
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second][current.first + 1];
    } else if (direction == "we") {
        // we = West/Left
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second][current.first - 1];
    } else if (direction == "ne") { // ----------------------------------------------------------------------
        // ne = North East/Right and Up
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size()) || current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first + 1];
    } else if (direction == "nw") {
        // nw = North West/Left and Up 
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0 || current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first - 1];
    } else if (direction == "se") {
        // se = South East/Right and Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size()) || current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first + 1];
    } else if (direction == "sw") {
        // sw = South West/Left and Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0 || current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first - 1];
    } else {
        // Invalid direction
        return nullptr;
    }

    // Check destination tile to see if it's a WalkableTile
    std::shared_ptr<WalkableTile> typeCheck = std::dynamic_pointer_cast<WalkableTile>(destination);
    if (typeCheck) {
        // Destination is a valid, WalkableTile 
        return typeCheck;        
    }

    return nullptr;

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0xca7c(%rip), %rsi      # 0x1b301
movq %rcx, %rdi
callq 0x50a0
testl %eax, %eax
je 0xe98c
leaq 0xca68(%rip), %rsi      # 0x1b304
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xe9bf
leaq 0xca54(%rip), %rsi      # 0x1b307
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xea1b
leaq 0xca40(%rip), %rsi      # 0x1b30a
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xea5a
leaq 0xca2c(%rip), %rsi      # 0x1b30d
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xeaf7
leaq 0xca18(%rip), %rsi      # 0x1b310
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xeb51
leaq 0xca04(%rip), %rsi      # 0x1b313
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xeb9c
leaq 0xc9f0(%rip), %rsi      # 0x1b316
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
jne 0xebf1
movq (%r15), %rdi
callq 0x1a8d6
testl %eax, %eax
jle 0xebf1
movq %rax, %rcx
shrq $0x20, %rcx
incl %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdi
movq -0x18(%rsi,%rdi,8), %rdx
movq -0x10(%rsi,%rdi,8), %rsi
subq %rdx, %rsi
shrq $0x3, %rsi
imull $0xaaaaaaab, %esi, %esi # imm = 0xAAAAAAAB
cmpl %esi, %ecx
jge 0xebf1
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq %rsi, %rax
movslq %ecx, %rcx
jmp 0xeb8d
movq (%r15), %rdi
callq 0x1a8d6
movq %rax, %rcx
shrq $0x20, %rcx
testl %ecx, %ecx
jle 0xebf1
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
jmp 0xea00
movq (%r15), %rdi
callq 0x1a8d6
movq %rax, %rcx
shrq $0x20, %rcx
incl %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdi
movq -0x18(%rsi,%rdi,8), %rdx
movq -0x10(%rsi,%rdi,8), %rsi
subq %rdx, %rsi
shrq $0x3, %rsi
imull $0xaaaaaaab, %esi, %esi # imm = 0xAAAAAAAB
cmpl %esi, %ecx
jge 0xebf1
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx,8), %rcx
cltq
shlq $0x4, %rax
leaq (%rcx,%rax), %rsi
movq (%rcx,%rax), %rax
jmp 0xea9c
movq (%r15), %rdi
callq 0x1a8d6
leal 0x1(%rax), %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
movq 0x20(%rdx), %rsi
subq 0x18(%rdx), %rsi
shrq $0x4, %rsi
cmpl %esi, %ecx
jge 0xebf1
sarq $0x20, %rax
leaq (%rax,%rax,2), %rax
movslq %ecx, %rcx
jmp 0xea8c
movq (%r15), %rdi
callq 0x1a8d6
testl %eax, %eax
jle 0xebf1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
addl %eax, %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdx
sarq $0x20, %rax
movq -0x18(%rsi,%rdx,8), %rdx
leaq (%rax,%rax,2), %rax
movq (%rdx,%rax,8), %rax
shlq $0x4, %rcx
leaq (%rax,%rcx), %rsi
movq (%rax,%rcx), %rax
movq %rax, (%rsp)
leaq 0x8(%rsp), %r14
addq $0x8, %rsi
movq %r14, %rdi
callq 0xbb5a
movq -0x8(%r14), %rdi
testq %rdi, %rdi
je 0xebf1
leaq 0x1629b(%rip), %rsi     # 0x24d60
leaq 0x159cc(%rip), %rdx     # 0x24498
xorl %ecx, %ecx
callq 0x5230
testq %rax, %rax
je 0xebf1
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0xeb45
movq 0x164d3(%rip), %rdx     # 0x24fc0
cmpb $0x0, (%rdx)
je 0xeb41
incl 0x8(%rcx)
jmp 0xeb45
movq (%r15), %rdi
callq 0x1a8d6
leal 0x1(%rax), %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
movq 0x20(%rdx), %rsi
subq 0x18(%rdx), %rsi
shrq $0x4, %rsi
cmpl %esi, %ecx
jge 0xebf1
shrq $0x20, %rax
testl %eax, %eax
jle 0xebf1
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %esi, %eax
jmp 0xea51
lock
incl 0x8(%rcx)
movq %rax, (%rbx)
movq %rcx, 0x8(%rbx)
jmp 0xebf7
movq (%r15), %rdi
callq 0x1a8d6
testl %eax, %eax
jle 0xebf1
movq %rax, %rcx
shrq $0x20, %rcx
testl %ecx, %ecx
jle 0xebf1
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq %rdx, %rax
addl %edx, %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdx
movq -0x18(%rsi,%rdx,8), %rdx
leaq (%rcx,%rcx,2), %rcx
movl %eax, %eax
movq (%rdx,%rcx,8), %rcx
jmp 0xea0a
movq (%r15), %rdi
callq 0x1a8d6
leal 0x1(%rax), %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
leaq (%rdx,%rdx,2), %rdi
movq -0x18(%rsi,%rdi,8), %rdx
movq 0x20(%rdx), %r8
subq 0x18(%rdx), %r8
shrq $0x4, %r8
cmpl %r8d, %ecx
jge 0xebf1
leaq (%rsi,%rdi,8), %rsi
shrq $0x20, %rax
incl %eax
movq -0x10(%rsi), %rsi
subq %rdx, %rsi
shrq $0x3, %rsi
imull $0xaaaaaaab, %esi, %esi # imm = 0xAAAAAAAB
cmpl %esi, %eax
jge 0xebf1
cltq
jmp 0xea51
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xec06
callq 0x7828
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0xec23
jmp 0xec23
jmp 0xec23
jmp 0xec23
jmp 0xec23
jmp 0xec23
jmp 0xec23
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xec35
callq 0x7828
movq %rbx, %rdi
callq 0x5380
nop