// File: ArashPartow[P]exprtk/exprtk_simple_example_11.cpp
// Function: void square_wave2<double>()
void square_wave2()
{
   typedef exprtk::symbol_table<T> symbol_table_t;
   typedef exprtk::expression<T>   expression_t;
   typedef exprtk::parser<T>       parser_t;

   const std::string wave_program =
      " var r := 0;                                          "
      "                                                      "
      " for (var i := 0; i < 1000; i += 1)                   "
      " {                                                    "
      "    r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
      " };                                                   "
      "                                                      "
      " r *= a * (4 / pi);                                   ";

   static const T pi = T(3.141592653589793238462643383279502);

   T f = pi / T(10);
   T t = T(0);
   T a = T(10);

   symbol_table_t symbol_table;
   symbol_table.add_variable("f",f);
   symbol_table.add_variable("t",t);
   symbol_table.add_variable("a",a);
   symbol_table.add_constants();

   expression_t expression;
   expression.register_symbol_table(symbol_table);

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

   const T delta = (T(4) * pi) / T(1000);

   for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
   {
      const T result = expression.value();
      printf("%19.15f\t%19.15f\n", t, result);
   }
}

// Assembly:
subq $0xba8, %rsp            # imm = 0xBA8
leaq 0xb87(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x111640
movq 0x28(%rsp), %rdx
leaq 0x32fd40(%rip), %rsi    # 0x448565
leaq 0xb88(%rsp), %rdi
callq 0x118740
jmp 0x118834
leaq 0xb87(%rsp), %rdi
callq 0x111760
movabsq $0x3fd41b2f769cf0e0, %rax # imm = 0x3FD41B2F769CF0E0
movq %rax, 0xb68(%rsp)
movq $0x0, 0xb60(%rsp)
movabsq $0x4024000000000000, %rax # imm = 0x4024000000000000
movq %rax, 0xb58(%rsp)
leaq 0xb50(%rsp), %rdi
movl $0x1, %esi
callq 0x118f70
jmp 0x118885
leaq 0xb2f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x111640
movq 0x20(%rsp), %rdx
leaq 0x32fc2f(%rip), %rsi    # 0x4484d2
leaq 0xb30(%rsp), %rdi
callq 0x118740
jmp 0x1188b2
leaq 0xb50(%rsp), %rdi
leaq 0xb30(%rsp), %rsi
leaq 0xb68(%rsp), %rdx
xorl %ecx, %ecx
callq 0x118fb0
jmp 0x1188d3
leaq 0xb30(%rsp), %rdi
callq 0x111778
leaq 0xb2f(%rsp), %rdi
callq 0x111760
leaq 0xb07(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x111640
movq 0x18(%rsp), %rdx
leaq 0x3304bc(%rip), %rsi    # 0x448dc7
leaq 0xb08(%rsp), %rdi
callq 0x118740
jmp 0x11891a
leaq 0xb50(%rsp), %rdi
leaq 0xb08(%rsp), %rsi
leaq 0xb60(%rsp), %rdx
xorl %ecx, %ecx
callq 0x118fb0
jmp 0x11893b
leaq 0xb08(%rsp), %rdi
callq 0x111778
leaq 0xb07(%rsp), %rdi
callq 0x111760
leaq 0xadf(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x111640
movq 0x10(%rsp), %rdx
leaq 0x330fb7(%rip), %rsi    # 0x44992a
leaq 0xae0(%rsp), %rdi
callq 0x118740
jmp 0x118982
leaq 0xb50(%rsp), %rdi
leaq 0xae0(%rsp), %rsi
leaq 0xb58(%rsp), %rdx
xorl %ecx, %ecx
callq 0x118fb0
jmp 0x1189a3
leaq 0xae0(%rsp), %rdi
callq 0x111778
leaq 0xadf(%rsp), %rdi
callq 0x111760
leaq 0xb50(%rsp), %rdi
callq 0x119060
jmp 0x1189cc
leaq 0xab8(%rsp), %rdi
callq 0x1190c0
jmp 0x1189db
leaq 0xab8(%rsp), %rdi
leaq 0xb50(%rsp), %rsi
callq 0x119170
jmp 0x1189f2
leaq 0x40(%rsp), %rdi
movl $0x7f, %esi
callq 0x119220
jmp 0x118a03
leaq 0x190(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x119350
jmp 0x118a17
leaq 0x40(%rsp), %rdi
callq 0x119b80
leaq 0x190(%rsp), %rdi
leaq 0xb88(%rsp), %rsi
leaq 0xab8(%rsp), %rdx
callq 0x119bf0
jmp 0x118a40
movsd 0x32a5c8(%rip), %xmm0   # 0x443010
movsd %xmm0, 0x38(%rsp)
movsd 0x32a5b2(%rip), %xmm0   # 0x443008
movsd %xmm0, 0xb60(%rsp)
movsd 0x32a5b1(%rip), %xmm0   # 0x443018
ucomisd 0xb60(%rsp), %xmm0
jb 0x118c60
leaq 0xab8(%rsp), %rdi
callq 0x11a480
movsd %xmm0, 0x8(%rsp)
jmp 0x118a8b
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0xb60(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
leaq 0x32fc69(%rip), %rdi    # 0x448716
movb $0x2, %al
callq 0x111080
jmp 0x118ab6
jmp 0x118ab8
movsd 0x32a550(%rip), %xmm0   # 0x443010
addsd 0xb60(%rsp), %xmm0
movsd %xmm0, 0xb60(%rsp)
jmp 0x118a5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0xb87(%rsp), %rdi
callq 0x111760
jmp 0x118cc3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118cb6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118b4a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0xb30(%rsp), %rdi
callq 0x111778
leaq 0xb2f(%rsp), %rdi
callq 0x111760
jmp 0x118ca9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118b93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0xb08(%rsp), %rdi
callq 0x111778
leaq 0xb07(%rsp), %rdi
callq 0x111760
jmp 0x118ca9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118bdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0xae0(%rsp), %rdi
callq 0x111778
leaq 0xadf(%rsp), %rdi
callq 0x111760
jmp 0x118ca9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118ca9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
jmp 0x118c9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x119b80
jmp 0x118c9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb78(%rsp)
movl %eax, 0xb74(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x11a4a0
jmp 0x118c9c
leaq 0x190(%rsp), %rdi
callq 0x11a4a0
leaq 0xab8(%rsp), %rdi
callq 0x11a6a0
leaq 0xb50(%rsp), %rdi
callq 0x11a6e0
leaq 0xb88(%rsp), %rdi
callq 0x111778
addq $0xba8, %rsp            # imm = 0xBA8
retq
leaq 0xab8(%rsp), %rdi
callq 0x11a6a0
leaq 0xb50(%rsp), %rdi
callq 0x11a6e0
leaq 0xb88(%rsp), %rdi
callq 0x111778
movq 0xb78(%rsp), %rdi
callq 0x111440