// File: /workspace/github_cpp_makefile_star2_O2/ArashPartow[P]exprtk-extras/exprtk_loop_timeout_rtc.cpp
// Function: void loop_timeout_rtc<double>()
void loop_timeout_rtc()
{
   typedef exprtk::expression<T>      expression_t;
   typedef exprtk::parser<T>          parser_t;
   typedef exprtk::loop_runtime_check loop_runtime_check_t;

   const std::string sieve_of_eratosthenes_program =
      " var sieve[10^8] := [false];                   "
      " var m := trunc(sqrt(sieve[]));                "
      "                                               "
      " sieve[0] := true;                             "
      " sieve[1] := true;                             "
      "                                               "
      " for (var i := 0; i <= m; i += 1)              "
      " {                                             "
      "    if (false == sieve[i])                     "
      "    {                                          "
      "       for (var j := i^2; j < sieve[]; j += i) "
      "       {                                       "
      "          sieve[j] := true;                    "
      "       }                                       "
      "    }                                          "
      " };                                            "
      "                                               "
      " var prime_count := sieve[] - sum(sieve);      "
      "                                               "
      " prime_count == 5761455;                       ";

   timeout_rtc_handler loop_runtime_check;
   loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
   loop_runtime_check.max_loop_iterations =
      static_cast<std::uint64_t>(10e8 * std::log10(std::log10(10e8))); // O(n) = nlog(log(n))

   vector_access_rtc vec_rtc;

   expression_t expression;

   parser_t parser;

   parser.register_loop_runtime_check(loop_runtime_check);
   parser.register_vector_access_runtime_check(vec_rtc);

   parser.compile(sieve_of_eratosthenes_program,expression);

   const auto max_duration = std::chrono::seconds(1);
   const auto timeout_tp   = std::chrono::steady_clock::now() + max_duration;
   loop_runtime_check.set_timeout_time(timeout_tp);

   try
   {
      expression.value();
   }
   catch (timeout_rtc_handler::timeout_exception& /*toe*/)
   {
      printf("Timeout Exception\n");
   }
   catch (std::runtime_error& rte)
   {
      printf("Exception: %s\n",rte.what());
   }

   parser.clear_loop_runtime_check();
}

// Assembly:
pushq %rbx
subq $0xaf0, %rsp            # imm = 0xAF0
leaq 0x129db3(%rip), %rsi    # 0x23a105
leaq 0x58(%rsp), %rdi
leaq 0x1c8(%rsp), %rdx
callq 0x110308
leaq 0x24e1dd(%rip), %rax    # 0x35e548
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rsp)
movl $0x7, 0x38(%rsp)
movq $0x38e095cd, 0x40(%rsp) # imm = 0x38E095CD
leaq 0x24e238(%rip), %rax    # 0x35e5c8
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x11058c
leaq 0x78(%rsp), %rdi
pushq $0x7f
popq %rsi
callq 0x1105ec
leaq 0x1c8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1106fc
leaq 0x78(%rsp), %rdi
callq 0x110d14
leaq 0x30(%rsp), %rax
leaq 0x1c8(%rsp), %rdi
movq %rax, 0x8d8(%rdi)
leaq 0x8(%rsp), %rax
movq %rax, 0x8e0(%rdi)
leaq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x110d5c
callq 0x10e4d0
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, 0x50(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
leaq 0x1c8(%rsp), %rdi
andq $0x0, 0x8d8(%rdi)
callq 0x1111de
leaq 0x10(%rsp), %rdi
callq 0x111318
leaq 0x58(%rsp), %rdi
callq 0x10e600
addq $0xaf0, %rsp            # imm = 0xAF0
popq %rbx
retq
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x11046b
movq %rbx, %rdi
callq 0x10e050
leaq 0x12eba2(%rip), %rdi    # 0x23f001
callq 0x10e520
callq 0x10e3a0
jmp 0x110416
cmpl $0x1, %edx
jne 0x1104a3
movq %rbx, %rdi
callq 0x10e050
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x12a02a(%rip), %rdi    # 0x23a4b2
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e060
callq 0x10e3a0
jmp 0x110416
jmp 0x1104a0
jmp 0x1104a0
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
callq 0x1111de
jmp 0x1104c4
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x110d14
jmp 0x1104c4
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x111318
jmp 0x1104d3
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x10e600
jmp 0x1104e2
movq %rax, %rbx
movq %rbx, %rdi
callq 0x10e330