// File: xukmin[P]rase/routing-builders.cc
// Function: WeightedRandomizedParentSelector::SelectParent(int, std::vector<int, std::allocator<int>> const&, SensorNetwork const&)
int WeightedRandomizedParentSelector::SelectParent(
    int sensor,
    const std::vector<int>& candidates,
    const SensorNetwork& network) {
  assert(!candidates.empty());
  std::vector<double> weights(candidates.size());
  double total_weights = 0.0;
  for (int i = 0; i < candidates.size(); i++) {
    int num_neighbors = network.GetNeighbors(i).size();
    assert(num_neighbors > 0);
    weights[i] = 1.0 / num_neighbors;
    total_weights += weights[i];
  }

  double r = DoubleRand(0, total_weights);
  for (int i = 0; i < weights.size(); i++) {
    if (r < weights[i]) {
      return candidates[i];
    }
    r -= weights[i];
  }

  // Should never get here.
  assert(false);
  exit(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8610
xorb $-0x1, %al
testb $0x1, %al
jne 0x1063b
jmp 0x1063d
jmp 0x1065c
leaq 0x3f63(%rip), %rdi      # 0x145a7
leaq 0x3e39(%rip), %rsi      # 0x14484
movl $0x12b, %edx            # imm = 0x12B
leaq 0x430b(%rip), %rcx      # 0x14962
callq 0x3130
movq -0x18(%rbp), %rdi
callq 0x8530
movq %rax, -0x80(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x78(%rbp)
callq 0x9620
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x11090
jmp 0x10689
leaq -0x39(%rbp), %rdi
callq 0x4900
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8530
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jae 0x107a2
movq -0x20(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x8900
movq %rax, -0x90(%rbp)
jmp 0x106dd
movq -0x90(%rbp), %rdi
callq 0x11100
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jle 0x106f4
jmp 0x10747
leaq 0x42d8(%rip), %rdi      # 0x149d3
leaq 0x3d82(%rip), %rsi      # 0x14484
movl $0x130, %edx            # imm = 0x130
leaq 0x4254(%rip), %rcx      # 0x14962
callq 0x3130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x4900
jmp 0x10899
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4720
jmp 0x10899
cvtsi2sdl -0x60(%rbp), %xmm1
movsd 0x3cfc(%rip), %xmm0     # 0x14450
divsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movslq -0x5c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x8ba0
movsd -0x98(%rbp), %xmm0
movsd %xmm0, (%rax)
movslq -0x5c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x8ba0
movsd (%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x106a1
movsd -0x58(%rbp), %xmm1
xorps %xmm0, %xmm0
callq 0x133a0
movsd %xmm0, -0xa0(%rbp)
jmp 0x107b9
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movslq -0x6c(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x9a50
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1087a
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movslq -0x6c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x8ba0
movsd -0xb0(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1084d
movq -0x18(%rbp), %rdi
movslq -0x6c(%rbp), %rsi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4720
movl -0xb4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
movslq -0x6c(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x8ba0
movsd (%rax), %xmm1
movsd -0x68(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x107cd
leaq 0x4164(%rip), %rdi      # 0x149e5
leaq 0x3bfc(%rip), %rsi      # 0x14484
movl $0x13e, %edx            # imm = 0x13E
leaq 0x40ce(%rip), %rcx      # 0x14962
callq 0x3130
movq -0x48(%rbp), %rdi
callq 0x3340
nopw %cs:(%rax,%rax)