// File: /workspace/github_cpp_makefile_star2_O3/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 0x69da
sarq $0x4, %rax
imulq %r13, %rax
movq %rax, %xmm1
punpckldq 0x465c(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4664(%rip), %xmm1     # 0xb030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
jmp 0x69e2
movsd 0x465e(%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 0x42c4
movq %r15, (%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rbx)
movq %r14, 0x10(%rbx)
leaq 0x20(%r12), %rdi
movq %rbx, %rsi
callq 0x42c4
movq 0x48(%r12), %rsi
subq 0x40(%r12), %rsi
leaq 0x60(%r12), %r14
sarq $0x4, %rsi
imulq %r13, %rsi
movq %r15, (%rbx)
movq %r14, %rdi
movq %rbx, %rdx
callq 0x73a8
movq 0x48(%r12), %rsi
subq 0x40(%r12), %rsi
leaq 0x78(%r12), %rdi
sarq $0x4, %rsi
imulq %r13, %rsi
movq %r15, (%rbx)
movq %rdi, -0x68(%rbp)
movq %rbx, %rdx
callq 0x73e2
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 0x741c
cmpq %rbx, %r15
je 0x6b7c
movq %r13, %xmm0
punpckldq 0x4552(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x455a(%rip), %xmm0     # 0xb030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x90(%rbp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x40(%r12), %rax
xorps %xmm0, %xmm0
cvtsi2sdq (%rax,%rbx), %xmm0
mulsd -0x90(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x58(%r12), %xmm1
divsd %xmm1, %xmm0
movq -0x80(%rbp), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x451b(%rip), %xmm1     # 0xb040
ucomisd %xmm0, %xmm1
jbe 0x6b4b
movl $0x18, %edi
callq 0x21e0
movq %r15, 0x10(%rax)
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2050
leaq -0x38(%rbp), %rax
jmp 0x6b69
movl $0x18, %edi
callq 0x21e0
movq %r15, 0x10(%rax)
movq %rax, %rdi
leaq -0x60(%rbp), %rsi
callq 0x2050
leaq -0x50(%rbp), %rax
incq (%rax)
incq %r15
addq $0x30, %rbx
cmpq %r15, %r13
jne 0x6aef
movq -0x48(%rbp), %r13
leaq -0x48(%rbp), %r15
cmpq %r15, %r13
sete %al
movq -0x60(%rbp), %r12
leaq -0x60(%rbp), %rcx
cmpq %rcx, %r12
sete %cl
orb %al, %cl
jne 0x6c7d
movq 0x10(%r13), %r15
decq -0x38(%rbp)
movq %r13, %rdi
callq 0x2280
movq %r13, %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 -0x68(%rbp), %rcx
movq (%rcx), %rcx
movsd %xmm0, (%rcx,%r15,8)
movq (%r14), %rcx
movq %rbx, (%rcx,%r15,8)
addsd 0x463d(%rip), %xmm0     # 0xb238
addsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbx,8)
movsd 0x4433(%rip), %xmm1     # 0xb040
ucomisd %xmm0, %xmm1
jbe 0x6c36
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 0x6c57
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
leaq -0x48(%rbp), %r15
incq (%rax)
movq -0x48(%rbp), %r13
movq -0x60(%rbp), %r12
leaq -0x60(%rbp), %rax
cmpq %rax, %r12
sete %al
cmpq %r15, %r13
je 0x6c7d
testb %al, %al
je 0x6ba0
cmpq %r15, %r13
je 0x6cbd
movabsq $0x3ff0000000000000, %r14 # imm = 0x3FF0000000000000
movq -0x68(%rbp), %r12
movq 0x10(%r13), %rbx
decq -0x38(%rbp)
movq %r13, %rdi
callq 0x2280
movq %r13, %rdi
callq 0x21c0
movq (%r12), %rax
movq %r14, (%rax,%rbx,8)
movq -0x48(%rbp), %r13
cmpq %r15, %r13
jne 0x6c90
movq -0x60(%rbp), %r12
leaq -0x60(%rbp), %r13
cmpq %r13, %r12
je 0x6cfd
movq -0x68(%rbp), %r14
movq 0x10(%r12), %rbx
decq -0x50(%rbp)
movq %r12, %rdi
callq 0x2280
movq %r12, %rdi
callq 0x21c0
movq (%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax,%rbx,8)
movq -0x60(%rbp), %r12
cmpq %r13, %r12
jne 0x6cca
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x6d0f
callq 0x21c0
movq -0x60(%rbp), %r12
cmpq %r13, %r12
je 0x6d28
movq (%r12), %rbx
movq %r12, %rdi
callq 0x21c0
movq %rbx, %r12
cmpq %r13, %rbx
jne 0x6d14
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x6d41
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x6d31
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x6d68
jmp 0x6d57
movq %rax, %r14
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x6d68
callq 0x21c0
movq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %r15
cmpq %r15, %rdi
je 0x6d85
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x6d75
movq -0x48(%rbp), %rdi
leaq -0x48(%rbp), %r15
cmpq %r15, %rdi
je 0x6da2
movq (%rdi), %rbx
callq 0x21c0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x6d92
movq %r14, %rdi
callq 0x22b0