// File: /workspace/github_cpp_makefile_star2_O1/cegme[P]vose/Vose.cc
// Function: Vose::init()
void Vose::init() {

  // Reset the random number generators
  reset_scores_die();
  reset_random_double();

  // clear alias array
  //vose_alias.clear();
  alias.resize(scores.size(), 0);

  // clear prob array
  //vose_prob.clear();
  prob.resize(scores.size(), 0);

  // Work lists
  std::list<size_t> small;
  std::list<size_t> large;

  size_t n = scores.size();

  std::vector<double> probs_list(n);
  for(size_t i = 0; i != n; ++i) {
    // Compute the weighted probabilies, times the size
    double prob = (double) n * scores[i].weight / total_weight;
    probs_list[i] = prob;
    //std:: cerr << "probs_list[" << i << "]: " << probs_list[i] << "\n";

    // Build work lists
    if(prob < 1) 
      small.push_back(i);
    else
      large.push_back(i);
  }

  while(!small.empty() && !large.empty()) {
    size_t from_small = small.front(); small.pop_front();
    size_t from_large = large.front(); large.pop_front();
    
    prob[from_small] = probs_list[from_small];
    alias[from_small] = from_large;

    probs_list[from_large] += probs_list[from_small] - 1;

    if(probs_list[from_large] < 1)
      small.push_front(from_large);
    else
      large.push_front(from_large);
  }

  while(!small.empty()) {
    size_t from_small = small.front(); small.pop_front();
    prob[from_small] = 1;
  }
  while (!large.empty()) {
    size_t from_large = large.front(); large.pop_front();
    prob[from_large] =  1;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r12
movq 0x48(%rdi), %rax
movq 0x40(%rdi), %rcx
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
subq %rcx, %rax
je 0x6e2a
sarq $0x4, %rax
imulq %r13, %rax
movq %rax, %xmm1
punpckldq 0x420c(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4214(%rip), %xmm1     # 0xb030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
jmp 0x6e32
movsd 0x420e(%rip), %xmm0     # 0xb040
xorl %r15d, %r15d
leaq -0x48(%rbp), %rbx
movq %r15, (%rbx)
movsd %xmm0, 0x8(%rbx)
movl $0x2a, %r14d
movq %r14, 0x10(%rbx)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x45dc
movq %r15, (%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rbx)
movq %r14, 0x10(%rbx)
leaq 0x20(%r12), %rdi
movq %rbx, %rsi
callq 0x45dc
movq 0x48(%r12), %rsi
subq 0x40(%r12), %rsi
leaq 0x60(%r12), %rdi
sarq $0x4, %rsi
imulq %r13, %rsi
movq %r15, (%rbx)
movq %rdi, -0x68(%rbp)
movq %rbx, %rdx
callq 0x77e6
movq 0x48(%r12), %rsi
subq 0x40(%r12), %rsi
leaq 0x78(%r12), %r14
sarq $0x4, %rsi
imulq %r13, %rsi
movq %r15, (%rbx)
movq %r14, %rdi
movq %rbx, %rdx
callq 0x7820
movq %rbx, 0x8(%rbx)
movq %rbx, (%rbx)
movq %r15, 0x10(%rbx)
leaq -0x60(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %r15, 0x10(%rax)
movq 0x40(%r12), %rbx
movq 0x48(%r12), %r15
movq %r13, %rax
movq %r15, %r13
subq %rbx, %r13
sarq $0x4, %r13
imulq %rax, %r13
leaq -0x80(%rbp), %rdi
leaq -0x29(%rbp), %rdx
movq %r13, %rsi
callq 0x785a
cmpq %rbx, %r15
je 0x6fcb
movq %r13, %xmm0
punpckldq 0x4102(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x410a(%rip), %xmm0     # 0xb030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x90(%rbp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x40(%r12), %rax
xorps %xmm0, %xmm0
cvtsi2sdq (%rax,%r15), %xmm0
mulsd -0x90(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x58(%r12), %xmm1
divsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd 0x40cc(%rip), %xmm1     # 0xb040
ucomisd %xmm0, %xmm1
jbe 0x6f9a
movl $0x18, %edi
callq 0x21e0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2050
leaq -0x38(%rbp), %rax
jmp 0x6fb8
movl $0x18, %edi
callq 0x21e0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
leaq -0x60(%rbp), %rsi
callq 0x2050
leaq -0x50(%rbp), %rax
incq (%rax)
incq %rbx
addq $0x30, %r15
cmpq %rbx, %r13
jne 0x6f3f
movq -0x48(%rbp), %r12
leaq -0x60(%rbp), %rax
cmpq %rax, -0x60(%rbp)
sete %al
leaq -0x48(%rbp), %r13
cmpq %r13, %r12
movq -0x68(%rbp), %r13
je 0x70cd
testb %al, %al
jne 0x70cd
movq 0x10(%r12), %r15
decq -0x38(%rbp)
movq %r12, %rdi
callq 0x2280
movq %r12, %rdi
callq 0x21c0
movq -0x60(%rbp), %r12
movq 0x10(%r12), %rbx
decq -0x50(%rbp)
movq %r12, %rdi
callq 0x2280
movq %r12, %rdi
callq 0x21c0
movq -0x80(%rbp), %rax
movsd (%rax,%r15,8), %xmm0
movq (%r14), %rcx
movsd %xmm0, (%rcx,%r15,8)
movq (%r13), %rcx
movq %rbx, (%rcx,%r15,8)
movsd (%rax,%r15,8), %xmm0
addsd 0x41e6(%rip), %xmm0     # 0xb238
addsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbx,8)
movsd 0x3fdc(%rip), %xmm1     # 0xb040
ucomisd %xmm0, %xmm1
jbe 0x708d
movq -0x48(%rbp), %r12
movl $0x18, %edi
callq 0x21e0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq %r12, %rsi
callq 0x2050
leaq -0x38(%rbp), %rax
jmp 0x70ae
movq -0x60(%rbp), %r12
movl $0x18, %edi
callq 0x21e0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq %r12, %rsi
callq 0x2050
leaq -0x50(%rbp), %rax
incq (%rax)
movq -0x48(%rbp), %r12
leaq -0x60(%rbp), %rax
cmpq %rax, -0x60(%rbp)
sete %al
leaq -0x48(%rbp), %rcx
cmpq %rcx, %r12
jne 0x6feb
movq -0x48(%rbp), %r15
leaq -0x48(%rbp), %r13
cmpq %r13, %r15
movabsq $0x3ff0000000000000, %r12 # imm = 0x3FF0000000000000
je 0x710c
movq 0x10(%r15), %rbx
decq -0x38(%rbp)
movq %r15, %rdi
callq 0x2280
movq %r15, %rdi
callq 0x21c0
movq (%r14), %rax
movq %r12, (%rax,%rbx,8)
movq -0x48(%rbp), %r15
cmpq %r13, %r15
jne 0x70e4
movq -0x60(%rbp), %r15
movq %r12, %r13
leaq -0x60(%rbp), %r12
cmpq %r12, %r15
je 0x7141
movq 0x10(%r15), %rbx
decq -0x50(%rbp)
movq %r15, %rdi
callq 0x2280
movq %r15, %rdi
callq 0x21c0
movq (%r14), %rax
movq %r13, (%rax,%rbx,8)
movq -0x60(%rbp), %r15
jmp 0x7117
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x714f
callq 0x21c0
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
leaq -0x48(%rbp), %r14
je 0x716c
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x715c
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x7185
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x7175
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x71ac
jmp 0x719b
movq %rax, %r14
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x71ac
callq 0x21c0
movq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %r15
cmpq %r15, %rdi
je 0x71c9
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x71b9
movq -0x48(%rbp), %rdi
leaq -0x48(%rbp), %r15
cmpq %r15, %rdi
je 0x71e6
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x71d6
movq %r14, %rdi
callq 0x22b0