// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0xd727(%rip), %rsi      # 0x1c301
movq %rcx, %rdi
callq 0x50a0
testl %eax, %eax
je 0xece1
leaq 0xd713(%rip), %rsi      # 0x1c304
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xed43
leaq 0xd6ff(%rip), %rsi      # 0x1c307
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xedb0
leaq 0xd6eb(%rip), %rsi      # 0x1c30a
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xee26
leaq 0xd6d7(%rip), %rsi      # 0x1c30d
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xee8d
leaq 0xd6c3(%rip), %rsi      # 0x1c310
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xeed7
leaq 0xd6af(%rip), %rsi      # 0x1c313
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
je 0xef2b
leaq 0xd69b(%rip), %rsi      # 0x1c316
movq %r12, %rdi
callq 0x50a0
testl %eax, %eax
jne 0xeff0
movq (%r15), %rdi
callq 0x1ad96
testl %eax, %eax
jle 0xeff0
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 0xeff0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addq %rsi, %rax
movslq %ecx, %rcx
jmp 0xef13
movq (%r15), %rdi
callq 0x1ad96
movq %rax, %r15
shrq $0x20, %r15
testl %r15d, %r15d
jle 0xee65
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %r15d, %edx
addl %ecx, %edx
movslq 0x108(%r14), %rcx
movq (%r14), %rsi
leaq (%rcx,%rcx,2), %rcx
movq -0x18(%rsi,%rcx,8), %rcx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rcx
cltq
shlq $0x4, %rax
leaq (%rcx,%rax), %rsi
addq $0x8, %rsi
movq -0x8(%rsi), %rax
leaq 0x8(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xbc6e
jmp 0xee6b
movq (%r15), %rdi
callq 0x1ad96
movq %rax, %r15
shrq $0x20, %r15
incl %r15d
movslq 0x108(%r14), %rcx
movq (%r14), %rdx
leaq (%rcx,%rcx,2), %rsi
movq -0x18(%rdx,%rsi,8), %rcx
movq -0x10(%rdx,%rsi,8), %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
imull $0xaaaaaaab, %edx, %ebp # imm = 0xAAAAAAAB
cmpl %ebp, %r15d
jge 0xee12
movslq %r15d, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rcx
cltq
shlq $0x4, %rax
leaq (%rcx,%rax), %rsi
addq $0x8, %rsi
movq -0x8(%rsi), %rax
leaq 0x8(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xbc6e
jmp 0xee18
movq (%r15), %rdi
callq 0x1ad96
leal 0x1(%rax), %ebp
movslq 0x108(%r14), %rcx
movq (%r14), %rdx
leaq (%rcx,%rcx,2), %rcx
movq -0x18(%rdx,%rcx,8), %rcx
movq 0x20(%rcx), %r14
subq 0x18(%rcx), %r14
shrq $0x4, %r14
cmpl %r14d, %ebp
jge 0xee79
sarq $0x20, %rax
leaq (%rax,%rax,2), %rax
movslq %ebp, %rdx
movq (%rcx,%rax,8), %rax
shlq $0x4, %rdx
leaq (%rax,%rdx), %rsi
addq $0x8, %rsi
movq -0x8(%rsi), %rax
leaq 0x8(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xbc6e
jmp 0xee7f
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
cmpl %ebp, %r15d
jl 0xefa8
jmp 0xf01c
movq (%r15), %rdi
callq 0x1ad96
movq %rax, %r15
testl %r15d, %r15d
jle 0xee65
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %r15d, %eax
movslq 0x108(%r14), %rcx
movq (%r14), %rdx
leaq (%rcx,%rcx,2), %rcx
movq %r15, %rsi
sarq $0x20, %rsi
movq -0x18(%rdx,%rcx,8), %rcx
leaq (%rsi,%rsi,2), %rdx
movq (%rcx,%rdx,8), %rcx
jmp 0xed20
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
testl %r15d, %r15d
jg 0xefa8
jmp 0xf01c
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
cmpl %r14d, %ebp
jl 0xefa8
jmp 0xf01c
movq (%r15), %rdi
callq 0x1ad96
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 0xeff0
shrq $0x20, %rax
testl %eax, %eax
jle 0xeff0
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %esi, %eax
jmp 0xef7f
movq (%r15), %rdi
callq 0x1ad96
testl %eax, %eax
jle 0xeff0
movq %rax, %rcx
shrq $0x20, %rcx
testl %ecx, %ecx
jle 0xeff0
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
shlq $0x4, %rax
leaq (%rcx,%rax), %rsi
movq (%rcx,%rax), %rax
jmp 0xef96
movq (%r15), %rdi
callq 0x1ad96
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 0xeff0
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 0xeff0
cltq
leaq (%rax,%rax,2), %rax
movslq %ecx, %rcx
movq (%rdx,%rax,8), %rax
shlq $0x4, %rcx
leaq (%rax,%rcx), %rsi
movq (%rax,%rcx), %rax
movq %rax, (%rsp)
leaq 0x8(%rsp), %rdi
addq $0x8, %rsi
callq 0xbc6e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xefc8
leaq 0x16da8(%rip), %rsi     # 0x25d60
leaq 0x164d9(%rip), %rdx     # 0x25498
xorl %ecx, %ecx
callq 0x5240
jmp 0xefca
xorl %eax, %eax
testq %rax, %rax
je 0xefea
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xeffc
movq 0x16fe0(%rip), %rcx     # 0x25fc0
cmpb $0x0, (%rcx)
je 0xeff8
incl 0x8(%rdi)
jmp 0xeffc
xorl %eax, %eax
xorl %edi, %edi
jmp 0xeffc
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
jmp 0xf01c
lock
incl 0x8(%rdi)
testq %rax, %rax
je 0xf00c
movq %rax, (%rbx)
movq %rdi, 0x8(%rbx)
xorl %edi, %edi
jmp 0xf012
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
testq %rdi, %rdi
je 0xf01c
callq 0x78e4
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf02b
callq 0x78e4
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf049
jmp 0xf049
jmp 0xf049
jmp 0xf049
jmp 0xf049
jmp 0xf049
jmp 0xf049
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xf05b
callq 0x78e4
movq %rbx, %rdi
callq 0x5390
nop