// File: /workspace/github_cpp_makefile_star2_O3/cegme[P]vose/test.cc
// Function: test::test2()
bool test::test2() {
  std::cerr << "Check that the vose data structure comes close to actual values.\n";

  Vose vose;
  vose.add("red", 5);
  vose.add("blue", 10);
  vose.add("yellow", 20);

  vose.init();
  
  std::cerr << "Vose Dist: ";
  for (size_t i = 0; i != vose.scores.size(); ++i) {
    std::cerr << "\t" << i << "-->" << vose.scores[i].weight << "\n";
  }
  std::cerr << "\n";
  for (size_t i = 0; i != vose.probabilities().size(); ++i) {
    std::cerr << "\t" << i << "-->" << vose.probabilities()[i] << "\n";
  }
  std::cerr << "\n";

  std::cerr << "Create a histogram by sampling the vose structure.\n";
  std::map<size_t,int> histogram;
  auto hist_sum = 0L;
  for (int i = 0; i != 50000; ++i) {
    size_t idx = vose.rand();

    if (histogram.find(idx) != histogram.end()) {
      histogram[idx] += 1;
      hist_sum += 1L;
    }
    else {
      histogram.insert(std::make_pair(idx, 1));
      hist_sum += 1L;
    }
  }

  std::cerr << "Sampled Distribution:\n";

  // Create the new dist
  std::vector<double> new_dist;
  for (auto& h : histogram) {
    std::cerr << "\t" << h.first <<  "-->" << (double)h.second/hist_sum << "\n";
    new_dist.push_back((double)h.second/hist_sum);
  }

  double kl_result = Vose::kl(vose.probabilities(), new_dist); 
  std::cerr << "kl(probabilities, new_dist): " << kl_result << "\n";
  std::cerr << "kl(new_dist, probabilities): " << Vose::kl(new_dist, vose.probabilities()) << "\n";

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq 0x8179(%rip), %rdi      # 0xfff0
leaq 0x3529(%rip), %rsi      # 0xb3a7
movl $0x41, %edx
callq 0x2200
leaq -0x128(%rbp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x2100
leaq -0x178(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x347c(%rip), %rsi      # 0xb329
leaq 0x3478(%rip), %rdx      # 0xb32c
leaq -0x188(%rbp), %rdi
callq 0xa378
leaq -0x128(%rbp), %rdi
leaq -0x188(%rbp), %rsi
movl $0x5, %edx
callq 0x6874
movq -0x188(%rbp), %rdi
cmpq %r14, %rdi
je 0x7ee9
callq 0x21c0
leaq -0x158(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x3605(%rip), %rsi      # 0xb500
leaq 0x3602(%rip), %rdx      # 0xb504
leaq -0x168(%rbp), %rdi
callq 0xa378
leaq -0x128(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movl $0xa, %edx
callq 0x6874
movq -0x168(%rbp), %rdi
cmpq %r14, %rdi
je 0x7f37
callq 0x21c0
leaq -0x138(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x3438(%rip), %rsi      # 0xb381
leaq 0x3437(%rip), %rdx      # 0xb387
leaq -0x148(%rbp), %rdi
callq 0xa378
leaq -0x128(%rbp), %rdi
leaq -0x148(%rbp), %rsi
movl $0x14, %edx
callq 0x6874
movq -0x148(%rbp), %rdi
cmpq %r14, %rdi
je 0x7f85
callq 0x21c0
leaq -0x128(%rbp), %rdi
callq 0x6984
movq 0x8058(%rip), %rdi      # 0xfff0
leaq 0x344a(%rip), %rsi      # 0xb3e9
movl $0xb, %edx
callq 0x2200
movq -0xe0(%rbp), %rax
cmpq -0xe8(%rbp), %rax
je 0x805a
xorl %r15d, %r15d
movq 0x8029(%rip), %rbx      # 0xfff0
leaq 0x359e(%rip), %r12      # 0xb56c
xorl %r13d, %r13d
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x3415(%rip), %rsi      # 0xb3f5
callq 0x2200
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2120
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0x33f5(%rip), %rsi      # 0xb3f7
callq 0x2200
movq -0xe8(%rbp), %rax
movq (%rax,%r15), %rsi
movq %r14, %rdi
callq 0x21d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2200
incq %r13
movq -0xe0(%rbp), %rax
subq -0xe8(%rbp), %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
addq $0x30, %r15
cmpq %rax, %r13
jne 0x7fd1
movq 0x7f8f(%rip), %rdi      # 0xfff0
leaq 0x3504(%rip), %rsi      # 0xb56c
movl $0x1, %edx
callq 0x2200
xorl %r14d, %r14d
leaq -0x128(%rbp), %r12
movq 0x7f6d(%rip), %r15      # 0xfff0
xorpd %xmm0, %xmm0
movapd %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq %r12, %rcx
callq 0xa01b
movq -0x90(%rbp), %rbx
movq -0x88(%rbp), %r13
testq %rbx, %rbx
je 0x80cf
movq %rbx, %rdi
callq 0x21c0
subq %rbx, %r13
sarq $0x3, %r13
cmpq %r13, %r14
je 0x8188
movl $0x1, %edx
movq %r15, %rdi
leaq 0x3307(%rip), %rsi      # 0xb3f5
callq 0x2200
movq %r15, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x32e7(%rip), %rsi      # 0xb3f7
callq 0x2200
xorpd %xmm0, %xmm0
movapd %xmm0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
leaq -0x90(%rbp), %rdx
movq %r12, %rcx
callq 0xa01b
movq -0x90(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
movq %rbx, %rdi
callq 0x2260
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3402(%rip), %rsi      # 0xb56c
callq 0x2200
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x8180
callq 0x21c0
incq %r14
jmp 0x8083
movq 0x7e61(%rip), %rdi      # 0xfff0
leaq 0x33d6(%rip), %rsi      # 0xb56c
movl $0x1, %edx
callq 0x2200
movq 0x7e49(%rip), %rdi      # 0xfff0
leaq 0x324d(%rip), %rsi      # 0xb3fb
movl $0x33, %edx
callq 0x2200
leaq -0x88(%rbp), %r12
movl $0x0, (%r12)
xorl %r13d, %r13d
movq %r13, 0x8(%r12)
movq %r12, 0x10(%r12)
movq %r12, 0x18(%r12)
movq %r13, 0x20(%r12)
leaq -0x128(%rbp), %rbx
leaq -0x90(%rbp), %r14
leaq -0x60(%rbp), %r15
movq %rbx, %rdi
callq 0x6daa
movq %rax, -0x40(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x823e
movq %r12, %r8
movq %r8, %rcx
movq %rdi, %rdx
movq 0x20(%rdi), %rsi
xorl %edi, %edi
cmpq %rax, %rsi
setb %dil
movq %rdx, %r8
cmovbq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x8208
cmpq %r12, %r8
je 0x823e
cmpq %rax, %rsi
cmovbq %rcx, %rdx
cmpq 0x20(%rdx), %rax
jae 0x8262
movq %rax, -0x60(%rbp)
movl $0x1, -0x58(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa620
incq %r13
cmpq $0xc350, %r13           # imm = 0xC350
jne 0x81f0
jmp 0x8272
movq %r14, %rdi
leaq -0x40(%rbp), %rsi
callq 0x9f6e
incl (%rax)
jmp 0x8254
movq 0x7d77(%rip), %rdi      # 0xfff0
leaq 0x31af(%rip), %rsi      # 0xb42f
movl $0x16, %edx
callq 0x2200
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x78(%rbp), %rbx
cmpq %r12, %rbx
je 0x8363
movq 0x7d41(%rip), %r14      # 0xfff0
leaq 0x32b6(%rip), %r13      # 0xb56c
movl $0x1, %edx
movq %r14, %rdi
leaq 0x3130(%rip), %rsi      # 0xb3f5
callq 0x2200
movq 0x20(%rbx), %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %r15
movl $0x3, %edx
movq %rax, %rdi
leaq 0x310f(%rip), %rsi      # 0xb3f7
callq 0x2200
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%rbx), %xmm0
divsd 0x2fbb(%rip), %xmm0     # 0xb2b8
movq %r15, %rdi
callq 0x2260
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2200
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%rbx), %xmm0
divsd 0x2f93(%rip), %xmm0     # 0xb2b8
movsd %xmm0, -0x40(%rbp)
movq -0x58(%rbp), %rsi
cmpq -0x50(%rbp), %rsi
je 0x8342
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, -0x58(%rbp)
jmp 0x834f
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x74b4
movq %rbx, %rdi
callq 0x2130
movq %rax, %rbx
cmpq %r12, %rax
jne 0x82b6
leaq -0x40(%rbp), %rdx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
leaq -0x128(%rbp), %rcx
movq 0x40(%rcx), %rdi
movq 0x48(%rcx), %rsi
callq 0xa01b
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x6e94
movsd %xmm0, -0x98(%rbp)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x83ae
callq 0x21c0
movq 0x7c3b(%rip), %rdi      # 0xfff0
leaq 0x308a(%rip), %rsi      # 0xb446
movl $0x1d, %edx
callq 0x2200
movq 0x7c23(%rip), %rdi      # 0xfff0
movsd -0x98(%rbp), %xmm0
callq 0x2260
leaq 0x318b(%rip), %rsi      # 0xb56c
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq 0x7bfb(%rip), %rdi      # 0xfff0
leaq 0x3068(%rip), %rsi      # 0xb464
movl $0x1d, %edx
callq 0x2200
xorpd %xmm0, %xmm0
leaq -0x40(%rbp), %rdx
movapd %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
leaq -0x128(%rbp), %rcx
movq 0x40(%rcx), %rdi
movq 0x48(%rcx), %rsi
callq 0xa01b
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x6e94
movq 0x7bae(%rip), %rdi      # 0xfff0
callq 0x2260
leaq 0x311e(%rip), %rsi      # 0xb56c
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x8469
callq 0x21c0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8477
callq 0x21c0
leaq -0x90(%rbp), %rdi
callq 0xa0f8
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x8494
callq 0x21c0
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x84a5
callq 0x21c0
leaq -0xe8(%rbp), %rdi
callq 0x46c4
movq -0xf8(%rbp), %rax
testq %rax, %rax
je 0x84ce
leaq -0x108(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x118(%rbp), %rax
testq %rax, %rax
je 0x84eb
leaq -0x128(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movb $0x1, %al
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8501
movq %rax, %rdi
callq 0x4411
jmp 0x8540
jmp 0x8540
jmp 0x8540
jmp 0x855f
movq %rax, %rbx
movq -0x148(%rbp), %rdi
jmp 0x8537
jmp 0x859b
movq %rax, %rbx
movq -0x168(%rbp), %rdi
jmp 0x8537
jmp 0x859b
movq %rax, %rbx
movq -0x188(%rbp), %rdi
cmpq %r14, %rdi
jne 0x8573
jmp 0x859e
jmp 0x859b
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x857f
callq 0x21c0
jmp 0x857f
jmp 0x857c
jmp 0x859b
jmp 0x857c
jmp 0x855f
jmp 0x8564
jmp 0x8564
movq %rax, %rbx
jmp 0x858d
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x859e
callq 0x21c0
jmp 0x859e
jmp 0x859b
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x858d
callq 0x21c0
leaq -0x90(%rbp), %rdi
callq 0xa0f8
jmp 0x859e
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
callq 0x405e
movq %rbx, %rdi
callq 0x22b0