// File: 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:
subq $0x198, %rsp            # imm = 0x198
movq 0x1b0(%rsp), %rax
movq 0x1a8(%rsp), %rax
movq 0x1a0(%rsp), %rax
movq %rdi, 0x190(%rsp)
movl %esi, 0x18c(%rsp)
movl %edx, 0x188(%rsp)
movq %rcx, 0x180(%rsp)
movl %r8d, 0x17c(%rsp)
movl %r9d, 0x178(%rsp)
movb $0x1, %al
cmpl $0x0, 0x17c(%rsp)
movb %al, 0x57(%rsp)
jl 0x39c9
movl 0x17c(%rsp), %ecx
movb $0x1, %al
cmpl 0x18c(%rsp), %ecx
movb %al, 0x57(%rsp)
jge 0x39c9
movb $0x1, %al
cmpl $0x0, 0x178(%rsp)
movb %al, 0x57(%rsp)
jl 0x39c9
movl 0x178(%rsp), %ecx
movb $0x1, %al
cmpl 0x188(%rsp), %ecx
movb %al, 0x57(%rsp)
jge 0x39c9
movq 0x1a0(%rsp), %rdi
movslq 0x17c(%rsp), %rsi
callq 0x5710
movq %rax, %rdi
movslq 0x178(%rsp), %rsi
callq 0x5730
movq %rax, 0x168(%rsp)
movq %rdx, 0x170(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x5790
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, 0x57(%rsp)
jne 0x39c9
movq 0x180(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x190(%rsp), %rdi
movslq 0x17c(%rsp), %rsi
callq 0x57c0
movq %rax, %rdi
movslq 0x178(%rsp), %rsi
callq 0x32b0
movq %rax, %rcx
movq 0x48(%rsp), %rax
movsbl (%rcx), %ecx
subl $0x61, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
setne %al
xorb $-0x1, %al
movb %al, 0x57(%rsp)
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x39d3
jmp 0x39d8
jmp 0x3e6d
movq 0x1a0(%rsp), %rdi
movslq 0x17c(%rsp), %rsi
callq 0x5710
movq %rax, %rdi
movslq 0x178(%rsp), %rsi
callq 0x5730
movq %rax, 0x158(%rsp)
movq %rdx, 0x160(%rsp)
leaq 0x158(%rsp), %rdi
movl $0x1, %esi
callq 0x57e0
movq 0x1a8(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x17c(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x5860
movq 0x38(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x5830
movq 0x180(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x190(%rsp), %rdi
movslq 0x17c(%rsp), %rsi
callq 0x57c0
movq %rax, %rdi
movslq 0x178(%rsp), %rsi
callq 0x32b0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movsbl (%rcx), %ecx
subl $0x61, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x148(%rsp)
movq 0x148(%rsp), %rax
testb $0x1, 0xd0(%rax)
je 0x3c59
leaq 0x128(%rsp), %rdi
callq 0x3350
movq 0x1a8(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x5890
movq %rax, 0x118(%rsp)
movq 0x120(%rsp), %rdi
callq 0x58c0
movq %rax, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x58f0
testb $0x1, %al
jne 0x3b24
jmp 0x3bca
leaq 0x118(%rsp), %rdi
callq 0x5930
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rdi
callq 0x5940
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rdi
callq 0x5950
movq %rax, 0xf8(%rsp)
movq 0x190(%rsp), %rdi
movq 0x100(%rsp), %rax
movslq (%rax), %rsi
callq 0x57c0
movq %rax, %rdi
movq 0xf8(%rsp), %rax
movslq (%rax), %rsi
callq 0x32b0
movsbl (%rax), %esi
leaq 0x128(%rsp), %rdi
callq 0x3170
jmp 0x3ba0
jmp 0x3ba2
leaq 0x118(%rsp), %rdi
callq 0x5960
jmp 0x3b06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x3c47
movq 0x1b0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x1a8(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x59b0
jmp 0x3bf6
movq 0x30(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x5980
jmp 0x3c0a
leaq 0xb0(%rsp), %rdi
callq 0x5a20
leaq 0x128(%rsp), %rdi
callq 0x3160
jmp 0x3c59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5a20
leaq 0x128(%rsp), %rdi
callq 0x3160
jmp 0x3e75
leaq 0x14600(%rip), %rax     # 0x18260
movq %rax, 0xa8(%rsp)
leaq 0x145f1(%rip), %rdi     # 0x18260
callq 0x5890
movq %rax, 0xa0(%rsp)
leaq 0x145dd(%rip), %rdi     # 0x18260
callq 0x58c0
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x58f0
testb $0x1, %al
jne 0x3cae
jmp 0x3e25
leaq 0xa0(%rsp), %rdi
callq 0x5930
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rdi
callq 0x5940
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rdi
callq 0x5950
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rax
movl (%rax), %ecx
movl %ecx, %eax
negl %eax
cmovnsl %eax, %ecx
movq 0x80(%rsp), %rax
movl (%rax), %edx
movl %edx, %eax
negl %eax
cmovnsl %eax, %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, 0x2f(%rsp)
jne 0x3d98
movq 0x1a8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x17c(%rsp), %eax
movq 0x88(%rsp), %rcx
addl (%rcx), %eax
movl %eax, 0x74(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x74(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x5a50
movl 0x178(%rsp), %eax
movq 0x80(%rsp), %rcx
addl (%rcx), %eax
movl %eax, 0x68(%rsp)
leaq 0x6c(%rsp), %rdi
leaq 0x17c(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x5a80
movq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x6c(%rsp), %rdx
callq 0x3790
xorb $-0x1, %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x3da2
jmp 0x3e11
movq 0x190(%rsp), %rdi
movl 0x18c(%rsp), %esi
movl 0x188(%rsp), %edx
movq 0x148(%rsp), %rcx
movl 0x17c(%rsp), %r8d
movq 0x88(%rsp), %rax
addl (%rax), %r8d
movl 0x178(%rsp), %r9d
movq 0x80(%rsp), %rax
addl (%rax), %r9d
movq 0x1a0(%rsp), %r11
movq 0x1a8(%rsp), %r10
movq 0x1b0(%rsp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x3880
jmp 0x3e13
leaq 0xa0(%rsp), %rdi
callq 0x5960
jmp 0x3c90
movq 0x1a8(%rsp), %rdi
callq 0x5ab0
movq 0x1a0(%rsp), %rdi
movslq 0x17c(%rsp), %rsi
callq 0x5710
movq %rax, %rdi
movslq 0x178(%rsp), %rsi
callq 0x5730
movq %rax, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x57e0
addq $0x198, %rsp            # imm = 0x198
retq
movq 0xf0(%rsp), %rdi
callq 0x33f0
nopw %cs:(%rax,%rax)