// File: /workspace/github_cpp_makefile_star2_O2/redpony[P]cpyp/crp_test.cc
// Function: test_mh1()
void test_mh1() {
  cpyp::MT19937 eng;
  const vector<double> ref = {0, 0, 0, 0.00466121, 0.0233846, 0.0647365, 0.125693, 0.183448, 0.204806, 0.177036, 0.119629, 0.0627523, 0.02507, 0.00725451, 0.0013911};
  cpyp::crp<int> crp(0.5, 1.0);
  vector<int> hist(15, 0);
  double c = 0;
  double ac = 0;
  double tmh = 0;
  for (int s = 0; s < 200000; ++s) {
    for (int i = 0; i < 15; ++i) {
      int y_i = i % 3;
      cpyp::crp<int> prev = crp;  // save old state
      bool wasrem = false;
      if (s > 0)
        wasrem = crp.decrement(y_i, eng);

      bool wasnew = crp.increment(y_i, q0[y_i], eng);
      if (s > 0) {
        double p_new = 1, q_new = 1;
        if (wasnew) { p_new = p0[y_i]; q_new = q0[y_i]; }
        double p_old = 1, q_old = 1;
        if (wasrem) { p_old = p0[y_i]; q_old = q0[y_i]; }
        double a = p_new / p_old * q_old / q_new;
        ++tmh;
        if (a >= 1.0 || cpyp::sample_uniform01<double>(eng) < a) { // mh accept
          ++ac;
        } else { // mh reject
          std::swap(crp, prev);  // swap is faster than =
        }
      }
    }
    if (s > 300 && s % 4 == 3) { ++c; hist[crp.num_tables()]++; }
  }
  ac /= tmh;
  cerr << "ACCEPTANCE: " << ac << endl;
  int j =0;
  double te = 0;
  double me = 0;
  for (auto i : hist) {
    double err = (i / c - ref[j++]);
    cerr << err << "\t" << i/c << endl;
    te += fabs(err);
    if (fabs(err) > me) { me = fabs(err); }
  }
  te /= 12;
  cerr << "Average error: " << te;
  if (te > 0.01) { cerr << "  ** TOO HIGH **"; }
  cerr << endl << "    Max error: " << me;
  if (me > 0.01) { cerr << "  ** TOO HIGH **"; }
  cerr << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x14e8, %rsp           # imm = 0x14E8
leaq 0x160(%rsp), %rdi
callq 0x3f30
leaq 0x6cf3(%rip), %rsi      # 0x9148
pushq $0xf
popq %rdx
leaq 0xe8(%rsp), %rax
movq %rdx, %rcx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rcx
movq %rax, %rsi
callq 0x3f4a
leaq 0xe8(%rsp), %rdi
movsd 0x6b7d(%rip), %xmm0     # 0x9008
movsd 0x6b7d(%rip), %xmm1     # 0x9010
callq 0x3f80
leaq 0x70(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
pushq $0xf
popq %rsi
leaq 0xc(%rsp), %rcx
callq 0x3fe8
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0xe8(%rsp), %r15
leaq 0x160(%rsp), %r13
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
cmpl $0x30d40, %ebx          # imm = 0x30D40
je 0x266d
xorl %ebp, %ebp
pushq $0xf
popq %r12
movl %ebp, %eax
xorl %edx, %edx
pushq $0x3
popq %rcx
divl %ecx
subl $0x1, %r12d
jb 0x262c
movl %edx, 0xc(%rsp)
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x403e
testl %ebx, %ebx
je 0x252e
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x4086
testl %eax, %eax
setne %r14b
jmp 0x2531
xorl %r14d, %r14d
movslq 0xc(%rsp), %rax
leaq 0x6bd3(%rip), %rcx      # 0x9110
leaq (%rcx,%rax,8), %rdx
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
movq %r13, %rcx
callq 0x41d2
testl %ebx, %ebx
je 0x261b
movsd 0x6aaf(%rip), %xmm0     # 0x9010
movapd %xmm0, %xmm3
testl %eax, %eax
je 0x2586
movslq 0xc(%rsp), %rax
leaq 0x6bbb(%rip), %rcx      # 0x9130
movsd (%rcx,%rax,8), %xmm3
leaq 0x6b8f(%rip), %rcx      # 0x9110
movsd (%rcx,%rax,8), %xmm0
movsd 0x6a82(%rip), %xmm2     # 0x9010
movapd %xmm2, %xmm1
testb %r14b, %r14b
je 0x25b4
movslq 0xc(%rsp), %rax
leaq 0x6b8d(%rip), %rcx      # 0x9130
movsd (%rcx,%rax,8), %xmm1
leaq 0x6b61(%rip), %rcx      # 0x9110
movsd (%rcx,%rax,8), %xmm2
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm0, %xmm3
movsd 0x6a48(%rip), %xmm0     # 0x9010
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm3, 0x28(%rsp)
ucomisd %xmm0, %xmm3
jae 0x25f8
movq %r13, %rdi
callq 0x427a
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x260e
movsd 0x18(%rsp), %xmm0
addsd 0x6a0a(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x18(%rsp)
jmp 0x261b
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
callq 0x429b
leaq 0x78(%rsp), %rdi
callq 0x5008
incl %ebp
jmp 0x24ec
cmpl $0x12d, %ebx            # imm = 0x12D
setae %al
movl %ebx, %ecx
notl %ecx
testb $0x3, %cl
sete %cl
testb %cl, %al
je 0x2666
movsd 0x20(%rsp), %xmm0
addsd 0x69bf(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x20(%rsp)
movl 0xe8(%rsp), %eax
movq 0x30(%rsp), %rcx
incl (%rcx,%rax,4)
incl %ebx
jmp 0x24da
movq 0xb97c(%rip), %rdi      # 0xdff0
leaq 0x6d2e(%rip), %rsi      # 0x93a9
callq 0x2170
movsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rax, %rdi
callq 0x2200
movq %rax, %rdi
callq 0x2080
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r12
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
movq 0xb936(%rip), %rbx      # 0xdff0
leaq 0x6cf5(%rip), %r14      # 0x93b6
movsd %xmm0, 0x18(%rsp)
leaq (%r15,%r13), %rax
cmpq %r12, %rax
je 0x274d
cvtsi2sdl (%rax), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0x48(%rsp), %rax
movsd %xmm0, 0x28(%rsp)
subsd (%rax,%r13,2), %xmm0
movq %rbx, %rdi
movapd %xmm0, 0x60(%rsp)
callq 0x2200
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rax, %rdi
movsd 0x28(%rsp), %xmm0
callq 0x2200
movq %rax, %rdi
callq 0x2080
movapd 0x60(%rsp), %xmm1
andpd 0x6978(%rip), %xmm1     # 0x90a0
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
maxsd 0x18(%rsp), %xmm1
addq $0x4, %r13
movsd %xmm1, 0x18(%rsp)
jmp 0x26c7
movq 0xb89c(%rip), %rdi      # 0xdff0
leaq 0x6c5d(%rip), %rsi      # 0x93b8
callq 0x2170
movsd 0x10(%rsp), %xmm0
divsd 0x68aa(%rip), %xmm0     # 0x9018
movsd %xmm0, 0x10(%rsp)
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2200
movsd 0x10(%rsp), %xmm0
ucomisd 0x6890(%rip), %xmm0     # 0x9020
jbe 0x27a5
movq 0xb857(%rip), %rdi      # 0xdff0
leaq 0x6c28(%rip), %rsi      # 0x93c8
callq 0x2170
movq 0xb844(%rip), %rdi      # 0xdff0
callq 0x2080
leaq 0x6c21(%rip), %rsi      # 0x93d9
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x2200
movsd 0x18(%rsp), %xmm0
ucomisd 0x6844(%rip), %xmm0     # 0x9020
jbe 0x27f1
movq 0xb80b(%rip), %rdi      # 0xdff0
leaq 0x6bdc(%rip), %rsi      # 0x93c8
callq 0x2170
movq 0xb7f8(%rip), %rdi      # 0xdff0
callq 0x2080
leaq 0x30(%rsp), %rdi
callq 0x5be4
leaq 0xf0(%rsp), %rdi
callq 0x5008
leaq 0x48(%rsp), %rdi
callq 0x59a8
addq $0x14e8, %rsp           # imm = 0x14E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x2860
movq %rax, %r14
jmp 0x286d
jmp 0x2844
jmp 0x2844
jmp 0x2844
jmp 0x2849
jmp 0x2849
movq %rax, %r14
jmp 0x2856
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x5008
leaq 0x30(%rsp), %rdi
callq 0x5be4
leaq 0xf0(%rsp), %rdi
callq 0x5008
leaq 0x48(%rsp), %rdi
callq 0x59a8
movq %r14, %rdi
callq 0x2240