// File: /workspace/github_cpp_makefile_star2_O2/redpony[P]cpyp/crp_test.cc
// Function: test_mh1a()
void test_mh1a() {
  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
      double lq_old = 0;
      double lp_old = llh(crp, p0);
      if (s > 0) crp.decrement(y_i, eng, &lq_old);

      double lq_new = 0;
      crp.increment_no_base(y_i, eng, &lq_new);
      if (s > 0) {
        double lp_new = llh(crp, p0);
        double a = exp(lp_new - lp_old + lq_old - lq_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 $0x14f8, %rsp           # imm = 0x14F8
leaq 0x170(%rsp), %rdi
callq 0x3f30
leaq 0x68a4(%rip), %rsi      # 0x9148
pushq $0xf
popq %rdx
leaq 0xf8(%rsp), %rax
movq %rdx, %rcx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x58(%rsp), %rdi
leaq 0x80(%rsp), %rcx
movq %rax, %rsi
callq 0x3f4a
leaq 0xf8(%rsp), %rdi
movsd 0x672b(%rip), %xmm0     # 0x9008
movsd 0x672b(%rip), %xmm1     # 0x9010
callq 0x3f80
leaq 0x80(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x40(%rsp), %rdi
pushq $0xf
popq %rsi
leaq 0x28(%rsp), %rcx
callq 0x3fe8
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0xf8(%rsp), %r15
leaq 0x34(%rsp), %r13
leaq 0x170(%rsp), %rbp
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
cmpl $0x30d40, %r14d         # imm = 0x30D40
je 0x2aa4
xorl %ebx, %ebx
pushq $0xf
popq %r12
movl %ebx, %eax
xorl %edx, %edx
pushq $0x3
popq %rcx
divl %ecx
subl $0x1, %r12d
jb 0x2a60
movl %edx, 0x34(%rsp)
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x403e
andq $0x0, 0x28(%rsp)
movq %r15, %rdi
leaq 0x67b1(%rip), %rsi      # 0x9130
callq 0x23b1
movsd %xmm0, 0x8(%rsp)
testl %r14d, %r14d
je 0x29a2
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
callq 0x4086
andq $0x0, 0x38(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x38(%rsp), %rcx
callq 0x42ea
testl %r14d, %r14d
je 0x2a4c
movq %r15, %rdi
leaq 0x6762(%rip), %rsi      # 0x9130
callq 0x23b1
subsd 0x8(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
callq 0x2250
movapd %xmm0, %xmm2
movsd 0x661a(%rip), %xmm0     # 0x9010
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm2, 0x8(%rsp)
ucomisd %xmm0, %xmm2
jae 0x2a26
movq %rbp, %rdi
callq 0x427a
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2a3c
movsd 0x18(%rsp), %xmm0
addsd 0x65dc(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x18(%rsp)
jmp 0x2a4c
movq %r15, %rdi
leaq 0x80(%rsp), %rsi
callq 0x429b
leaq 0x88(%rsp), %rdi
callq 0x5008
incl %ebx
jmp 0x2948
cmpl $0x12d, %r14d           # imm = 0x12D
setae %al
movl %r14d, %ecx
notl %ecx
testb $0x3, %cl
sete %cl
testb %cl, %al
je 0x2a9c
movsd 0x20(%rsp), %xmm0
addsd 0x6589(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x20(%rsp)
movl 0xf8(%rsp), %eax
movq 0x40(%rsp), %rcx
incl (%rcx,%rax,4)
incl %r14d
jmp 0x2935
movq 0xb545(%rip), %rdi      # 0xdff0
leaq 0x68f7(%rip), %rsi      # 0x93a9
callq 0x2170
movsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rax, %rdi
callq 0x2200
movq %rax, %rdi
callq 0x2080
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r12
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r13d, %r13d
movq 0xb4ff(%rip), %rbx      # 0xdff0
leaq 0x68be(%rip), %r14      # 0x93b6
movsd %xmm0, 0x10(%rsp)
leaq (%r15,%r13), %rax
cmpq %r12, %rax
je 0x2b84
cvtsi2sdl (%rax), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, 0x18(%rsp)
subsd (%rax,%r13,2), %xmm0
movq %rbx, %rdi
movapd %xmm0, 0x70(%rsp)
callq 0x2200
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x2200
movq %rax, %rdi
callq 0x2080
movapd 0x70(%rsp), %xmm1
andpd 0x6541(%rip), %xmm1     # 0x90a0
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
maxsd 0x10(%rsp), %xmm1
addq $0x4, %r13
movsd %xmm1, 0x10(%rsp)
jmp 0x2afe
movq 0xb465(%rip), %rdi      # 0xdff0
leaq 0x6826(%rip), %rsi      # 0x93b8
callq 0x2170
movsd 0x8(%rsp), %xmm0
divsd 0x6473(%rip), %xmm0     # 0x9018
movsd %xmm0, 0x8(%rsp)
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2200
movsd 0x8(%rsp), %xmm0
ucomisd 0x6459(%rip), %xmm0     # 0x9020
jbe 0x2bdc
movq 0xb420(%rip), %rdi      # 0xdff0
leaq 0x67f1(%rip), %rsi      # 0x93c8
callq 0x2170
movq 0xb40d(%rip), %rdi      # 0xdff0
callq 0x2080
leaq 0x67ea(%rip), %rsi      # 0x93d9
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2200
movsd 0x10(%rsp), %xmm0
ucomisd 0x640d(%rip), %xmm0     # 0x9020
jbe 0x2c28
movq 0xb3d4(%rip), %rdi      # 0xdff0
leaq 0x67a5(%rip), %rsi      # 0x93c8
callq 0x2170
movq 0xb3c1(%rip), %rdi      # 0xdff0
callq 0x2080
leaq 0x40(%rsp), %rdi
callq 0x5be4
leaq 0x100(%rsp), %rdi
callq 0x5008
leaq 0x58(%rsp), %rdi
callq 0x59a8
addq $0x14f8, %rsp           # imm = 0x14F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2c9c
movq %rax, %rbx
jmp 0x2ca9
jmp 0x2c7b
jmp 0x2c7b
jmp 0x2c7b
jmp 0x2c82
jmp 0x2c82
movq %rax, %rbx
jmp 0x2c92
jmp 0x2c82
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x5008
leaq 0x40(%rsp), %rdi
callq 0x5be4
leaq 0x100(%rsp), %rdi
callq 0x5008
leaq 0x58(%rsp), %rdi
callq 0x59a8
movq %rbx, %rdi
callq 0x2240