// File: xukmin[P]rase/routing-builders.cc
// Function: SecondNearestFirstParentSelector::SelectParent(int, std::vector<int, std::allocator<int>> const&, SensorNetwork const&)
int SecondNearestFirstParentSelector::SelectParent(
    int sensor,
    const std::vector<int>& candidates,
    const SensorNetwork& network) {
  assert(!candidates.empty());
  if (candidates.size() == 1) {
    return candidates[0];
  }

  int min_sensor = candidates[0];
  double min_distance = network.GetDistance(sensor, min_sensor);
  int second_min_sensor = candidates[1];
  double second_min_distance = network.GetDistance(sensor, second_min_sensor);
  if (min_distance > second_min_sensor) {
    std::swap(min_sensor, second_min_sensor);
    std::swap(min_distance, second_min_distance);
  }
  for (int i = 2; i < candidates.size(); i++) {
    double distance = network.GetDistance(sensor, candidates[i]);
    if (distance < min_distance) {
      second_min_sensor = min_sensor;
      second_min_distance = min_distance;
      min_sensor = candidates[i];
      min_distance = distance;
    } else if (distance < second_min_distance) {
      second_min_sensor = candidates[i];
      second_min_distance = distance;
    }
  }

  return second_min_sensor;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x8610
xorb $-0x1, %al
testb $0x1, %al
jne 0x100e8
jmp 0x100ea
jmp 0x10109
leaq 0x44b6(%rip), %rdi      # 0x145a7
leaq 0x438c(%rip), %rsi      # 0x14484
movl $0xdf, %edx
leaq 0x4729(%rip), %rcx      # 0x1482d
callq 0x3130
movq -0x20(%rbp), %rdi
callq 0x8530
cmpq $0x1, %rax
jne 0x1012f
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x1027e
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x10f20
movsd %xmm0, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x10f20
movsd %xmm0, -0x48(%rbp)
movsd -0x38(%rbp), %xmm0
cvtsi2sdl -0x3c(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x101a6
leaq -0x2c(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x10fa0
leaq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x10fd0
movl $0x2, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x8530
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x10278
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x10e00
movq -0x70(%rbp), %rdi
movl -0x64(%rbp), %esi
movl (%rax), %edx
callq 0x10f20
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1023a
movl -0x2c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x10268
movsd -0x58(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x10266
movq -0x20(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x10e00
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x10268
jmp 0x1026a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x101ad
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)