// File: /workspace/github_cpp_makefile_star2_O1/razimantv[P]DancingLinks/spangram/spangram.cpp
// Function: main
int main(int argc, char* argv[]) {
  std::unordered_set<std::string> exclude; // Words to exclude
  std::string filename = "words.txt";      // Default dictionary file
  int maxsel = -1;                         // Maximum number of words to select

  // Parse command line arguments
  //    Number: Maximum number of words to select
  //    Dot in the argument: Filename
  //    Otherwise: Words to exclude
  for (int i = 1; i < argc; i++) {
    auto cur = std::string(argv[i]);
    if (isdigit(cur[0])) {
      maxsel = std::stoi(cur);
    } else if (cur.find('.') != std::string::npos) {
      filename = cur;
    } else {
      exclude.insert(cur);
    }
  }

  // Create trie from all the words in the dictionary with >= 4 characters
  TrieNode trie;
  {
    std::ifstream dictionary(filename);
    for (std::string word; dictionary >> word;)
      if (word.size() >= 4 and not exclude.count(word)) trie.insert(word);
  }

  // Read the grid from the input file
  Grid grid;
  {
    for (std::string line; std::cin >> line;) {
      std::transform(line.begin(), line.end(), line.begin(),
                     [](unsigned char c) { return std::tolower(c); });
      grid.push_back(line);
    }
  }
  int rows = grid.size(), cols = grid[0].size();

  // Find all placements of words in the grid
  std::vector<std::vector<bool>> visited(rows, std::vector<bool>(cols, false));
  Path path;
  std::vector<Placement> placements;
  for (int i = 0; i < rows; ++i)
    for (int j = 0; j < cols; ++j)
      find_placements(grid, rows, cols, trie, i, j, visited, path, placements);

  // Set up the adjacency matrix for dancing links
  // We want to cover the cells of the grid with words
  // Essential constraints: Every cell is covered by exactly one word
  // Non-essential constraints: The centre of a 2x2 square of characters can
  //    have at most one diagonal line passing through it (paths cannot cross)
  int R = placements.size(), C1 = rows * cols, C2 = (rows - 1) * (cols - 1),
      C = C1 + C2;
  char* mat = new (std::nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);

  // Fill the adjacency matrix with the placements
  for (int i = 0; i < R; ++i) {
    const auto& [word, path] = placements[i];
    int l = path.size();
    for (const auto& [x, y] : path) mat[i * C + x * cols + y] = 1;
    for (int k = 1; k < l; ++k) {
      int x1 = path[k - 1].first, y1 = path[k - 1].second, x2 = path[k].first,
          y2 = path[k].second, dx = x2 - x1, dy = y2 - y1;
      if (abs(dx) == 1 and abs(dy) == 1)
        mat[i * C + C1 + std::min(x1, x2) * (cols - 1) + std::min(y1, y2)] = 1;
    }
  }

  // Now just solve it with dancing links
  DancingLinks D;
  D.Create(R, C1, C2, mat);
  std::vector<std::vector<int>> all;
  std::vector<int> cur;
  D.solve(all, cur, false, maxsel);

  // Print all solutions
  for (auto& sol : all) {
    std::vector<Placement> solution_placements;
    for (int i : sol) solution_placements.push_back(placements[i]);
    print_solution(grid, rows, cols, solution_placements);
    std::cout << "\n";
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, %r12
movl %edi, %ebx
leaq 0x158(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x34a3(%rip), %rsi      # 0x6064
leaq 0x34a5(%rip), %rdx      # 0x606d
leaq 0x70(%rsp), %rdi
callq 0x3f9a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
cmpl $0x2, %ebx
jl 0x2d01
movl %ebx, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
movl $0x1, %r14d
leaq 0x160(%rsp), %r13
movq (%r12,%r14,8), %rsi
movq %r13, %rdi
leaq 0x370(%rsp), %rdx
callq 0x3674
movq 0x160(%rsp), %rbp
movsbl (%rbp), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x2c8c
movq %rbx, %r15
callq 0x2040
movq %rax, %rbx
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movl $0x0, (%rbx)
movq %rbp, %rdi
leaq 0x370(%rsp), %rsi
movl $0xa, %edx
callq 0x21e0
cmpq %rbp, 0x370(%rsp)
je 0x32ba
movslq %eax, %rcx
movq %rax, 0x18(%rsp)
cmpq %rax, %rcx
jne 0x32c6
movl (%rbx), %eax
cmpl $0x22, %eax
je 0x32c6
testl %eax, %eax
jne 0x2c87
movl 0x8(%rsp), %eax
movl %eax, (%rbx)
movq %r15, %rbx
jmp 0x2cd0
movq %r13, %rdi
movl $0x2e, %esi
xorl %edx, %edx
callq 0x21c0
cmpq $-0x1, %rax
je 0x2cb0
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x20c0
jmp 0x2cd0
leaq 0x128(%rsp), %rdi
movq %rdi, 0x370(%rsp)
movq %r13, %rsi
leaq 0x370(%rsp), %rdx
callq 0x402a
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x2cf5
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
incq %r14
cmpq %r14, %rbx
jne 0x2bff
leaq 0x370(%rsp), %rdi
callq 0x4b28
leaq 0x160(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl $0x8, %edx
callq 0x2240
leaq 0x30(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x160(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0x128(%rsp), %r15
leaq 0x370(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2d96
cmpq $0x4, 0x28(%rsp)
jb 0x2d58
movq %r15, %rdi
movq %r14, %rsi
callq 0x444a
testq %rax, %rax
jne 0x2d58
movq %r12, %rdi
movq %r14, %rsi
callq 0x4b70
jmp 0x2d58
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2dad
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x160(%rsp), %r14
movq %r14, %rdi
callq 0x2060
xorl %eax, %eax
leaq 0x20(%rsp), %r15
movq %rax, 0x10(%r15)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
leaq 0x170(%rsp), %rcx
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
movb $0x0, 0x10(%r14)
movq 0x61f6(%rip), %rbx      # 0x8fe0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
movq 0x160(%rsp), %r12
jne 0x2e3d
movq 0x168(%rsp), %r13
testq %r13, %r13
je 0x2e30
xorl %ebp, %ebp
movzbl (%r12,%rbp), %edi
callq 0x2200
movb %al, (%r12,%rbp)
incq %rbp
cmpq %rbp, %r13
jne 0x2e1a
movq %r15, %rdi
movq %r14, %rsi
callq 0x36b2
jmp 0x2dea
leaq 0x170(%rsp), %rax
cmpq %rax, %r12
je 0x2e5d
movq 0x170(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2160
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r14
movq 0x8(%rbx), %r15
movslq %r15d, %rsi
leaq 0x40(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x160(%rsp), %rdi
leaq 0xb8(%rsp), %rcx
callq 0x36f0
subq %rbx, %r14
shrq $0x5, %r14
movq %r14, 0x8(%rsp)
movslq %r14d, %rsi
leaq 0x110(%rsp), %rdi
leaq 0x160(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rdx, %rbp
callq 0x3758
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2eff
movq 0x180(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
xorl %eax, %eax
movq %rax, 0x160(%rsp)
movl %eax, 0x168(%rsp)
movq %rax, 0x170(%rsp)
movl %eax, 0x178(%rsp)
movq %rax, 0x180(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x170(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movq %rbx, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x8(%rsp), %rsi
testl %esi, %esi
jle 0x2f7b
leaq 0x20(%rsp), %r14
leaq 0x370(%rsp), %r12
testl %r15d, %r15d
jle 0x2f75
xorl %r13d, %r13d
subq $0x8, %rsp
movq %r14, %rdi
movl %r15d, %edx
movq %r12, %rcx
movl %ebx, %r8d
movl %r13d, %r9d
leaq 0x48(%rsp), %rax
pushq %rax
pushq %rbp
leaq 0x128(%rsp), %rax
pushq %rax
callq 0x25cc
addq $0x20, %rsp
incl %r13d
cmpl %r13d, %r15d
movq 0x8(%rsp), %rsi
jne 0x2f3c
incl %ebx
cmpl %esi, %ebx
jne 0x2f34
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $0x6db6db6db6db6db7, %r13 # imm = 0x6DB6DB6DB6DB6DB7
imulq %rax, %r13
movl %esi, %eax
imull %r15d, %eax
leal -0x1(%rsi), %ecx
leal -0x1(%r15), %r12d
imull %r12d, %ecx
movq %rax, 0x68(%rsp)
movq %rcx, 0xb0(%rsp)
addl %ecx, %eax
movl %eax, 0x14(%rsp)
imull %r13d, %eax
movslq %eax, %rbx
movq 0x5fec(%rip), %rsi      # 0x8fb8
movq %rbx, %rdi
callq 0x2270
testq %rax, %rax
je 0x321b
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20d0
movq %r13, 0x60(%rsp)
testl %r13d, %r13d
jle 0x30dd
movq 0x60(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
imulq $0x38, %rcx, %rsi
movq 0x20(%r14,%rsi), %rdi
movq 0x28(%r14,%rsi), %r8
movq %r8, %rdx
subq %rdi, %rdx
je 0x3049
movl 0x14(%rsp), %r9d
imull %ecx, %r9d
movl (%rdi), %r10d
imull %r15d, %r10d
addl %r9d, %r10d
movslq 0x4(%rdi), %r11
movslq %r10d, %r10
addq %r11, %r10
movb $0x1, (%rbp,%r10)
addq $0x8, %rdi
cmpq %r8, %rdi
jne 0x3026
shrq $0x3, %rdx
cmpl $0x2, %edx
jl 0x30d1
addq %r14, %rsi
addq $0x20, %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x14(%rsp), %edi
imull %ecx, %edi
addl 0x68(%rsp), %edi
movl $0x1, %r8d
movq (%rsi), %rbx
movl -0x8(%rbx,%r8,8), %r9d
movl (%rbx,%r8,8), %r11d
movl %r11d, %r10d
subl %r9d, %r10d
incl %r10d
testl $0xfffffffd, %r10d      # imm = 0xFFFFFFFD
jne 0x30c9
movl -0x4(%rbx,%r8,8), %r10d
movl 0x4(%rbx,%r8,8), %r13d
movl %r13d, %ebx
subl %r10d, %ebx
incl %ebx
testl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jne 0x30c9
cmpl %r9d, %r11d
cmovll %r11d, %r9d
imull %r12d, %r9d
cmpl %r10d, %r13d
cmovll %r13d, %r10d
addl %edi, %r10d
addl %r9d, %r10d
movslq %r10d, %r9
movb $0x1, (%rbp,%r9)
incq %r8
cmpq %r8, %rdx
jne 0x3070
incq %rcx
cmpq %rax, %rcx
jne 0x3007
leaq 0xb8(%rsp), %rdi
callq 0x4be8
leaq 0xb8(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movq %rbp, %r8
callq 0x4ce0
movq 0x18(%rsp), %r8
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0xd0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0xb8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x501e
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %rbx
cmpq %rbx, %r14
je 0x31e0
leaq 0xf0(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
movq (%r14), %r13
movq 0x8(%r14), %rbp
cmpq %rbp, %r13
je 0x31a2
movslq (%r13), %rax
imulq $0x38, %rax, %rsi
addq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x37f0
addq $0x4, %r13
jmp 0x3182
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl %r15d, %edx
movq %r12, %rcx
callq 0x28b7
movl $0x1, %edx
movq 0x5e0d(%rip), %rdi      # 0x8fd0
leaq 0x2e98(%rip), %rsi      # 0x6062
callq 0x2180
movq %r12, %rdi
callq 0x381c
addq $0x18, %r14
cmpq %rbx, %r14
jne 0x3164
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x31fd
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x90(%rsp), %rdi
callq 0x384c
leaq 0xb8(%rsp), %rdi
callq 0x4c34
xorl %ebx, %ebx
jmp 0x322c
leaq 0x2ec6(%rip), %rdi      # 0x60e8
callq 0x2230
movl $0x1, %ebx
leaq 0x40(%rsp), %rdi
callq 0x381c
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x3253
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x110(%rsp), %rdi
callq 0x389a
leaq 0x20(%rsp), %rdi
callq 0x3618
leaq 0x370(%rsp), %rdi
callq 0x4b34
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3299
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x128(%rsp), %rdi
callq 0x39f8
movl %ebx, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2dd7(%rip), %rdi      # 0x6098
callq 0x20e0
leaq 0x2dcb(%rip), %rdi      # 0x6098
callq 0x21f0
movq %rax, %r14
jmp 0x33df
movq %rax, %r14
jmp 0x3409
jmp 0x3418
movq %rax, %r14
movq 0x160(%rsp), %rsi
testq %rsi, %rsi
je 0x344f
leaq 0x160(%rsp), %rdi
callq 0x22d0
jmp 0x344f
movq %rax, %r14
jmp 0x344f
movq %rax, %r14
jmp 0x3459
jmp 0x332c
movq %rax, %r14
jmp 0x3488
movq %rax, %r14
jmp 0x3342
movq %rax, %r14
jmp 0x3466
movq %rax, %r14
cmpl $0x0, (%rbx)
jne 0x3342
movl 0x8(%rsp), %eax
movl %eax, (%rbx)
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x3466
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x3466
jmp 0x33cf
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x338c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x160(%rsp), %rdi
callq 0x2060
jmp 0x3459
movq %rax, %r14
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x344f
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x344f
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x381c
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x33fc
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x90(%rsp), %rdi
callq 0x384c
leaq 0xb8(%rsp), %rdi
callq 0x4c34
jmp 0x341b
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x381c
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x3442
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
leaq 0x110(%rsp), %rdi
callq 0x389a
leaq 0x20(%rsp), %rdi
callq 0x3618
leaq 0x370(%rsp), %rdi
callq 0x4b34
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x3488
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x128(%rsp), %rdi
callq 0x39f8
movq %r14, %rdi
callq 0x2280
nop