// File: /workspace/github_cpp_makefile_star2_O2/ArashPartow[P]exprtk-extras/exprtk_factorize_pollard.cpp
// Function: void factorize_pollards_rho<double>()
void factorize_pollards_rho()
{
   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;

   // Form: n = p*q where p,q are prime
   T composites[] =
   {
       199203677,  779234623,  843093203,  883543291, 1197162971,
      1282615157, 1552390397, 1765737859, 1878769589, 1993904873,
      2257133471, 2520523529, 2579094799, 2853450949, 2935025369,
      3095780533, 3164132249, 3408963511, 4260042859, 4608613981,
      4654875857, 5085931997, 7278175081, 7289187463, 9206112101
   };

   exprtk::rtl::io::println<T> println;

   symbol_table_t symbol_table;

   symbol_table.add_vector  ("composites", composites);
   symbol_table.add_function("println"   , println   );

   compositor_t compositor(symbol_table);

   compositor.add(function_t()
      .name("gcd")
      .var("x") .var("y")
      .expression
      (
         " x := abs(x);       "
         " y := abs(y);       "
         " while (0 != y)     "
         " {                  "
         "    var r := x % y; "
         "    x := y;         "
         "    y := r;         "
         " };                 "
         " x                  "
      ));

   compositor.add(function_t()
      .name("pollard_rho")
      .var("n")
      .expression
      (
         " var c  := 10;                "
         " var a1 := 1 + c;             "
         " var a2 := 11 + c;            "
         "                              "
         " while (gcd(n, a2 - a1) == 1) "
         " {                            "
         "    /* The Tortoise */        "
         "    a1 := (a1^2 + c) % n;     "
         "                              "
         "    /* The Hare */            "
         "    a2 := (a2^2 + c) % n;     "
         "    a2 := (a2^2 + c) % n;     "
         " };                           "
         "                              "
         " var g := gcd(n, a2 - a1);    "
         "                              "
         " n / g                        "
      ));

   const std::string factorize_composites_program =
      " for (var i := 0; i < composites[]; i += 1)                               "
      " {                                                                        "
      "    var n       := composites[i];                                         "
      "    var factor0 := pollard_rho(n);                                        "
      "    var factor1 := n / factor0;                                           "
      "                                                                          "
      "    if ((factor0 * factor1 == n) and (factor0 != 1))                      "
      "    {                                                                     "
      "       println('n: ', n, ' factors: { ', factor0 ,' , ', factor1 ,' } '); "
      "    }                                                                     "
      "    else                                                                  "
      "    {                                                                     "
      "       println('failed to factorize ', n);                                "
      "    }                                                                     "
      " }                                                                        "
      "                                                                          ";
   expression_t expression;
   expression.register_symbol_table(symbol_table);

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

   expression.value();
}

// Assembly:
pushq %rbx
subq $0x1610, %rsp           # imm = 0x1610
leaq 0x13610e(%rip), %rsi    # 0x249430
leaq 0x230(%rsp), %rdi
movl $0xc8, %edx
callq 0x111070
leaq 0x130de2(%rip), %rsi    # 0x24411d
leaq 0xc28(%rsp), %rdi
leaq 0x300(%rsp), %rdx
callq 0x1132d8
leaq 0x78(%rsp), %rdi
leaq 0xc28(%rsp), %rsi
callq 0x113892
leaq 0xc28(%rsp), %rdi
callq 0x1115d8
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x113924
leaq 0x130da1(%rip), %rsi    # 0x244124
leaq 0xc28(%rsp), %rdi
leaq 0x300(%rsp), %rdx
callq 0x1132d8
leaq 0x10(%rsp), %rdi
leaq 0xc28(%rsp), %rsi
leaq 0x230(%rsp), %rdx
callq 0x113948
leaq 0xc28(%rsp), %rdi
callq 0x1115d8
leaq 0x130d69(%rip), %rsi    # 0x24412f
leaq 0xc28(%rsp), %rdi
leaq 0x300(%rsp), %rdx
callq 0x1132d8
leaq 0x10(%rsp), %rdi
leaq 0xc28(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x1139aa
leaq 0xc28(%rsp), %rdi
callq 0x1115d8
leaq 0xc28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x113a72
leaq 0x300(%rsp), %rdi
callq 0x113b90
leaq 0x130d12(%rip), %rsi    # 0x244137
leaq 0xe0(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0x1132d8
leaq 0x300(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x111550
leaq 0x130c22(%rip), %rsi    # 0x244075
leaq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x1132d8
leaq 0x340(%rsp), %rbx
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x11e858
leaq 0x1315e9(%rip), %rsi    # 0x244a67
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x1132d8
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x11e858
leaq 0x130c9a(%rip), %rsi    # 0x24413b
leaq 0x58(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x1132d8
leaq 0x320(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x111550
leaq 0xc28(%rsp), %rdi
leaq 0x300(%rsp), %rsi
xorl %edx, %edx
callq 0x113b80
leaq 0x58(%rsp), %rdi
callq 0x1115d8
leaq 0x18(%rsp), %rdi
callq 0x1115d8
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xe0(%rsp), %rdi
callq 0x1115d8
leaq 0x300(%rsp), %rdi
callq 0x113be8
leaq 0x300(%rsp), %rdi
callq 0x113b90
leaq 0x130ccb(%rip), %rsi    # 0x2441f0
leaq 0xe0(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x1132d8
leaq 0x300(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x111550
leaq 0x130a95(%rip), %rsi    # 0x243fe8
leaq 0x38(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0x1132d8
leaq 0x340(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x11e858
leaq 0x130c81(%rip), %rsi    # 0x2441fc
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x1132d8
leaq 0x320(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x111550
leaq 0xc28(%rsp), %rdi
leaq 0x300(%rsp), %rsi
xorl %edx, %edx
callq 0x113b80
leaq 0x18(%rsp), %rdi
callq 0x1115d8
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xe0(%rsp), %rdi
callq 0x1115d8
leaq 0x300(%rsp), %rdi
callq 0x113be8
leaq 0x130e13(%rip), %rsi    # 0x2443fb
leaq 0x38(%rsp), %rdi
leaq 0x300(%rsp), %rdx
callq 0x1132d8
leaq 0x18(%rsp), %rdi
callq 0x113c08
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x113c68
leaq 0xe0(%rsp), %rdi
pushq $0x7f
popq %rsi
callq 0x113cc4
leaq 0x300(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x113dd4
leaq 0xe0(%rsp), %rdi
callq 0x1143ec
leaq 0x300(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x114434
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
leaq 0x300(%rsp), %rdi
callq 0x1148b6
leaq 0x18(%rsp), %rdi
callq 0x1149f0
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xc28(%rsp), %rdi
callq 0x114a06
leaq 0x10(%rsp), %rdi
callq 0x114a5a
leaq 0x78(%rsp), %rdi
callq 0x114a9c
addq $0x1610, %rsp           # imm = 0x1610
popq %rbx
retq
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x1143ec
jmp 0x113771
jmp 0x1136cd
movq %rax, %rbx
jmp 0x113771
movq %rax, %rbx
jmp 0x11377b
jmp 0x113711
jmp 0x1136f7
jmp 0x1136f7
jmp 0x113701
jmp 0x113701
jmp 0x113709
jmp 0x113711
jmp 0x113787
jmp 0x113787
jmp 0x1136f7
movq %rax, %rbx
jmp 0x1137a3
jmp 0x113701
movq %rax, %rbx
jmp 0x1137ad
movq %rax, %rbx
jmp 0x1137ba
movq %rax, %rbx
jmp 0x1137c7
jmp 0x113734
jmp 0x11371f
jmp 0x113734
movq %rax, %rbx
leaq 0xc28(%rsp), %rdi
callq 0x1115d8
jmp 0x1137d4
movq %rax, %rbx
jmp 0x1137d4
movq %rax, %rbx
jmp 0x1137de
movq %rax, %rbx
leaq 0xc28(%rsp), %rdi
callq 0x1115d8
jmp 0x1137e8
movq %rax, %rbx
jmp 0x1137e8
movq %rax, %rbx
leaq 0x300(%rsp), %rdi
callq 0x1148b6
leaq 0x18(%rsp), %rdi
callq 0x1149f0
leaq 0x38(%rsp), %rdi
callq 0x1115d8
jmp 0x1137c7
movq %rax, %rbx
jmp 0x113799
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x1115d8
leaq 0x18(%rsp), %rdi
callq 0x1115d8
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xe0(%rsp), %rdi
callq 0x1115d8
leaq 0x300(%rsp), %rdi
callq 0x113be8
leaq 0xc28(%rsp), %rdi
callq 0x114a06
leaq 0x10(%rsp), %rdi
callq 0x114a5a
leaq 0x78(%rsp), %rdi
callq 0x114a9c
movq %rbx, %rdi
callq 0x111310