// File: 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 %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xe530
leaq -0x40(%rbp), %rdi
movsd 0x5698(%rip), %xmm1     # 0x1f1a0
movaps %xmm1, %xmm0
callq 0xc1a0
jmp 0x19b12
movq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movsd 0x5686(%rip), %xmm0     # 0x1f1a8
leaq -0x30(%rbp), %rdx
callq 0xd060
movb %al, -0xa9(%rbp)
jmp 0x19b33
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x19b64
jmp 0x19b3f
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x19e9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x19eb3
leaq -0x30(%rbp), %rdi
callq 0xe550
movq %rax, -0xc0(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0xf640
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x16fa0
jmp 0x19b9d
leaq -0x69(%rbp), %rdi
callq 0x5d30
movl $0x0, -0x70(%rbp)
movslq -0x70(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jae 0x19e01
movslq -0x70(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe570
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x18f50
movq %rax, -0xd0(%rbp)
jmp 0x19bfb
movq -0xd0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x14fe0
movsd %xmm0, -0xd8(%rbp)
jmp 0x19c15
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1b060
movsd %xmm0, -0xe0(%rbp)
jmp 0x19c42
movsd -0xe0(%rbp), %xmm1
movsd -0xe8(%rbp), %xmm0
movsd 0x5556(%rip), %xmm2     # 0x1f1b0
callq 0x19ed0
movsd %xmm0, -0xf0(%rbp)
jmp 0x19c69
movsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0xeb20
movl %eax, -0xf4(%rbp)
jmp 0x19c8d
movl -0xf4(%rbp), %eax
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
jl 0x19dc2
movq -0x18(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x18f50
movq %rax, -0x100(%rbp)
jmp 0x19cbb
movq -0x18(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0x18f50
movq %rax, -0x108(%rbp)
jmp 0x19cd3
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdi
callq 0x14fe0
movsd %xmm0, -0x110(%rbp)
jmp 0x19cf0
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x120(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1b060
movsd %xmm0, -0x118(%rbp)
jmp 0x19d23
movsd -0x118(%rbp), %xmm1
movsd -0x120(%rbp), %xmm0
movsd 0x5475(%rip), %xmm2     # 0x1f1b0
callq 0x19ed0
movsd %xmm0, -0x128(%rbp)
jmp 0x19d4a
movsd -0x128(%rbp), %xmm1
movsd -0x88(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0xeb20
movl %eax, -0x12c(%rbp)
jmp 0x19d7d
movl -0x12c(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x19c99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x5d30
jmp 0x19eb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5b50
jmp 0x19eb3
movsd 0x48c6(%rip), %xmm0     # 0x1e690
subsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x138(%rbp)
movslq -0x70(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0xebc0
movsd -0x138(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x19bad
leaq -0x68(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x1b0e0
movq -0x148(%rbp), %rdi
movq %rax, -0xa0(%rbp)
callq 0x1b110
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
xorps %xmm0, %xmm0
callq 0x1b080
movsd %xmm0, -0x140(%rbp)
jmp 0x19e4b
leaq -0x68(%rbp), %rdi
callq 0xfa70
movsd -0x140(%rbp), %xmm0
movq %rax, %xmm1
movaps 0x5318(%rip), %xmm2     # 0x1f180
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x531c(%rip), %xmm2     # 0x1f190
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5b50
leaq -0x30(%rbp), %rdi
callq 0xe670
movsd -0x8(%rbp), %xmm0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0xe670
movq -0x48(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)