// File: 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:
subq $0x1938, %rsp           # imm = 0x1938
leaq 0x34d052(%rip), %rsi    # 0x468890
leaq 0x1870(%rsp), %rdi
movl $0xc8, %edx
callq 0x114090
leaq 0x17e7(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x114660
movq 0x90(%rsp), %rdx
leaq 0x347cf1(%rip), %rsi    # 0x463565
leaq 0x17e8(%rsp), %rdi
callq 0x11b770
jmp 0x11b883
leaq 0x1808(%rsp), %rdi
leaq 0x17e8(%rsp), %rsi
callq 0x11c4b0
jmp 0x11b89a
leaq 0x17e8(%rsp), %rdi
callq 0x1147a8
leaq 0x17e7(%rsp), %rdi
callq 0x114790
leaq 0x17c8(%rsp), %rdi
movl $0x1, %esi
callq 0x11c5c0
jmp 0x11b8c8
leaq 0x17a7(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x114660
movq 0x88(%rsp), %rdx
leaq 0x347c80(%rip), %rsi    # 0x46356c
leaq 0x17a8(%rsp), %rdi
callq 0x11b770
jmp 0x11b8fb
leaq 0x17c8(%rsp), %rdi
leaq 0x17a8(%rsp), %rsi
leaq 0x1870(%rsp), %rdx
callq 0x11c600
jmp 0x11b91a
leaq 0x17a8(%rsp), %rdi
callq 0x1147a8
leaq 0x17a7(%rsp), %rdi
callq 0x114790
leaq 0x177f(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x114660
movq 0x80(%rsp), %rdx
leaq 0x347c1f(%rip), %rsi    # 0x463577
leaq 0x1780(%rsp), %rdi
callq 0x11b770
jmp 0x11b967
leaq 0x17c8(%rsp), %rdi
leaq 0x1780(%rsp), %rsi
leaq 0x1808(%rsp), %rdx
callq 0x11c6a0
jmp 0x11b986
leaq 0x1780(%rsp), %rdi
callq 0x1147a8
leaq 0x177f(%rsp), %rdi
callq 0x114790
leaq 0xd90(%rsp), %rdi
leaq 0x17c8(%rsp), %rsi
callq 0x11c8a0
jmp 0x11b9b7
leaq 0xd00(%rsp), %rdi
callq 0x11ca90
jmp 0x11b9c6
leaq 0xcdf(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x114660
movq 0x78(%rsp), %rdx
leaq 0x347b9b(%rip), %rsi    # 0x46357f
leaq 0xce0(%rsp), %rdi
callq 0x11b770
jmp 0x11b9f3
leaq 0xd00(%rsp), %rdi
leaq 0xce0(%rsp), %rsi
callq 0x11cb00
movq %rax, 0x70(%rsp)
jmp 0x11ba0f
leaq 0xcb7(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x114660
movq 0x68(%rsp), %rdx
leaq 0x347a90(%rip), %rsi    # 0x4634bd
leaq 0xcb8(%rsp), %rdi
callq 0x11b770
jmp 0x11ba3c
movq 0x70(%rsp), %rdi
leaq 0xcb8(%rsp), %rsi
callq 0x11cb30
movq %rax, 0x60(%rsp)
jmp 0x11ba55
leaq 0xc8f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x114660
movq 0x58(%rsp), %rdx
leaq 0x347b10(%rip), %rsi    # 0x463583
leaq 0xc90(%rsp), %rdi
callq 0x11b770
jmp 0x11ba82
movq 0x60(%rsp), %rdi
leaq 0xc90(%rsp), %rsi
callq 0x11cb30
movq %rax, 0x50(%rsp)
jmp 0x11ba9b
leaq 0xc67(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x114660
movq 0x48(%rsp), %rdx
leaq 0x347acc(%rip), %rsi    # 0x463585
leaq 0xc68(%rsp), %rdi
callq 0x11b770
jmp 0x11bac8
movq 0x50(%rsp), %rdi
leaq 0xc68(%rsp), %rsi
callq 0x11cb60
movq %rax, 0x40(%rsp)
jmp 0x11bae1
movq 0x40(%rsp), %rsi
leaq 0xd90(%rsp), %rdi
xorl %edx, %edx
callq 0x11ca40
jmp 0x11baf7
leaq 0xc68(%rsp), %rdi
callq 0x1147a8
leaq 0xc67(%rsp), %rdi
callq 0x114790
leaq 0xc90(%rsp), %rdi
callq 0x1147a8
leaq 0xc8f(%rsp), %rdi
callq 0x114790
leaq 0xcb8(%rsp), %rdi
callq 0x1147a8
leaq 0xcb7(%rsp), %rdi
callq 0x114790
leaq 0xce0(%rsp), %rdi
callq 0x1147a8
leaq 0xcdf(%rsp), %rdi
callq 0x114790
leaq 0xd00(%rsp), %rdi
callq 0x11cb90
leaq 0xbd0(%rsp), %rdi
callq 0x11ca90
jmp 0x11bb7b
leaq 0xbaf(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x114660
movq 0x38(%rsp), %rdx
leaq 0x347aa1(%rip), %rsi    # 0x46363a
leaq 0xbb0(%rsp), %rdi
callq 0x11b770
jmp 0x11bba8
leaq 0xbd0(%rsp), %rdi
leaq 0xbb0(%rsp), %rsi
callq 0x11cb00
movq %rax, 0x30(%rsp)
jmp 0x11bbc4
leaq 0xb87(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x114660
movq 0x28(%rsp), %rdx
leaq 0x34784e(%rip), %rsi    # 0x463430
leaq 0xb88(%rsp), %rdi
callq 0x11b770
jmp 0x11bbf1
movq 0x30(%rsp), %rdi
leaq 0xb88(%rsp), %rsi
callq 0x11cb30
movq %rax, 0x20(%rsp)
jmp 0x11bc0a
leaq 0xb5f(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x114660
movq 0x18(%rsp), %rdx
leaq 0x347a1e(%rip), %rsi    # 0x463646
leaq 0xb60(%rsp), %rdi
callq 0x11b770
jmp 0x11bc37
movq 0x20(%rsp), %rdi
leaq 0xb60(%rsp), %rsi
callq 0x11cb60
movq %rax, 0x10(%rsp)
jmp 0x11bc50
movq 0x10(%rsp), %rsi
leaq 0xd90(%rsp), %rdi
xorl %edx, %edx
callq 0x11ca40
jmp 0x11bc66
leaq 0xb60(%rsp), %rdi
callq 0x1147a8
leaq 0xb5f(%rsp), %rdi
callq 0x114790
leaq 0xb88(%rsp), %rdi
callq 0x1147a8
leaq 0xb87(%rsp), %rdi
callq 0x114790
leaq 0xbb0(%rsp), %rdi
callq 0x1147a8
leaq 0xbaf(%rsp), %rdi
callq 0x114790
leaq 0xbd0(%rsp), %rdi
callq 0x11cb90
leaq 0xb37(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x114660
movq 0x8(%rsp), %rdx
leaq 0x347b66(%rip), %rsi    # 0x463845
leaq 0xb38(%rsp), %rdi
callq 0x11b770
jmp 0x11bcee
leaq 0xb37(%rsp), %rdi
callq 0x114790
leaq 0xb10(%rsp), %rdi
callq 0x11cbd0
jmp 0x11bd0a
leaq 0xb10(%rsp), %rdi
leaq 0x17c8(%rsp), %rsi
callq 0x11cc80
jmp 0x11bd21
leaq 0x98(%rsp), %rdi
movl $0x7f, %esi
callq 0x11cd30
jmp 0x11bd35
leaq 0x1e8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x11ce60
jmp 0x11bd4c
leaq 0x98(%rsp), %rdi
callq 0x11d690
leaq 0x1e8(%rsp), %rdi
leaq 0xb38(%rsp), %rsi
leaq 0xb10(%rsp), %rdx
callq 0x11d700
jmp 0x11bd78
leaq 0xb10(%rsp), %rdi
callq 0x11df90
jmp 0x11bd87
leaq 0x1e8(%rsp), %rdi
callq 0x11dfb0
leaq 0xb10(%rsp), %rdi
callq 0x11e1b0
leaq 0xb38(%rsp), %rdi
callq 0x1147a8
leaq 0xd90(%rsp), %rdi
callq 0x11e1f0
leaq 0x17c8(%rsp), %rdi
callq 0x11e280
leaq 0x1808(%rsp), %rdi
callq 0x11e300
addq $0x1938, %rsp           # imm = 0x1938
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11be14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0x17e8(%rsp), %rdi
callq 0x1147a8
leaq 0x17e7(%rsp), %rdi
callq 0x114790
jmp 0x11c1f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c1e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11be76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0x17a8(%rsp), %rdi
callq 0x1147a8
leaq 0x17a7(%rsp), %rdi
callq 0x114790
jmp 0x11c1dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bebf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0x1780(%rsp), %rdi
callq 0x1147a8
leaq 0x177f(%rsp), %rdi
callq 0x114790
jmp 0x11c1dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c1dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c1cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c018
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c00b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bffe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bff1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bfe4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bfd7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11bfca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0xc68(%rsp), %rdi
callq 0x1147a8
leaq 0xc67(%rsp), %rdi
callq 0x114790
leaq 0xc90(%rsp), %rdi
callq 0x1147a8
leaq 0xc8f(%rsp), %rdi
callq 0x114790
leaq 0xcb8(%rsp), %rdi
callq 0x1147a8
leaq 0xcb7(%rsp), %rdi
callq 0x114790
leaq 0xce0(%rsp), %rdi
callq 0x1147a8
leaq 0xcdf(%rsp), %rdi
callq 0x114790
leaq 0xd00(%rsp), %rdi
callq 0x11cb90
jmp 0x11c1cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c100
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c0f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c0e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c0d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c0cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0xb60(%rsp), %rdi
callq 0x1147a8
leaq 0xb5f(%rsp), %rdi
callq 0x114790
leaq 0xb88(%rsp), %rdi
callq 0x1147a8
leaq 0xb87(%rsp), %rdi
callq 0x114790
leaq 0xbb0(%rsp), %rdi
callq 0x1147a8
leaq 0xbaf(%rsp), %rdi
callq 0x114790
leaq 0xbd0(%rsp), %rdi
callq 0x11cb90
jmp 0x11c1cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0xb37(%rsp), %rdi
callq 0x114790
jmp 0x11c1cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c1c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
jmp 0x11c1b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x11d690
jmp 0x11c1b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x17d8(%rsp)
movl %eax, 0x17d4(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x11dfb0
leaq 0xb10(%rsp), %rdi
callq 0x11e1b0
leaq 0xb38(%rsp), %rdi
callq 0x1147a8
leaq 0xd90(%rsp), %rdi
callq 0x11e1f0
leaq 0x17c8(%rsp), %rdi
callq 0x11e280
leaq 0x1808(%rsp), %rdi
callq 0x11e300
movq 0x17d8(%rsp), %rdi
callq 0x114450
nopw %cs:(%rax,%rax)
nopl (%rax)