// File: /workspace/github_cpp_makefile_star2_O3/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)
movaps 0x2c96(%rip), %xmm0     # 0xd1f0
leaq 0x50(%rsp), %rsi
movaps %xmm0, (%rsi)
movsd 0x2c96(%rip), %xmm0     # 0xd200
movq %rbx, %rdi
callq 0x63d4
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %rdi
testb %al, %al
je 0xa774
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
sarq $0x2, %rsi
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x95d4
movq 0x10(%rsp), %rdi
cmpq %rdi, 0x18(%rsp)
je 0xa6f3
movq (%rbx), %rax
xorl %r15d, %r15d
leaq 0x50(%rsp), %r14
movslq (%rdi,%r15,4), %rcx
leaq (%rcx,%rcx,8), %r12
leaq (%rax,%r12,8), %rsi
movq %r14, %rdi
callq 0x83c3
divsd 0x90(%rbx), %xmm0
movsd 0x2c2b(%rip), %xmm1     # 0xd208
callq 0x3350
movapd %xmm0, %xmm1
mulsd 0x2c22(%rip), %xmm1     # 0xd210
movsd 0x210a(%rip), %xmm0     # 0xc700
divsd %xmm1, %xmm0
ucomisd 0x1a16(%rip), %xmm0     # 0xc018
jb 0xa60a
sqrtsd %xmm0, %xmm0
jmp 0xa60f
callq 0x3150
callq 0x3340
movapd %xmm0, %xmm1
mulsd 0x2bf8(%rip), %xmm1     # 0xd218
addsd 0x20d8(%rip), %xmm1     # 0xc700
movq (%rbx), %rax
movl 0x44(%rax,%r12,8), %ecx
testl %ecx, %ecx
js 0xa6bf
movsd %xmm1, 0x8(%rsp)
leaq (%rax,%r12,8), %rdi
movl %ecx, %ecx
leaq (%rcx,%rcx,8), %r13
leaq (%rax,%r13,8), %rsi
callq 0x83c3
divsd 0x90(%rbx), %xmm0
movsd 0x2ba7(%rip), %xmm1     # 0xd208
callq 0x3350
movapd %xmm0, %xmm1
mulsd 0x2b9e(%rip), %xmm1     # 0xd210
movsd 0x2086(%rip), %xmm0     # 0xc700
divsd %xmm1, %xmm0
ucomisd 0x1992(%rip), %xmm0     # 0xc018
jb 0xa68e
sqrtsd %xmm0, %xmm0
jmp 0xa693
callq 0x3150
callq 0x3340
mulsd 0x2b78(%rip), %xmm0     # 0xd218
addsd 0x2058(%rip), %xmm0     # 0xc700
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq (%rbx), %rax
movl 0x44(%rax,%r13,8), %ecx
jmp 0xa630
movsd 0x2039(%rip), %xmm0     # 0xc700
subsd %xmm1, %xmm0
movq 0x28(%rsp), %rcx
movsd %xmm0, (%rcx,%r15,8)
incq %r15
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
subq %rdi, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
ja 0xa5b9
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0xa716
movq %rax, %rdx
addsd (%rdx), %xmm0
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0xa709
subq %rax, %rcx
sarq $0x3, %rcx
movq %rcx, %xmm1
punpckldq 0x2aa6(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x2aae(%rip), %xmm1     # 0xd1e0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
testq %rax, %rax
je 0xa770
movq 0x38(%rsp), %rsi
subq %rax, %rsi
movq %rax, %rdi
movsd %xmm0, 0x8(%rsp)
movapd %xmm2, 0x40(%rsp)
callq 0x31e0
movapd 0x40(%rsp), %xmm2
movsd 0x8(%rsp), %xmm0
movq 0x10(%rsp), %rdi
divsd %xmm2, %xmm0
testq %rdi, %rdi
je 0xa792
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 0xa7a2
movq %rax, %rbx
jmp 0xa7c3
jmp 0xa7a9
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xa7c3
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xa7da
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq %rbx, %rdi
callq 0x3360