// File: xukmin[P]rase/routing-metric-calculators.cc
// Function: DataAggregationCalculator::CalculateMetric(SensorNetwork const&)
double DataAggregationCalculator::CalculateMetric(const SensorNetwork& network) {
  // TODO: Make it an argument.
  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;
  }

  std::set<int> visited;
  visited.insert(0);

  int num_transmissions = 1;
  for (int i = 0; i < triggered.size(); i++) {
    visited.insert(triggered[i]);
    num_transmissions++;
    int parent = network.GetParent(triggered[i]);
    while (visited.find(parent) == visited.end()) {
      visited.insert(parent);
      num_transmissions++;
      parent = network.GetParent(parent);
      assert(parent >= 0);
    }
  }

  return num_transmissions;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xe530
leaq -0x40(%rbp), %rdi
movsd 0x5238(%rip), %xmm1     # 0x1f1a0
movaps %xmm1, %xmm0
callq 0xc1a0
jmp 0x19f72
movq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movsd 0x5226(%rip), %xmm0     # 0x1f1a8
leaq -0x30(%rbp), %rdx
callq 0xd060
movb %al, -0xd9(%rbp)
jmp 0x19f93
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x19fc4
jmp 0x19f9f
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x1a21e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1a235
leaq -0x80(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0xe2e0
movq -0xf8(%rbp), %rdi
movl $0x0, -0x84(%rbp)
leaq -0x84(%rbp), %rsi
callq 0xeb50
movb %dl, -0xe9(%rbp)
movq %rax, -0xe8(%rbp)
jmp 0x1a000
movb -0xe9(%rbp), %al
movq -0xe8(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movb %al, -0x90(%rbp)
movl $0x1, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movslq -0xa0(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x1a201
movslq -0xa0(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe570
movq %rax, %rsi
leaq -0x80(%rbp), %rdi
callq 0xe390
movb %dl, -0x109(%rbp)
movq %rax, -0x108(%rbp)
jmp 0x1a083
movb -0x109(%rbp), %al
movq -0x108(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
movl -0x9c(%rbp), %eax
incl %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
movslq -0xa0(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe570
movq -0x118(%rbp), %rdi
movl (%rax), %esi
callq 0xeb20
movl %eax, -0x110(%rbp)
jmp 0x1a0dc
movl -0x110(%rbp), %eax
movl %eax, -0xb4(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0x1afe0
movq %rax, -0x120(%rbp)
jmp 0x1a101
movq -0x120(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xe4d0
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x14e80
testb $0x1, %al
jne 0x1a13b
jmp 0x1a1eb
leaq -0x80(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
callq 0xe390
movb %dl, -0x129(%rbp)
movq %rax, -0x128(%rbp)
jmp 0x1a15a
movb -0x129(%rbp), %al
movq -0x128(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movb %al, -0xd0(%rbp)
movl -0x9c(%rbp), %eax
incl %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rdi
movl -0xb4(%rbp), %esi
callq 0xeb20
movl %eax, -0x130(%rbp)
jmp 0x1a199
movl -0x130(%rbp), %eax
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
jl 0x1a1b0
jmp 0x1a1e6
leaq 0x50bf(%rip), %rdi      # 0x1f276
leaq 0x5024(%rip), %rsi      # 0x1f1e2
movl $0xbe, %edx
leaq 0x5104(%rip), %rcx      # 0x1f2ce
callq 0x3140
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x62d0
jmp 0x1a235
jmp 0x1a0e8
jmp 0x1a1ed
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x1a02e
cvtsi2sdl -0x9c(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x62d0
leaq -0x30(%rbp), %rdi
callq 0xe670
movsd -0x8(%rbp), %xmm0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0xe670
movq -0x48(%rbp), %rdi
callq 0x33d0
nopw (%rax,%rax)