// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1558, %rsp           # imm = 0x1558
leaq 0x1d0(%rsp), %rdi
callq 0x4bf4
leaq 0x656e(%rip), %rsi      # 0x9178
leaq 0x158(%rsp), %rax
movl $0xf, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rcx
movl $0xf, %edx
movq %rax, %rsi
callq 0x4c72
leaq 0x68(%rsp), %rdi
movsd 0x63c7(%rip), %xmm0     # 0x9008
movsd 0x63c7(%rip), %xmm1     # 0x9010
callq 0x4cb0
movl $0x3c, %edi
callq 0x21b0
xorpd %xmm0, %xmm0
movupd %xmm0, 0x2c(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movq %rax, 0x18(%rsp)
movupd %xmm0, (%rax)
leaq 0xe8(%rsp), %r15
leaq 0x70(%rsp), %r14
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
movl $0xf, %ebp
xorl %r13d, %r13d
movl %r13d, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rcx, %rax
shrq $0x21, %rax
leal (%rax,%rax,2), %eax
movl %r13d, %ecx
subl %eax, %ecx
movl %ecx, 0x14(%rsp)
movq 0x68(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %rax, 0xe0(%rsp)
movq %rbx, 0xe8(%rsp)
movq %rcx, 0xf0(%rsp)
movq %rbx, 0xf8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x100(%rsp)
movups 0x20(%r14), %xmm0
movups %xmm0, 0x20(%r15)
movq %rbx, 0x118(%rsp)
movq %r15, 0x158(%rsp)
movq %r15, %rdi
movq %r14, %rsi
leaq 0x158(%rsp), %rdx
callq 0x5a68
movq 0x68(%r14), %rax
movq %rax, 0x68(%r15)
movupd 0x38(%r14), %xmm0
movupd 0x48(%r14), %xmm1
movupd 0x58(%r14), %xmm2
movupd %xmm2, 0x58(%r15)
movupd %xmm1, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
movq $0x0, 0x40(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x63f8(%rip), %rsi      # 0x9160
callq 0x24b1
movsd %xmm0, (%rsp)
testl %r12d, %r12d
je 0x2d93
leaq 0x68(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x4d1a
movq $0x0, 0x38(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x4fce
testl %r12d, %r12d
je 0x2f12
leaq 0x68(%rsp), %rdi
leaq 0x6393(%rip), %rsi      # 0x9160
callq 0x24b1
subsd (%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
callq 0x2280
movapd %xmm0, %xmm2
movsd 0x621c(%rip), %xmm0     # 0x9010
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd %xmm2, (%rsp)
ucomisd %xmm0, %xmm2
jae 0x2e27
leaq 0x1d0(%rsp), %rdi
callq 0x770d
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e40
movsd 0x8(%rsp), %xmm0
addsd 0x61db(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x8(%rsp)
jmp 0x2f12
movq 0x68(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x799e
movq 0x68(%r14), %rax
movq %rax, 0x68(%rbx)
movups 0x38(%r14), %xmm0
movups 0x48(%r14), %xmm1
movups 0x58(%r14), %xmm2
movups %xmm2, 0x58(%rbx)
movups %xmm1, 0x48(%rbx)
movups %xmm0, 0x38(%rbx)
movq 0xe0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x22c0
movq 0x68(%r15), %rax
movq %rax, 0x68(%r14)
movups 0x38(%r15), %xmm0
movups 0x48(%r15), %xmm1
movups 0x58(%r15), %xmm2
movups %xmm2, 0x58(%r14)
movups %xmm1, 0x48(%r14)
movups %xmm0, 0x38(%r14)
movq 0x158(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22c0
movq 0x68(%rbx), %rax
movq %rax, 0x68(%r15)
movupd 0x38(%rbx), %xmm0
movupd 0x48(%rbx), %xmm1
movupd 0x58(%rbx), %xmm2
movupd %xmm2, 0x58(%r15)
movupd %xmm1, 0x48(%r15)
movupd %xmm0, 0x38(%r15)
movq %rbx, %rdi
callq 0x5c7a
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x5c7a
incl %r13d
decl %ebp
jne 0x2ca4
cmpl $0x12d, %r12d           # imm = 0x12D
setae %al
movl %r12d, %ecx
notl %ecx
testb $0x3, %cl
sete %cl
testb %cl, %al
je 0x2f5e
movsd 0x30(%rsp), %xmm0
addsd 0x60c4(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x30(%rsp)
movl 0x68(%rsp), %eax
movq 0x18(%rsp), %rcx
incl (%rcx,%rax,4)
incl %r12d
cmpl $0x30d40, %r12d         # imm = 0x30D40
jne 0x2c9c
movq 0xa07b(%rip), %rdi      # 0xcff0
leaq 0x645c(%rip), %rsi      # 0x93d8
movl $0xc, %edx
callq 0x21c0
movsd 0x8(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0xa057(%rip), %rdi      # 0xcff0
callq 0x2230
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movq 0xa01a(%rip), %rbx      # 0xcff0
leaq 0x6408(%rip), %r15      # 0x93e5
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movsd %xmm1, 0x48(%rsp)
movq 0x18(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%r13), %xmm0
divsd 0x30(%rsp), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, 0x8(%rsp)
subsd (%rax,%r13,2), %xmm0
movq %rbx, %rdi
movapd %xmm0, 0x20(%rsp)
callq 0x2230
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2230
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movapd 0x20(%rsp), %xmm0
andpd 0x6028(%rip), %xmm0     # 0x90a0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
maxsd 0x48(%rsp), %xmm0
addq $0x4, %r13
movapd %xmm0, %xmm1
cmpq $0x3c, %r13
jne 0x2fe6
movapd %xmm0, 0x20(%rsp)
movq 0x9f45(%rip), %rdi      # 0xcff0
leaq 0x6335(%rip), %rsi      # 0x93e7
movl $0xf, %edx
callq 0x21c0
movsd (%rsp), %xmm0
divsd 0x5f4f(%rip), %xmm0     # 0x9018
movsd %xmm0, (%rsp)
movq 0x9f1b(%rip), %rbx      # 0xcff0
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x2230
movsd (%rsp), %xmm0
ucomisd 0x5f31(%rip), %xmm0     # 0x9020
jbe 0x3109
movq 0x9ef8(%rip), %rdi      # 0xcff0
leaq 0x62f8(%rip), %rsi      # 0x93f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x9ec9(%rip), %rbx      # 0xcff0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq %rax, %r15
leaq 0x62c7(%rip), %rsi      # 0x9408
movl $0xf, %edx
movq %rax, %rdi
callq 0x21c0
movq %r15, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x2230
movapd 0x20(%rsp), %xmm0
ucomisd 0x5eb6(%rip), %xmm0     # 0x9020
jbe 0x3184
movq 0x9e7d(%rip), %rdi      # 0xcff0
leaq 0x627d(%rip), %rsi      # 0x93f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x9e4e(%rip), %rdi      # 0xcff0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x18(%rsp), %rdi
callq 0x2180
movq %r14, %rdi
callq 0x5c7a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x31d0
callq 0x2180
addq $0x1558, %rsp           # imm = 0x1558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x321c
movq %rax, %rbx
jmp 0x3226
jmp 0x3202
jmp 0x3202
jmp 0x31f2
movq %rax, %rdi
callq 0x58fe
jmp 0x3202
jmp 0x3207
jmp 0x3207
jmp 0x3207
movq %rax, %rbx
jmp 0x3212
movq %rax, %rbx
movq %r15, %rdi
callq 0x5c7a
movq 0x18(%rsp), %rdi
callq 0x2180
leaq 0x70(%rsp), %rdi
callq 0x5c7a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3235
callq 0x2180
movq %rbx, %rdi
callq 0x2270