// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1558, %rsp           # imm = 0x1558
leaq 0x1d0(%rsp), %rdi
callq 0x4c22
leaq 0x7c14(%rip), %rsi      # 0xa178
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 0x4ca0
leaq 0x68(%rsp), %rdi
movsd 0x7a6d(%rip), %xmm0     # 0xa008
movsd 0x7a6d(%rip), %xmm1     # 0xa010
callq 0x4cde
movl $0x3c, %edi
callq 0x21b0
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x2c(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
leaq 0xe8(%rsp), %r14
leaq 0x70(%rsp), %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd %xmm0, 0x20(%rsp)
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
movq %rax, 0x18(%rsp)
movl $0xf, %r12d
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, 0xc(%rsp)
movq 0x68(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %rax, 0xe0(%rsp)
xorl %edx, %edx
movq %rdx, 0xe8(%rsp)
movq %rcx, 0xf0(%rsp)
movq %rdx, 0xf8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x100(%rsp)
movups 0x20(%r15), %xmm0
movups %xmm0, 0x20(%r14)
movq %rdx, 0x118(%rsp)
movq %r14, 0x158(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x158(%rsp), %rdx
callq 0x5b0e
movq 0x68(%r15), %rax
movq %rax, 0x68(%r14)
movupd 0x38(%r15), %xmm0
movupd 0x48(%r15), %xmm1
movupd 0x58(%r15), %xmm2
movupd %xmm2, 0x58(%r14)
movupd %xmm1, 0x48(%r14)
movupd %xmm0, 0x38(%r14)
testl %ebx, %ebx
je 0x26d5
leaq 0x68(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x4d48
testl %eax, %eax
setne %bpl
jmp 0x26d7
xorl %ebp, %ebp
movslq 0xc(%rsp), %rax
leaq 0x7a5d(%rip), %rcx      # 0xa140
leaq (%rcx,%rax,8), %rdx
leaq 0x68(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x1d0(%rsp), %rcx
callq 0x4e9e
testl %ebx, %ebx
je 0x2898
movsd 0x7902(%rip), %xmm0     # 0xa010
movapd %xmm0, %xmm3
testl %eax, %eax
je 0x2733
movslq 0xc(%rsp), %rax
leaq 0x7a3e(%rip), %rcx      # 0xa160
movsd (%rcx,%rax,8), %xmm3
leaq 0x7a12(%rip), %rcx      # 0xa140
movsd (%rcx,%rax,8), %xmm0
movsd 0x78d5(%rip), %xmm2     # 0xa010
movapd %xmm2, %xmm1
testb %bpl, %bpl
je 0x2761
movslq 0xc(%rsp), %rax
leaq 0x7a10(%rip), %rcx      # 0xa160
movsd (%rcx,%rax,8), %xmm1
leaq 0x79e4(%rip), %rcx      # 0xa140
movsd (%rcx,%rax,8), %xmm2
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm0, %xmm3
movsd 0x789b(%rip), %xmm0     # 0xa010
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm3, 0x40(%rsp)
ucomisd %xmm0, %xmm3
movq 0x18(%rsp), %rbp
jae 0x27af
leaq 0x1d0(%rsp), %rdi
callq 0x78bf
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x27c8
movsd 0x20(%rsp), %xmm0
addsd 0x7853(%rip), %xmm0     # 0xa010
movsd %xmm0, 0x20(%rsp)
jmp 0x289d
movq 0x68(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x7b4c
movq 0x68(%r15), %rax
movq %rax, 0x68(%rbp)
movups 0x38(%r15), %xmm0
movups 0x48(%r15), %xmm1
movups 0x58(%r15), %xmm2
movups %xmm2, 0x58(%rbp)
movups %xmm1, 0x48(%rbp)
movups %xmm0, 0x38(%rbp)
movq 0xe0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x22c0
movq 0x68(%r14), %rax
movq %rax, 0x68(%r15)
movups 0x38(%r14), %xmm0
movups 0x48(%r14), %xmm1
movups 0x58(%r14), %xmm2
movups %xmm2, 0x58(%r15)
movups %xmm1, 0x48(%r15)
movups %xmm0, 0x38(%r15)
movq 0x158(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x22c0
movq 0x68(%rbp), %rax
movq %rax, 0x68(%r14)
movupd 0x38(%rbp), %xmm0
movupd 0x48(%rbp), %xmm1
movupd 0x58(%rbp), %xmm2
movupd %xmm2, 0x58(%r14)
movupd %xmm1, 0x48(%r14)
movupd %xmm0, 0x38(%r14)
movq %rbp, %rdi
callq 0x5d20
movq 0x18(%rsp), %rbp
movq %r14, %rdi
callq 0x5d20
incl %r13d
decl %r12d
jne 0x25ff
cmpl $0x12d, %ebx            # imm = 0x12D
setae %al
movl %ebx, %ecx
notl %ecx
testb $0x3, %cl
sete %cl
testb %cl, %al
je 0x28e4
movsd 0x38(%rsp), %xmm0
addsd 0x773a(%rip), %xmm0     # 0xa010
movsd %xmm0, 0x38(%rsp)
movl 0x68(%rsp), %eax
incl (%rbp,%rax,4)
incl %ebx
cmpl $0x30d40, %ebx          # imm = 0x30D40
jne 0x25f6
movq 0xb6f7(%rip), %rdi      # 0xdff0
leaq 0x7ad8(%rip), %rsi      # 0xa3d8
movl $0xc, %edx
callq 0x21c0
movsd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq 0xb6d3(%rip), %rdi      # 0xdff0
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 %r12d, %r12d
movq 0xb696(%rip), %rbx      # 0xdff0
leaq 0x7a84(%rip), %r14      # 0xa3e5
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm1, 0x48(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl (%rbp,%r12), %xmm0
divsd 0x38(%rsp), %xmm0
movq 0x50(%rsp), %rax
movsd %xmm0, 0x40(%rsp)
subsd (%rax,%r12,2), %xmm0
movq %rbx, %rdi
movapd %xmm0, 0x20(%rsp)
callq 0x2230
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq %r15, %rdi
movsd 0x40(%rsp), %xmm0
callq 0x2230
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movapd 0x20(%rsp), %xmm0
andpd 0x76a7(%rip), %xmm0     # 0xa0a0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
maxsd 0x48(%rsp), %xmm0
addq $0x4, %r12
movapd %xmm0, %xmm1
cmpq $0x3c, %r12
jne 0x296b
movapd %xmm0, 0x20(%rsp)
movq 0xb5c2(%rip), %rdi      # 0xdff0
leaq 0x79b2(%rip), %rsi      # 0xa3e7
movl $0xf, %edx
callq 0x21c0
movsd 0x10(%rsp), %xmm0
divsd 0x75cb(%rip), %xmm0     # 0xa018
movsd %xmm0, 0x10(%rsp)
movq 0xb596(%rip), %rbx      # 0xdff0
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2230
movsd 0x10(%rsp), %xmm0
ucomisd 0x75aa(%rip), %xmm0     # 0xa020
jbe 0x2a90
movq 0xb571(%rip), %rdi      # 0xdff0
leaq 0x7971(%rip), %rsi      # 0xa3f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0xb542(%rip), %rbx      # 0xdff0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq %rax, %r14
leaq 0x7940(%rip), %rsi      # 0xa408
movl $0xf, %edx
movq %rax, %rdi
callq 0x21c0
movq %r14, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x2230
movapd 0x20(%rsp), %xmm0
ucomisd 0x752f(%rip), %xmm0     # 0xa020
jbe 0x2b0b
movq 0xb4f6(%rip), %rdi      # 0xdff0
leaq 0x78f6(%rip), %rsi      # 0xa3f7
movl $0x10, %edx
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0xb4c7(%rip), %rdi      # 0xdff0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq %rbp, %rdi
callq 0x2180
leaq 0x70(%rsp), %rdi
callq 0x5d20
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2b57
callq 0x2180
addq $0x1558, %rsp           # imm = 0x1558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2ba7
movq %rax, %rbx
jmp 0x2bb1
jmp 0x2b9c
jmp 0x2b9c
jmp 0x2b79
movq %rax, %rdi
callq 0x59a4
jmp 0x2b9c
jmp 0x2b85
movq %rax, %rbx
jmp 0x2b92
movq %rax, %rbx
movq 0x18(%rsp), %rbp
movq %r14, %rdi
callq 0x5d20
jmp 0x2b9f
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2180
leaq 0x70(%rsp), %rdi
callq 0x5d20
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2bc0
callq 0x2180
movq %rbx, %rdi
callq 0x2270