// File: /workspace/github_cpp_makefile_star2_O2/razimantv[P]DancingLinks/spangram/spangram.cpp
// Function: find_placements(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, int, int, TrieNode const&, int, int, std::vector<std::vector<bool, std::allocator<bool>>, std::allocator<std::vector<bool, std::allocator<bool>>>>&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>&, std::vector<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>>>>&)
void find_placements(const Grid& grid, int m, int n, const TrieNode& node,
                     int i, int j, std::vector<std::vector<bool>>& visited,
                     Path& path, std::vector<Placement>& placements) {
  // Check if the current cell is valid and can extend the current word
  if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] ||
      !node.children[grid[i][j] - 'a'])
    return;

  // Process current cell
  visited[i][j] = true;
  path.push_back({i, j});
  const TrieNode* next = node.children[grid[i][j] - 'a'];
  if (next->end) {
    // If we are at a word end, we can have a placement here
    // Find the corresponding word and placement
    std::string word;
    for (const auto& [x, y] : path) word += grid[x][y];
    placements.push_back({word, path});
  }
  // Recurse to all neighbours
  for (const auto& [dx, dy] : neighbours)
    // If the step is diagonal, ensure it won't cause self-intersection
    if (abs(dx) != abs(dy) or !contains_pair(path, {i + dx, j}, {i, j + dy}))
      find_placements(grid, m, n, *next, i + dx, j + dy, visited, path,
                      placements);

  // Undo to backtrack
  path.pop_back();
  visited[i][j] = false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
testl %r8d, %r8d
js 0x275b
movl %r9d, %ebx
cmpl 0xc(%rsp), %r9d
jge 0x275b
movl %r8d, %ebp
cmpl 0x8(%rsp), %r8d
jge 0x275b
testl %ebx, %ebx
js 0x275b
movq %rcx, %r13
movq 0xc0(%rsp), %rax
movl %ebp, %r14d
imulq $0x28, %r14, %r15
movq (%rax), %rdi
addq %r15, %rdi
movl %ebx, %r12d
movq %r12, %rsi
callq 0x3174
testq %rdx, (%rax)
jne 0x275b
movq 0x10(%rsp), %rax
movq (%rax), %rax
shlq $0x5, %r14
movq (%rax,%r14), %rax
movsbq (%rax,%r12), %rax
cmpq $0x0, -0x308(%r13,%rax,8)
je 0x275b
movq 0xc0(%rsp), %rax
movq (%rax), %rdi
movq %r15, 0x20(%rsp)
addq %r15, %rdi
movq %r12, %rsi
callq 0x3174
orq %rdx, (%rax)
leaq 0x50(%rsp), %rsi
movl %ebp, (%rsi)
movl %ebx, 0x4(%rsi)
movq 0xc8(%rsp), %rdi
callq 0x3772
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r14), %rax
movq %r12, 0x18(%rsp)
movsbq (%rax,%r12), %rax
movq -0x308(%r13,%rax,8), %rax
movq %rax, 0x28(%rsp)
cmpb $0x1, 0xd0(%rax)
jne 0x2684
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xc8(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r12
leaq 0x30(%rsp), %r15
cmpq %r12, %r14
je 0x2647
movslq (%r14), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
shlq $0x5, %rax
movslq 0x4(%r14), %rdx
movq (%rcx,%rax), %rax
movsbl (%rax,%rdx), %esi
movq %r15, %rdi
callq 0x2150
addq $0x8, %r14
jmp 0x2619
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0xc8(%rsp), %rdx
callq 0x3192
leaq 0x50(%rsp), %rsi
movq 0xd0(%rsp), %rdi
callq 0x38b4
leaq 0x50(%rsp), %rdi
callq 0x31c8
leaq 0x30(%rsp), %rdi
callq 0x20c0
movq 0x7b15(%rip), %r14      # 0xa1a0
movq 0x7b16(%rip), %r12      # 0xa1a8
cmpq %r12, %r14
je 0x272e
movl (%r14), %r15d
movl 0x4(%r14), %r13d
movl %r15d, %eax
negl %eax
cmovsl %r15d, %eax
movl %r13d, %ecx
negl %ecx
cmovsl %r13d, %ecx
addl %ebx, %r13d
addl %ebp, %r15d
cmpl %ecx, %eax
jne 0x26eb
movl %r15d, 0x50(%rsp)
movl %ebx, 0x54(%rsp)
movl %ebp, 0x30(%rsp)
movl %r13d, 0x34(%rsp)
movq 0xc8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x247c
testb %al, %al
jne 0x2725
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movq 0x30(%rsp), %rcx
movl %r15d, %r8d
movl %r13d, %r9d
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x24f4
addq $0x20, %rsp
addq $0x8, %r14
jmp 0x2692
movq 0xc8(%rsp), %rax
addq $-0x8, 0x8(%rax)
movq 0xc0(%rsp), %rax
movq 0x20(%rsp), %rdi
addq (%rax), %rdi
movq 0x18(%rsp), %rsi
callq 0x3174
notq %rdx
andq %rdx, (%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x31c8
jmp 0x2781
jmp 0x277e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x22b0