// File: /workspace/github_cpp_makefile_star2_O2/ArashPartow[P]exprtk-extras/exprtk_truthtable_gen.cpp
// Function: void truth_table_generator<double>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void truth_table_generator(const std::string& boolean_expression)
{
   typedef exprtk::symbol_table<T>    symbol_table_t;
   typedef exprtk::expression<T>      expression_t;
   typedef exprtk::parser<T>          parser_t;
   typedef exprtk::parser_error::type err_t;
   typedef typename parser_t::settings_store settings_t;

   symbol_table_t symbol_table;

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   static const std::size_t compile_options =
      settings_t::e_replacer            +
      settings_t::e_joiner              +
      settings_t::e_numeric_check       +
      settings_t::e_bracket_check       +
      settings_t::e_sequence_check      +
      settings_t::e_strength_reduction  +
      settings_t::e_disable_usr_on_rsrvd;

   parser_t parser(settings_t(compile_options)
                     .disable_all_base_functions    ()
                     .disable_all_control_structures());

   parser.enable_unknown_symbol_resolver();

   parser.dec().collect_variables() = true;
   parser.dec().collect_functions() = true;

   if (!parser.compile(boolean_expression,expression))
   {
      printf("Error: %s\tExpression: %s\n",
             parser.error().c_str(),
             boolean_expression.c_str());

      for (std::size_t i = 0; i < parser.error_count(); ++i)
      {
         err_t error = parser.get_error(i);

         printf("Error: %02d Position: %02d "
                "Type: [%s] "
                "Message: %s "
                "Expression: %s\n",
                static_cast<int>(i),
                static_cast<int>(error.token.position),
                exprtk::parser_error::to_str(error.mode).c_str(),
                error.diagnostic.c_str(),
                boolean_expression.c_str());

         exprtk::parser_error::update_error(error,boolean_expression);

         printf("Error[%02d] at line: %d column: %d\n",
                static_cast<int>(i),
                static_cast<int>(error.line_no),
                static_cast<int>(error.column_no));
      }

      return;
   }

   typedef typename parser_t::dependent_entity_collector::symbol_t dec_symbol_t;

   std::deque<dec_symbol_t> symbol_list;

   parser.dec().symbols(symbol_list);

   if (symbol_list.empty())
   {
      printf("Error: No variables found to build truth table.\n");
      return;
   }

   for (std::size_t i = 0; i < symbol_list.size(); ++i)
   {
      if (exprtk::details::imatch(symbol_list[i].first,"not"))
      {
         symbol_list.erase(symbol_list.begin() + i);

         if (i >= symbol_list.size())
            break;
      }

      if (parser_t::e_st_function == symbol_list[i].second)
      {
         printf("Error: call to function '%s' not allowed.\n",symbol_list[i].first.c_str());
         return;
      }
   }

   if (symbol_list.size() > 32)
   {
      printf("Error: no more than 32 unique variables allowed.\n");
      return;
   }

   unsigned int upper_bound = 1 << symbol_list.size();

   const int index_width = static_cast<int>(std::ceil(std::log10(upper_bound)));

   // Print truth table header
   printf(" #%s ",std::string(index_width - 1,' ').c_str());

   for (std::size_t i = 0; i < symbol_list.size(); ++i)
   {
      printf("| %s ",symbol_list[i].first.c_str());
   }

   printf("| %s \n",boolean_expression.c_str());

   for (std::size_t i = 0; i < upper_bound; ++i)
   {
      for (std::size_t j = 0; j < symbol_list.size(); ++j)
      {
         symbol_table.get_variable(symbol_list[j].first)->ref() = T((i & (1 << (symbol_list.size() - j - 1))) ? 0 : 1);
      }

      const std::size_t result = static_cast<std::size_t>(expression.value());

      printf(" %*d ", index_width, static_cast<int>(i));

      for (std::size_t j = 0; j < symbol_list.size(); ++j)
      {
         printf("| %d ",static_cast<int>(symbol_table.get_variable(symbol_list[j].first)->value()));
      }

      printf("| %d \n", static_cast<int>(result));
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb38, %rsp            # imm = 0xB38
movq %rdi, (%rsp)
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x1109f2
leaq 0x60(%rsp), %rdi
callq 0x110a16
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x110a76
leaq 0xa0(%rsp), %rdi
movl $0x85f, %esi            # imm = 0x85F
callq 0x110ad2
leaq 0xa0(%rsp), %rdi
callq 0x110be2
movq %rax, %rdi
callq 0x110c06
leaq 0x210(%rsp), %rdi
movq %rax, %rsi
callq 0x110c2a
leaq 0xa0(%rsp), %rdi
callq 0x111242
leaq 0x5f0(%rsp), %rax
movb $0x1, -0x18(%rax)
movq %rax, -0x8(%rax)
movw $0x101, -0x128(%rax)    # imm = 0x101
leaq 0x210(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movq (%rsp), %rsi
callq 0x11128a
testb %al, %al
je 0x11055d
leaq 0xa0(%rsp), %rdi
callq 0x235664
leaq 0x4c0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1119bc
movq 0xd0(%rsp), %rax
cmpq 0xb0(%rsp), %rax
je 0x11067e
xorl %r12d, %r12d
leaq 0xa0(%rsp), %r13
leaq 0x12bb26(%rip), %r14    # 0x23bf9f
leaq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %r15
movq %r13, %rdi
callq 0x111a4a
cmpq %rax, %r12
jae 0x110662
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x2362f8
movq %rax, %rbx
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x11033e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x111a5a
movl %eax, %ebx
movq %rbp, %rdi
callq 0x10e5e8
testb %bl, %bl
je 0x11053b
movups 0xb0(%rsp), %xmm0
movups 0xc0(%rsp), %xmm1
movaps %xmm0, 0x10(%rsp)
movaps %xmm1, 0x20(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x111aea
movapd 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movapd %xmm0, 0x80(%rsp)
movaps %xmm1, 0x90(%rsp)
leaq 0x1f0(%rsp), %rdi
movq %r13, %rsi
leaq 0x80(%rsp), %rdx
callq 0x111ab2
movq %r13, %rdi
callq 0x111a4a
cmpq %rax, %r12
jae 0x110662
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x2362f8
cmpl $0x5, 0x20(%rax)
je 0x11068c
incq %r12
jmp 0x110483
leaq 0xa0(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0x11170c
leaq 0xa0(%rsp), %rbx
movq (%rbx), %rsi
movq (%rsp), %rax
movq (%rax), %rdx
leaq 0x12bb9e(%rip), %rdi    # 0x23c129
xorl %eax, %eax
callq 0x10e060
movq %rbx, %rdi
callq 0x10e5e8
leaq 0x30(%rsp), %rbp
leaq 0x12bbdf(%rip), %r13    # 0x23c185
xorl %r14d, %r14d
movq (%rsp), %r12
movq %r14, 0x10(%rsp)
leaq 0x518(%rsp), %rdi
callq 0x1721b6
cmpq %rax, %r14
jae 0x1106ba
movq %rbx, %rdi
leaq 0x210(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x111760
movq 0x10(%rsp), %r15
movq 0xc8(%rsp), %r14
movl 0xd0(%rsp), %esi
movq %rbp, %rdi
callq 0x1117f4
movq 0x30(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq (%r12), %r9
leaq 0x12bb32(%rip), %rdi    # 0x23c143
movl %r15d, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10e060
movq %rbp, %rdi
callq 0x10e5e8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1118a0
movl 0x10(%rsp), %esi
movl 0x138(%rsp), %edx
movl 0x140(%rsp), %ecx
movq %r13, %rdi
xorl %eax, %eax
callq 0x10e060
movq %rbx, %rdi
callq 0x111990
movq 0x10(%rsp), %r14
incq %r14
jmp 0x1105ad
leaq 0xa0(%rsp), %rdi
callq 0x111a4a
cmpq $0x21, %rax
jb 0x1106ed
leaq 0x13074b(%rip), %rdi    # 0x240dc7
jmp 0x110685
leaq 0x130773(%rip), %rdi    # 0x240df8
callq 0x10e500
jmp 0x1106ad
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x2362f8
movq (%rax), %rsi
leaq 0x12bb03(%rip), %rdi    # 0x23c1a9
xorl %eax, %eax
callq 0x10e060
leaq 0xa0(%rsp), %rdi
callq 0x111b6c
leaq 0x210(%rsp), %rdi
callq 0x111bb8
leaq 0x60(%rsp), %rdi
callq 0x111cf2
leaq 0x8(%rsp), %rdi
callq 0x111d08
addq $0xb38, %rsp            # imm = 0xB38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa0(%rsp), %rdi
callq 0x111a4a
pushq $0x1
popq %rbp
movl %eax, %ecx
shll %cl, %ebp
movl %ebp, %edi
callq 0x111b09
callq 0x10e540
cvttsd2si %xmm0, %eax
movq %rax, 0x58(%rsp)
decl %eax
movslq %eax, %rsi
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x30(%rsp), %rdi
pushq $0x20
popq %rdx
callq 0x10e2c0
leaq 0x30(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x12ba94(%rip), %rdi    # 0x23c1d4
xorl %eax, %eax
callq 0x10e060
movq %rbx, %rdi
callq 0x10e5e8
leaq 0xa0(%rsp), %rbx
leaq 0x12ba7c(%rip), %r12    # 0x23c1da
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x111a4a
cmpq %rax, %r13
jae 0x110790
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x2362f8
movq (%rax), %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10e060
incq %r13
jmp 0x110761
movq (%rsp), %rax
movq (%rax), %rsi
leaq 0x12ba42(%rip), %rdi    # 0x23c1e0
xorl %eax, %eax
callq 0x10e060
movl %ebp, %eax
movq %rax, (%rsp)
leaq 0xa0(%rsp), %r14
leaq 0x8(%rsp), %r13
xorl %r12d, %r12d
cmpq (%rsp), %r12
je 0x1106ad
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x111a4a
cmpq %rax, %rbx
jae 0x11082b
movq %r14, %rdi
callq 0x111a4a
movq %rax, %r15
leaq 0xb0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2362f8
movq %r13, %rdi
movq %rax, %rsi
callq 0x111b16
addl %ebp, %r15d
pushq $0x1
popq %rdx
movl %r15d, %ecx
shll %cl, %edx
movslq %edx, %rcx
movsd 0x1267fd(%rip), %xmm0   # 0x237010
testq %rcx, %r12
je 0x11081c
xorpd %xmm0, %xmm0
movq 0x28(%rax), %rax
movsd %xmm0, (%rax)
incq %rbx
decl %ebp
jmp 0x1107ca
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x1267ba(%rip), %xmm0   # 0x237008
cvttsd2si %xmm0, %rbp
andq %rcx, %rbp
orq %rax, %rbp
leaq 0x12b987(%rip), %rdi    # 0x23c1e7
movq 0x58(%rsp), %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x10e060
xorl %ebx, %ebx
leaq 0x12b975(%rip), %r15    # 0x23c1ed
movq %r14, %rdi
callq 0x111a4a
cmpq %rax, %rbx
jae 0x1108b7
leaq 0xb0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2362f8
movq %r13, %rdi
movq %rax, %rsi
callq 0x111b16
movq 0x28(%rax), %rax
cvttsd2si (%rax), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e060
incq %rbx
jmp 0x110878
leaq 0x12b935(%rip), %rdi    # 0x23c1f3
movl %ebp, %esi
xorl %eax, %eax
callq 0x10e060
incq %r12
jmp 0x1107bb
jmp 0x110918
jmp 0x110918
jmp 0x1108fd
jmp 0x1108fd
jmp 0x1108fd
jmp 0x1108db
movq %rax, %rbx
jmp 0x110935
movq %rax, %rbx
jmp 0x11093f
jmp 0x110918
jmp 0x110918
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x111242
jmp 0x110935
jmp 0x110918
movq %rax, %rbx
jmp 0x110928
jmp 0x110904
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x111990
jmp 0x110928
jmp 0x110918
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x111b6c
leaq 0x210(%rsp), %rdi
callq 0x111bb8
leaq 0x60(%rsp), %rdi
callq 0x111cf2
leaq 0x8(%rsp), %rdi
callq 0x111d08
movq %rbx, %rdi
callq 0x10e320