// File: /workspace/github_cpp_makefile_star2_O2/xukmin[P]rase/routing-metric-calculators.cc
// Function: ChannelQualityCalculator::CalculateMetric(SensorNetwork const&)
double ChannelQualityCalculator::CalculateMetric(const SensorNetwork& network) {
  // Suppose the Bit Error Rate at the communication range is 1e-3, i.e
  // 0.5 * erfc(sqrt(1 / noise)) = 1e-3
  static const double noise = 0.209434;
  static const double sensing_range = 15.0;

  std::vector<int> triggered;
  Position position(50.0, 50.0);  // FIXME: pass in region to the calculator.
  if (!network.FindSensorsWithinRange(position, sensing_range, &triggered)) {
    // No sensor will ever be triggered by the event.
    return 0.0;
  }

  // Link Error Rates
  std::vector<double> lers(triggered.size());
  for (int i = 0; i < triggered.size(); i++) {
    int sensor = triggered[i];
    double distance = Distance(position, network.GetPosition(sensor));
    // Link Accuracy Rate
    double lar = BitAccuracyRate(distance, network.communication_range(), noise);
    int parent = network.GetParent(sensor);
    while (parent >= 0) {
      double distance = Distance(network.GetPosition(sensor),
                                 network.GetPosition(parent));
      lar *= BitAccuracyRate(distance, network.communication_range(), noise);
      parent = network.GetParent(parent);
    }
    lers[i] = 1.0 - lar;
  }

  return std::accumulate(lers.begin(), lers.end(), 0.0) / lers.size();
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x50, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
andq $0x0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movaps 0x2a99(%rip), %xmm0     # 0xc1b0
leaq 0x40(%rsp), %rsi
movaps %xmm0, (%rsi)
movsd 0x2a99(%rip), %xmm0     # 0xc1c0
movq %rbx, %rdi
callq 0x5c6e
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
testb %al, %al
je 0x986f
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x2, %rsi
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x8974
xorl %r15d, %r15d
leaq 0x40(%rsp), %r14
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
jbe 0x9813
movslq (%rax,%r15,4), %rax
imulq $0x48, %rax, %r12
movq (%rbx), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x7c0b
movsd 0x90(%rbx), %xmm1
callq 0x98b5
movapd %xmm0, %xmm1
movq (%rbx), %rsi
leaq (%rsi,%r12), %rax
addq $0x44, %rax
movslq (%rax), %rax
testq %rax, %rax
js 0x97f4
movsd %xmm1, 0x8(%rsp)
leaq (%rsi,%r12), %rdi
imulq $0x48, %rax, %r13
addq %r13, %rsi
callq 0x7c0b
movsd 0x90(%rbx), %xmm1
callq 0x98b5
movq (%rbx), %rsi
leaq (%rsi,%r13), %rax
addq $0x44, %rax
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
jmp 0x97b2
movsd 0x1ec4(%rip), %xmm0     # 0xb6c0
subsd %xmm1, %xmm0
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
jmp 0x9766
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
xorpd %xmm2, %xmm2
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x9833
addsd (%rdx), %xmm2
addq $0x8, %rdx
jmp 0x9824
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, %xmm0
punpckldq 0x2949(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2951(%rip), %xmm0     # 0xc1a0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x4636
leaq 0x10(%rsp), %rdi
callq 0x7156
movsd 0x8(%rsp), %xmm0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x988f
movq %rax, %rbx
jmp 0x98a3
jmp 0x9896
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4636
leaq 0x10(%rsp), %rdi
callq 0x7156
movq %rbx, %rdi
callq 0x3360