// File: kdungs[P]codecon/src/maze.cc
// Function: solveBfs(Maze const&, Point const&, Point const&)
auto solveBfs(const Maze& maze, const Point& from, const Point& to)
    -> std::vector<Point> {
  auto visited = std::vector<bool>(maze.rows * maze.cols, false);
  auto prev = std::vector<Point>(maze.rows * maze.cols, {maze.rows, maze.cols});

  auto getAddress =
      [&maze](const Point& p) { return p.row * maze.cols + p.col; };

  auto getNeighbours = [&maze, &visited, &getAddress](const Point& p) {
    auto ns = std::vector<Point>{{p.row - 1, p.col},
                                 {p.row + 1, p.col},
                                 {p.row, p.col - 1},
                                 {p.row, p.col + 1}};
    ns.erase(std::remove_if(std::begin(ns), std::end(ns),
                            [&maze, &visited, &getAddress](const Point& np) {
                              const auto addr = getAddress(np);
                              return (np.row >= maze.rows) ||
                                     (np.col >= maze.cols) || !maze.map[addr] ||
                                     visited[addr];
                            }),
             std::end(ns));
    return ns;
  };

  auto queue = std::vector<Point>{};
  queue.emplace_back(from);

  while (!queue.empty()) {
    auto cur = queue.front();
    queue.erase(std::begin(queue));
    visited[getAddress(cur)] = true;

    if (cur == to) {
      break;
    }

    auto ns = getNeighbours(cur);
    for (const auto& n : ns) {
      prev[getAddress(n)] = cur;
      queue.emplace_back(n);
    }
  }

  auto path = std::vector<Point>{};
  for (auto cur = to; cur != from; cur = prev[getAddress(cur)]) {
    path.emplace_back(cur);
  }
  path.emplace_back(from);
  std::reverse(std::begin(path), std::end(path));
  return path;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x188(%rbp)
movq %rdi, %rax
movq %rax, -0x180(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
imulq %rcx, %rax
movq %rax, -0x178(%rbp)
movb $0x0, -0x49(%rbp)
leaq -0x4a(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x2320
movq -0x178(%rbp), %rsi
movq -0x170(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq -0x49(%rbp), %rdx
callq 0x2340
jmp 0x1363
leaq -0x4a(%rbp), %rdi
callq 0x23c0
movq -0x10(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
movq %rax, %rcx
imulq %rdx, %rcx
movq %rcx, -0x198(%rbp)
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x23e0
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rcx
leaq -0x78(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x2400
jmp 0x13cb
leaq -0x89(%rbp), %rdi
callq 0x2480
movq -0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movq $0x0, -0xc0(%rbp)
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x24a0
movq -0x1a0(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x24c0
jmp 0x1440
jmp 0x1442
leaq -0xd0(%rbp), %rdi
callq 0x2540
xorb $-0x1, %al
testb $0x1, %al
jne 0x1459
jmp 0x16ad
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x2580
movq -0x1b0(%rbp), %rdi
movups (%rax), %xmm0
movaps %xmm0, -0xe0(%rbp)
callq 0x2620
movq %rax, -0x1a8(%rbp)
jmp 0x148b
movq -0x1a8(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x2650
movq -0xe8(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x25b0
movq %rax, -0x1b8(%rbp)
jmp 0x14c8
movq -0x1b8(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x1860
movq %rax, -0x1c0(%rbp)
jmp 0x14f2
movq -0x1c0(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2690
movq %rdx, -0x1d0(%rbp)
movq %rax, -0x1c8(%rbp)
jmp 0x1512
movq -0x1d0(%rbp), %rax
movq -0x1c8(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movq %rax, -0x100(%rbp)
leaq -0x108(%rbp), %rdi
movl $0x1, %esi
callq 0x26f0
movq -0x20(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x2740
testb $0x1, %al
jne 0x1555
jmp 0x15a2
jmp 0x16ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x4a(%rbp), %rdi
callq 0x23c0
jmp 0x184e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x2480
jmp 0x1845
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x1830
leaq -0x120(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x1890
jmp 0x15be
leaq -0x120(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdi
callq 0x2790
movq %rax, -0x130(%rbp)
movq -0x128(%rbp), %rdi
callq 0x27c0
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x27f0
testb $0x1, %al
jne 0x160e
jmp 0x169c
leaq -0x130(%rbp), %rdi
callq 0x2830
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x1860
movq %rax, -0x1d8(%rbp)
jmp 0x163d
movq -0x1d8(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x2850
movaps -0xe0(%rbp), %xmm0
movups %xmm0, (%rax)
movq -0x140(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x24c0
jmp 0x166c
jmp 0x166e
leaq -0x130(%rbp), %rdi
callq 0x2870
jmp 0x15f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x2890
jmp 0x1830
leaq -0x120(%rbp), %rdi
callq 0x2890
jmp 0x1442
movq -0x188(%rbp), %rdi
movb $0x0, -0x141(%rbp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x1080
movq -0x188(%rbp), %rdi
callq 0x24a0
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x158(%rbp), %rdi
callq 0x28f0
testb $0x1, %al
jne 0x1702
jmp 0x1779
movq -0x188(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x2920
jmp 0x1717
jmp 0x1719
leaq -0x98(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x1860
movq %rax, -0x1e0(%rbp)
jmp 0x1735
movq -0x1e0(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x2850
movq (%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x150(%rbp)
jmp 0x16ec
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
callq 0x2890
jmp 0x1830
movq -0x188(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x24c0
jmp 0x178b
movq -0x188(%rbp), %rdi
callq 0x2620
movq %rax, -0x1e8(%rbp)
jmp 0x17a0
movq -0x188(%rbp), %rdi
movq -0x1e8(%rbp), %rax
movq %rax, -0x160(%rbp)
callq 0x29e0
movq %rax, -0x1f0(%rbp)
jmp 0x17c3
movq -0x1f0(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rdi
movq -0x168(%rbp), %rsi
callq 0x29a0
jmp 0x17e6
movb $0x1, -0x141(%rbp)
testb $0x1, -0x141(%rbp)
jne 0x1802
movq -0x188(%rbp), %rdi
callq 0x2890
leaq -0xd0(%rbp), %rdi
callq 0x2890
leaq -0x78(%rbp), %rdi
callq 0x2890
leaq -0x48(%rbp), %rdi
callq 0x2a10
movq -0x180(%rbp), %rax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
leaq -0xd0(%rbp), %rdi
callq 0x2890
leaq -0x78(%rbp), %rdi
callq 0x2890
leaq -0x48(%rbp), %rdi
callq 0x2a10
movq -0x58(%rbp), %rdi
callq 0x1150
nopw (%rax,%rax)