// File: ArashPartow[P]exprtk-extras/exprtk_nqueens_problem.cpp
// Function: void sudoku_solver<double>()
void sudoku_solver()
{
   typedef exprtk::symbol_table<T>         symbol_table_t;
   typedef exprtk::expression<T>           expression_t;
   typedef exprtk::parser<T>               parser_t;
   typedef exprtk::function_compositor<T>  compositor_t;
   typedef typename compositor_t::function function_t;

   const T n = T(15);
   std::vector<T> board(static_cast<std::size_t>(n), T(- 1));

   exprtk::rtl::io::package<T> io_package;

   symbol_table_t symbol_table;
   symbol_table.add_constant("n"     , n    );
   symbol_table.add_vector  ("board" , board);
   symbol_table.add_package (io_package);

   compositor_t compositor(symbol_table);

   compositor.load_variables(true);
   compositor.load_vectors  (true);

   compositor.add(
      function_t("display_board")
      .expression
      (
         " for (var row := 0; row < n; row += 1)             "
         " {                                                 "
         "    for (var col := 0; col < n; col += 1)          "
         "    {                                              "
         "       print((board[row] == col) ? ' Q ' : ' . '); "
         "    };                                             "
         "                                                   "
         "    println('');                                   "
         " }                                                 "
      ));

   compositor.add(
      function_t("is_valid")
      .vars("row", "col")
      .expression
      (
         " for (var i := 0; i < row; i += 1)             "
         " {                                             "
         "    if (                                       "
         "         (board[i] == col) or                  "
         "         (abs(board[i] - col) == abs(i - row)) "
         "       )                                       "
         "    {                                          "
         "       return [false];                         "
         "    }                                          "
         " };                                            "
         "                                               "
         " return [true];                                "
      ));

   compositor.add(
      function_t("solve_nqueens")
      .var("row")
      .expression
      (
         " if (row == n)                         "
         " {                                     "
         "     return [true];                    "
         " };                                    "
         "                                       "
         " for (var col := 0; col < n; col += 1) "
         " {                                     "
         "    if (is_valid(row, col))            "
         "    {                                  "
         "       board[row] := col;              "
         "                                       "
         "       if (solve_nqueens(row + 1))     "
         "       {                               "
         "           return [true];              "
         "       }                               "
         "    }                                  "
         " };                                    "
         "                                       "
         " return [false];                       "
      ));

   const std::string sudoku_solver_program =
      " if (solve_nqueens(0))                                  "
      " {                                                      "
      "    println('N-Queens Puzzle Solved!');                 "
      " }                                                      "
      " else                                                   "
      " {                                                      "
      "    println('Error: Failed to solve n-queens puzzle.'); "
      " };                                                     "
      "                                                        "
      " display_board();                                       ";

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   parser_t parser;
   parser.compile(sudoku_solver_program,expression);

   expression.value();
}

// Assembly:
subq $0x19b8, %rsp           # imm = 0x19B8
movabsq $0x402e000000000000, %rax # imm = 0x402E000000000000
movq %rax, 0x19b0(%rsp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, 0x1990(%rsp)
leaq 0x198f(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x11c620
movq 0x88(%rsp), %rcx
leaq 0x1998(%rsp), %rdi
movl $0xf, %esi
leaq 0x1990(%rsp), %rdx
callq 0x11c630
jmp 0x11b894
leaq 0x198f(%rsp), %rdi
callq 0x11c6b0
leaq 0x18a8(%rsp), %rdi
callq 0x11c6c0
jmp 0x11b8b0
leaq 0x18a0(%rsp), %rdi
movl $0x1, %esi
callq 0x11c810
jmp 0x11b8c4
leaq 0x187f(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x114660
movq 0x80(%rsp), %rdx
leaq 0x348b48(%rip), %rsi    # 0x464430
leaq 0x1880(%rsp), %rdi
callq 0x11b770
jmp 0x11b8f7
leaq 0x18a0(%rsp), %rdi
leaq 0x1880(%rsp), %rsi
leaq 0x19b0(%rsp), %rdx
callq 0x11c850
jmp 0x11b916
leaq 0x1880(%rsp), %rdi
callq 0x1147a8
leaq 0x187f(%rsp), %rdi
callq 0x114790
leaq 0x1857(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x114660
movq 0x78(%rsp), %rdx
leaq 0x348c1f(%rip), %rsi    # 0x46456d
leaq 0x1858(%rsp), %rdi
callq 0x11b770
jmp 0x11b95d
leaq 0x18a0(%rsp), %rdi
leaq 0x1858(%rsp), %rsi
leaq 0x1998(%rsp), %rdx
callq 0x11c930
jmp 0x11b97c
leaq 0x1858(%rsp), %rdi
callq 0x1147a8
leaq 0x1857(%rsp), %rdi
callq 0x114790
leaq 0x18a0(%rsp), %rdi
leaq 0x18a8(%rsp), %rsi
callq 0x11c9f0
jmp 0x11b9ad
leaq 0xe68(%rsp), %rdi
leaq 0x18a0(%rsp), %rsi
callq 0x11ca20
jmp 0x11b9c4
leaq 0xe68(%rsp), %rdi
movl $0x1, %esi
callq 0x11cbc0
jmp 0x11b9d8
leaq 0xe68(%rsp), %rdi
movl $0x1, %esi
callq 0x11cbf0
jmp 0x11b9ec
leaq 0xdb7(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x114660
movq 0x70(%rsp), %rdx
leaq 0x348b5b(%rip), %rsi    # 0x464565
leaq 0xdb8(%rsp), %rdi
callq 0x11b770
jmp 0x11ba19
leaq 0xdd8(%rsp), %rdi
leaq 0xdb8(%rsp), %rsi
callq 0x11cc70
jmp 0x11ba30
leaq 0xd8f(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x114660
movq 0x68(%rsp), %rdx
leaq 0x348b25(%rip), %rsi    # 0x464573
leaq 0xd90(%rsp), %rdi
callq 0x11b770
jmp 0x11ba5d
leaq 0xdd8(%rsp), %rdi
leaq 0xd90(%rsp), %rsi
callq 0x11ccf0
movq %rax, 0x60(%rsp)
jmp 0x11ba79
movq 0x60(%rsp), %rsi
leaq 0xe68(%rsp), %rdi
xorl %edx, %edx
callq 0x11cc20
jmp 0x11ba8f
leaq 0xd90(%rsp), %rdi
callq 0x1147a8
leaq 0xd8f(%rsp), %rdi
callq 0x114790
leaq 0xdd8(%rsp), %rdi
callq 0x11cd20
leaq 0xdb8(%rsp), %rdi
callq 0x1147a8
leaq 0xdb7(%rsp), %rdi
callq 0x114790
leaq 0xcd7(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x114660
movq 0x58(%rsp), %rdx
leaq 0x348c51(%rip), %rsi    # 0x46473f
leaq 0xcd8(%rsp), %rdi
callq 0x11b770
jmp 0x11bafd
leaq 0xcf8(%rsp), %rdi
leaq 0xcd8(%rsp), %rsi
callq 0x11cc70
jmp 0x11bb14
leaq 0xcaf(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x114660
movq 0x50(%rsp), %rdx
leaq 0x348c16(%rip), %rsi    # 0x464748
leaq 0xcb0(%rsp), %rdi
callq 0x11b770
jmp 0x11bb41
leaq 0xc87(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x114660
movq 0x48(%rsp), %rdx
leaq 0x348bed(%rip), %rsi    # 0x46474c
leaq 0xc88(%rsp), %rdi
callq 0x11b770
jmp 0x11bb6e
leaq 0xcf8(%rsp), %rdi
leaq 0xcb0(%rsp), %rsi
leaq 0xc88(%rsp), %rdx
callq 0x11cd60
movq %rax, 0x40(%rsp)
jmp 0x11bb92
leaq 0xc5f(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x114660
movq 0x38(%rsp), %rdx
leaq 0x348ba0(%rip), %rsi    # 0x464750
leaq 0xc60(%rsp), %rdi
callq 0x11b770
jmp 0x11bbbf
movq 0x40(%rsp), %rdi
leaq 0xc60(%rsp), %rsi
callq 0x11ccf0
movq %rax, 0x30(%rsp)
jmp 0x11bbd8
movq 0x30(%rsp), %rsi
leaq 0xe68(%rsp), %rdi
xorl %edx, %edx
callq 0x11cc20
jmp 0x11bbee
leaq 0xc60(%rsp), %rdi
callq 0x1147a8
leaq 0xc5f(%rsp), %rdi
callq 0x114790
leaq 0xc88(%rsp), %rdi
callq 0x1147a8
leaq 0xc87(%rsp), %rdi
callq 0x114790
leaq 0xcb0(%rsp), %rdi
callq 0x1147a8
leaq 0xcaf(%rsp), %rdi
callq 0x114790
leaq 0xcf8(%rsp), %rdi
callq 0x11cd20
leaq 0xcd8(%rsp), %rdi
callq 0x1147a8
leaq 0xcd7(%rsp), %rdi
callq 0x114790
leaq 0xba7(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x114660
movq 0x28(%rsp), %rdx
leaq 0x348d04(%rip), %rsi    # 0x464985
leaq 0xba8(%rsp), %rdi
callq 0x11b770
jmp 0x11bc90
leaq 0xbc8(%rsp), %rdi
leaq 0xba8(%rsp), %rsi
callq 0x11cc70
jmp 0x11bca7
leaq 0xb7f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x114660
movq 0x20(%rsp), %rdx
leaq 0x348a83(%rip), %rsi    # 0x464748
leaq 0xb80(%rsp), %rdi
callq 0x11b770
jmp 0x11bcd4
leaq 0xbc8(%rsp), %rdi
leaq 0xb80(%rsp), %rsi
callq 0x11cdb0
movq %rax, 0x18(%rsp)
jmp 0x11bcf0
leaq 0xb57(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x114660
movq 0x10(%rsp), %rdx
leaq 0x348c85(%rip), %rsi    # 0x464993
leaq 0xb58(%rsp), %rdi
callq 0x11b770
jmp 0x11bd1d
movq 0x18(%rsp), %rdi
leaq 0xb58(%rsp), %rsi
callq 0x11ccf0
movq %rax, 0x8(%rsp)
jmp 0x11bd36
movq 0x8(%rsp), %rsi
leaq 0xe68(%rsp), %rdi
xorl %edx, %edx
callq 0x11cc20
jmp 0x11bd4c
leaq 0xb58(%rsp), %rdi
callq 0x1147a8
leaq 0xb57(%rsp), %rdi
callq 0x114790
leaq 0xb80(%rsp), %rdi
callq 0x1147a8
leaq 0xb7f(%rsp), %rdi
callq 0x114790
leaq 0xbc8(%rsp), %rdi
callq 0x11cd20
leaq 0xba8(%rsp), %rdi
callq 0x1147a8
leaq 0xba7(%rsp), %rdi
callq 0x114790
leaq 0xb2f(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x114660
movq (%rsp), %rdx
leaq 0x348eb6(%rip), %rsi    # 0x464c79
leaq 0xb30(%rsp), %rdi
callq 0x11b770
jmp 0x11bdd2
leaq 0xb2f(%rsp), %rdi
callq 0x114790
leaq 0xb08(%rsp), %rdi
callq 0x11cde0
jmp 0x11bdee
leaq 0xb08(%rsp), %rdi
leaq 0x18a0(%rsp), %rsi
callq 0x11ce90
jmp 0x11be05
leaq 0x90(%rsp), %rdi
movl $0x7f, %esi
callq 0x11cf40
jmp 0x11be19
leaq 0x1e0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x11d070
jmp 0x11be30
leaq 0x90(%rsp), %rdi
callq 0x11d8a0
leaq 0x1e0(%rsp), %rdi
leaq 0xb30(%rsp), %rsi
leaq 0xb08(%rsp), %rdx
callq 0x11d910
jmp 0x11be5c
leaq 0xb08(%rsp), %rdi
callq 0x11e1a0
jmp 0x11be6b
leaq 0x1e0(%rsp), %rdi
callq 0x11e1c0
leaq 0xb08(%rsp), %rdi
callq 0x11e3c0
leaq 0xb30(%rsp), %rdi
callq 0x1147a8
leaq 0xe68(%rsp), %rdi
callq 0x11e400
leaq 0x18a0(%rsp), %rdi
callq 0x11e490
leaq 0x18a8(%rsp), %rdi
callq 0x11e510
leaq 0x1998(%rsp), %rdi
callq 0x11e540
addq $0x19b8, %rsp           # imm = 0x19B8
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0x198f(%rsp), %rdi
callq 0x11c6b0
jmp 0x11c36d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c360
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c353
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11bf5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0x1880(%rsp), %rdi
callq 0x1147a8
leaq 0x187f(%rsp), %rdi
callq 0x114790
jmp 0x11c346
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11bfa6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0x1858(%rsp), %rdi
callq 0x1147a8
leaq 0x1857(%rsp), %rdi
callq 0x114790
jmp 0x11c346
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c346
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c074
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c067
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c04d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0xd90(%rsp), %rdi
callq 0x1147a8
leaq 0xd8f(%rsp), %rdi
callq 0x114790
leaq 0xdd8(%rsp), %rdi
callq 0x11cd20
leaq 0xdb8(%rsp), %rdi
callq 0x1147a8
leaq 0xdb7(%rsp), %rdi
callq 0x114790
jmp 0x11c339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c18f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c182
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c168
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c14e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c141
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c134
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0xc60(%rsp), %rdi
callq 0x1147a8
leaq 0xc5f(%rsp), %rdi
callq 0x114790
leaq 0xc88(%rsp), %rdi
callq 0x1147a8
leaq 0xc87(%rsp), %rdi
callq 0x114790
leaq 0xcb0(%rsp), %rdi
callq 0x1147a8
leaq 0xcaf(%rsp), %rdi
callq 0x114790
leaq 0xcf8(%rsp), %rdi
callq 0x11cd20
leaq 0xcd8(%rsp), %rdi
callq 0x1147a8
leaq 0xcd7(%rsp), %rdi
callq 0x114790
jmp 0x11c339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c277
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c26a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c243
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c236
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0xb58(%rsp), %rdi
callq 0x1147a8
leaq 0xb57(%rsp), %rdi
callq 0x114790
leaq 0xb80(%rsp), %rdi
callq 0x1147a8
leaq 0xb7f(%rsp), %rdi
callq 0x114790
leaq 0xbc8(%rsp), %rdi
callq 0x11cd20
leaq 0xba8(%rsp), %rdi
callq 0x1147a8
leaq 0xba7(%rsp), %rdi
callq 0x114790
jmp 0x11c339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0xb2f(%rsp), %rdi
callq 0x114790
jmp 0x11c339
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c32c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
jmp 0x11c31f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x11d8a0
jmp 0x11c31f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1980(%rsp)
movl %eax, 0x197c(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0x11e1c0
leaq 0xb08(%rsp), %rdi
callq 0x11e3c0
leaq 0xb30(%rsp), %rdi
callq 0x1147a8
leaq 0xe68(%rsp), %rdi
callq 0x11e400
leaq 0x18a0(%rsp), %rdi
callq 0x11e490
leaq 0x18a8(%rsp), %rdi
callq 0x11e510
leaq 0x1998(%rsp), %rdi
callq 0x11e540
movq 0x1980(%rsp), %rdi
callq 0x114450
nopw (%rax,%rax)