// File: /workspace/github_cpp_makefile_star2_O1/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 $0x60, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movabsq $0x4049000000000000, %rax # imm = 0x4049000000000000
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
movq %rax, 0x8(%rsi)
movsd 0x2c4b(%rip), %xmm0     # 0xd190
movq %rbx, %rdi
callq 0x641c
xorpd %xmm0, %xmm0
testb %al, %al
je 0xa74b
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x2, %rsi
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x9598
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0xa6d2
xorl %r15d, %r15d
leaq 0x50(%rsp), %r14
movslq (%rax,%r15,4), %rax
leaq (%rax,%rax,8), %r12
leaq (,%r12,8), %rsi
addq (%rbx), %rsi
movq %r14, %rdi
callq 0x8313
divsd 0x90(%rbx), %xmm0
movsd 0x2bdf(%rip), %xmm1     # 0xd198
callq 0x3350
movapd %xmm0, %xmm1
mulsd 0x2bd6(%rip), %xmm1     # 0xd1a0
movsd 0x20ce(%rip), %xmm0     # 0xc6a0
divsd %xmm1, %xmm0
ucomisd 0x1a3a(%rip), %xmm0     # 0xc018
jb 0xa5e6
sqrtsd %xmm0, %xmm0
jmp 0xa5eb
callq 0x3150
callq 0x3340
movapd %xmm0, %xmm1
mulsd 0x2bac(%rip), %xmm1     # 0xd1a8
addsd 0x209c(%rip), %xmm1     # 0xc6a0
movq (%rbx), %rax
movl 0x44(%rax,%r12,8), %eax
testl %eax, %eax
js 0xa69e
movsd %xmm1, 0x8(%rsp)
movq (%rbx), %rcx
leaq (%rcx,%r12,8), %rdi
movl %eax, %eax
leaq (%rax,%rax,8), %r13
leaq (%rcx,%r13,8), %rsi
callq 0x8313
divsd 0x90(%rbx), %xmm0
movsd 0x2b58(%rip), %xmm1     # 0xd198
callq 0x3350
movapd %xmm0, %xmm1
mulsd 0x2b4f(%rip), %xmm1     # 0xd1a0
movsd 0x2047(%rip), %xmm0     # 0xc6a0
divsd %xmm1, %xmm0
ucomisd 0x19b3(%rip), %xmm0     # 0xc018
jb 0xa66d
sqrtsd %xmm0, %xmm0
jmp 0xa672
callq 0x3150
callq 0x3340
mulsd 0x2b29(%rip), %xmm0     # 0xd1a8
addsd 0x2019(%rip), %xmm0     # 0xc6a0
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq (%rbx), %rax
movl 0x44(%rax,%r13,8), %eax
jmp 0xa60c
movsd 0x1ffa(%rip), %xmm0     # 0xc6a0
subsd %xmm1, %xmm0
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
ja 0xa58e
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rax
xorpd %xmm0, %xmm0
cmpq %rax, %rdi
je 0xa6f5
movq %rdi, %rcx
addsd (%rcx), %xmm0
addq $0x8, %rcx
cmpq %rax, %rcx
jne 0xa6e8
subq %rdi, %rax
sarq $0x3, %rax
movq %rax, %xmm1
punpckldq 0x2a67(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x2a6f(%rip), %xmm1     # 0xd180
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
testq %rdi, %rdi
je 0xa747
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
movapd %xmm2, 0x40(%rsp)
callq 0x31e0
movapd 0x40(%rsp), %xmm2
movsd 0x8(%rsp), %xmm0
divsd %xmm2, %xmm0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa76e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x31e0
movsd 0x8(%rsp), %xmm0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0xa77e
movq %rax, %rbx
jmp 0xa79f
jmp 0xa785
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xa79f
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa7b6
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq %rbx, %rdi
callq 0x3360