// File: xukmin[P]rase/routing-metric-calculators.cc
// Function: LatencyCalculator::CalculateMetric(SensorNetwork const&)
double LatencyCalculator::CalculateMetric(const SensorNetwork& network) {
  // TODO: Make sensing_range and position arguments.
  const double sensing_range = 15.0;
  const Position position(50.0, 50.0);

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

  std::vector<bool> active(network.num_sensors());
  for (int i = 0; i < triggered.size(); i++) {
    int parent = triggered[i];
    while (parent >= 0 && !active[parent]) {
      active[parent] = true;
      parent = network.GetParent(parent);
    }
  }

  std::vector<int> children(network.num_sensors());
  // Skip the base station which does not have a parent.
  for (int i = 1; i < network.num_sensors(); i++) {
    assert(network.GetParent(i) >= 0);
    if (active[i]) {
      children[network.GetParent(i)]++;
    }
  }

  std::vector<int> timestamps(network.num_sensors());
  std::vector<bool> visited(network.num_sensors());
  int num_to_visit = network.num_sensors();
  // Ignore the inactive sensors by marking them as visited.
  for (int i = 0; i < network.num_sensors(); i++) {
    visited[i] = !active[i];
    num_to_visit -= visited[i];
  }
  while (num_to_visit > 0) {
    std::vector<int> leaves;
    for (int i = 0; i < network.num_sensors(); i++) {
      if (!visited[i] && children[i] == 0) {
        int parent = network.GetParent(i);
        if (parent >= 0) {
          assert(active[parent]);
          timestamps[parent] = std::max(timestamps[parent], timestamps[i]) + 1;
        }
        leaves.push_back(i);
        visited[i] = true;
        num_to_visit--;
      }
    }
    for (int i = 0; i < leaves.size(); i++) {
      int parent = network.GetParent(leaves[i]);
      if (parent >= 0) {
        children[parent]--;
      }
    }
  }

  return timestamps[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movabsq $0x402e000000000000, %rax # imm = 0x402E000000000000
movq %rax, -0x20(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
movsd 0x4f1c(%rip), %xmm1     # 0x1f1a0
movaps %xmm1, %xmm0
callq 0xc1a0
leaq -0x48(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0xe530
movq -0x1e0(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
movq -0x18(%rbp), %rdi
movsd 0x4ef2(%rip), %xmm0     # 0x1f1a8
callq 0xd060
movb %al, -0x1c9(%rbp)
jmp 0x1a2c3
movb -0x1c9(%rbp), %al
testb $0x1, %al
jne 0x1a2f4
jmp 0x1a2cf
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1acba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1acd1
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x1e4(%rbp)
jmp 0x1a305
movl -0x1e4(%rbp), %eax
cltq
movq %rax, -0x1f8(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0xe7e0
movq -0x1f8(%rbp), %rsi
movq -0x1f0(%rbp), %rdx
leaq -0x80(%rbp), %rdi
callq 0xe800
jmp 0x1a340
leaq -0x81(%rbp), %rdi
callq 0xe840
movl $0x0, -0x88(%rbp)
movslq -0x88(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jae 0x1a4c1
movslq -0x88(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xe570
movl (%rax), %eax
movl %eax, -0x8c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x8c(%rbp)
movb %al, -0x201(%rbp)
jl 0x1a3f9
movslq -0x8c(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xe980
movq %rdx, -0x218(%rbp)
movq %rax, -0x210(%rbp)
jmp 0x1a3c9
movq -0x218(%rbp), %rax
movq -0x210(%rbp), %rcx
movq %rcx, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xea00
xorb $-0x1, %al
movb %al, -0x201(%rbp)
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x1a408
jmp 0x1a4ab
movslq -0x8c(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xe980
movq %rdx, -0x228(%rbp)
movq %rax, -0x220(%rbp)
jmp 0x1a428
movq -0x228(%rbp), %rax
movq -0x220(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
callq 0xea60
movq -0x18(%rbp), %rdi
movl -0x8c(%rbp), %esi
callq 0xeb20
movl %eax, -0x22c(%rbp)
jmp 0x1a46c
movl -0x22c(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x1a398
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x81(%rbp), %rdi
callq 0xe840
jmp 0x1acd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1acaf
jmp 0x1a4ad
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1a356
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x230(%rbp)
jmp 0x1a4d2
movl -0x230(%rbp), %eax
cltq
movq %rax, -0x240(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0xedb0
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x1acf0
jmp 0x1a510
leaq -0xc9(%rbp), %rdi
callq 0x10800
movl $0x1, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x248(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x244(%rbp)
jmp 0x1a543
movl -0x248(%rbp), %eax
movl -0x244(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1a65f
movq -0x18(%rbp), %rdi
movl -0xd0(%rbp), %esi
callq 0xeb20
movl %eax, -0x24c(%rbp)
jmp 0x1a56e
movl -0x24c(%rbp), %eax
cmpl $0x0, %eax
jl 0x1a57b
jmp 0x1a5c8
leaq 0x4d9d(%rip), %rdi      # 0x1f31f
leaq 0x4c59(%rip), %rsi      # 0x1f1e2
movl $0xdc, %edx
leaq 0x4da4(%rip), %rcx      # 0x1f339
callq 0x3140
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc9(%rbp), %rdi
callq 0x10800
jmp 0x1acaf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1aca3
movslq -0xd0(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xe980
movq %rdx, -0x260(%rbp)
movq %rax, -0x258(%rbp)
jmp 0x1a5e8
movq -0x260(%rbp), %rax
movq -0x258(%rbp), %rcx
movq %rcx, -0xe0(%rbp)
movq %rax, -0xd8(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xea00
testb $0x1, %al
jne 0x1a616
jmp 0x1a649
movq -0x18(%rbp), %rdi
movl -0xd0(%rbp), %esi
callq 0xeb20
movl %eax, -0x264(%rbp)
jmp 0x1a62d
movl -0x264(%rbp), %eax
movslq %eax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0xe570
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1a64b
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x1a526
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x268(%rbp)
jmp 0x1a670
movl -0x268(%rbp), %eax
cltq
movq %rax, -0x278(%rbp)
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0xedb0
movq -0x278(%rbp), %rsi
movq -0x270(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x1acf0
jmp 0x1a6ae
leaq -0xf9(%rbp), %rdi
callq 0x10800
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x27c(%rbp)
jmp 0x1a6cb
movl -0x27c(%rbp), %eax
cltq
movq %rax, -0x290(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0xe7e0
movq -0x290(%rbp), %rsi
movq -0x288(%rbp), %rdx
leaq -0x128(%rbp), %rdi
callq 0xe800
jmp 0x1a709
leaq -0x129(%rbp), %rdi
callq 0xe840
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x294(%rbp)
jmp 0x1a726
movl -0x294(%rbp), %eax
movl %eax, -0x130(%rbp)
movl $0x0, -0x134(%rbp)
movl -0x134(%rbp), %eax
movl %eax, -0x29c(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x298(%rbp)
jmp 0x1a759
movl -0x29c(%rbp), %eax
movl -0x298(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1a8e2
movslq -0x134(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xe980
movq %rdx, -0x2b0(%rbp)
movq %rax, -0x2a8(%rbp)
jmp 0x1a78d
movq -0x2b0(%rbp), %rax
movq -0x2a8(%rbp), %rcx
movq %rcx, -0x148(%rbp)
movq %rax, -0x140(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xea00
xorb $0x1, %al
movb %al, -0x2c1(%rbp)
movslq -0x134(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0xe980
movq %rdx, -0x2c0(%rbp)
movq %rax, -0x2b8(%rbp)
jmp 0x1a7e0
movb -0x2c1(%rbp), %al
movq -0x2c0(%rbp), %rcx
movq -0x2b8(%rbp), %rdx
movq %rdx, -0x158(%rbp)
movq %rcx, -0x150(%rbp)
movzbl %al, %esi
andl $0x1, %esi
leaq -0x158(%rbp), %rdi
callq 0xea60
movslq -0x134(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0xe980
movq %rdx, -0x2d8(%rbp)
movq %rax, -0x2d0(%rbp)
jmp 0x1a837
movq -0x2d8(%rbp), %rax
movq -0x2d0(%rbp), %rcx
movq %rcx, -0x168(%rbp)
movq %rax, -0x160(%rbp)
leaq -0x168(%rbp), %rdi
callq 0xea00
andb $0x1, %al
movzbl %al, %ecx
movl -0x130(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x130(%rbp)
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x1a73c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xf9(%rbp), %rdi
callq 0x10800
jmp 0x1aca3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1ac97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x129(%rbp), %rdi
callq 0xe840
jmp 0x1ac97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1ac8b
jmp 0x1a8e4
cmpl $0x0, -0x130(%rbp)
jle 0x1ac3c
leaq -0x180(%rbp), %rdi
callq 0xe530
movl $0x0, -0x184(%rbp)
movl -0x184(%rbp), %eax
movl %eax, -0x2e0(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x2dc(%rbp)
jmp 0x1a924
movl -0x2e0(%rbp), %eax
movl -0x2dc(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1ab7f
movslq -0x184(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0xe980
movq %rdx, -0x2f0(%rbp)
movq %rax, -0x2e8(%rbp)
jmp 0x1a95b
movq -0x2f0(%rbp), %rax
movq -0x2e8(%rbp), %rcx
movq %rcx, -0x198(%rbp)
movq %rax, -0x190(%rbp)
leaq -0x198(%rbp), %rdi
callq 0xea00
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2f1(%rbp)
jne 0x1a9b1
movslq -0x184(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0xe570
cmpl $0x0, (%rax)
sete %al
movb %al, -0x2f1(%rbp)
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0x1a9c0
jmp 0x1ab69
movq -0x18(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0xeb20
movl %eax, -0x2f8(%rbp)
jmp 0x1a9d7
movl -0x2f8(%rbp), %eax
movl %eax, -0x19c(%rbp)
cmpl $0x0, -0x19c(%rbp)
jl 0x1aaf5
movslq -0x19c(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xe980
movq %rdx, -0x308(%rbp)
movq %rax, -0x300(%rbp)
jmp 0x1aa10
movq -0x308(%rbp), %rax
movq -0x300(%rbp), %rcx
movq %rcx, -0x1b0(%rbp)
movq %rax, -0x1a8(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0xea00
testb $0x1, %al
jne 0x1aa3e
jmp 0x1aa40
jmp 0x1aa7c
leaq 0x493b(%rip), %rdi      # 0x1f382
leaq 0x4794(%rip), %rsi      # 0x1f1e2
movl $0xf0, %edx
leaq 0x48df(%rip), %rcx      # 0x1f339
callq 0x3140
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x180(%rbp), %rdi
callq 0xe670
jmp 0x1ac8b
movslq -0x19c(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x320(%rbp)
callq 0xe570
movq -0x320(%rbp), %rdi
movq %rax, -0x318(%rbp)
movslq -0x184(%rbp), %rsi
callq 0xe570
movq -0x318(%rbp), %rdi
movq %rax, %rsi
callq 0x1b140
movq %rax, -0x310(%rbp)
jmp 0x1aac8
movq -0x310(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x324(%rbp)
movslq -0x19c(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0xe570
movl -0x324(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x180(%rbp), %rdi
leaq -0x184(%rbp), %rsi
callq 0xe5c0
jmp 0x1ab0a
movslq -0x184(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0xe980
movq %rdx, -0x338(%rbp)
movq %rax, -0x330(%rbp)
jmp 0x1ab2d
movq -0x338(%rbp), %rax
movq -0x330(%rbp), %rcx
movq %rcx, -0x1c0(%rbp)
movq %rax, -0x1b8(%rbp)
leaq -0x1c0(%rbp), %rdi
movl $0x1, %esi
callq 0xea60
movl -0x130(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x1ab6b
movl -0x184(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x184(%rbp)
jmp 0x1a907
movl $0x0, -0x1c4(%rbp)
movslq -0x1c4(%rbp), %rax
movq %rax, -0x340(%rbp)
leaq -0x180(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x340(%rbp), %rax
cmpq %rcx, %rax
jae 0x1ac2b
movq -0x18(%rbp), %rax
movq %rax, -0x350(%rbp)
movslq -0x1c4(%rbp), %rsi
leaq -0x180(%rbp), %rdi
callq 0xe570
movq -0x350(%rbp), %rdi
movl (%rax), %esi
callq 0xeb20
movl %eax, -0x344(%rbp)
jmp 0x1abe6
movl -0x344(%rbp), %eax
movl %eax, -0x1c8(%rbp)
cmpl $0x0, -0x1c8(%rbp)
jl 0x1ac15
movslq -0x1c8(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0xe570
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x1ac17
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
jmp 0x1ab89
leaq -0x180(%rbp), %rdi
callq 0xe670
jmp 0x1a8e4
leaq -0xf8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xe570
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0x58(%rbp)
leaq -0x128(%rbp), %rdi
callq 0xeb00
leaq -0xf8(%rbp), %rdi
callq 0xe670
leaq -0xc8(%rbp), %rdi
callq 0xe670
leaq -0x80(%rbp), %rdi
callq 0xeb00
jmp 0x1acba
leaq -0x128(%rbp), %rdi
callq 0xeb00
leaq -0xf8(%rbp), %rdi
callq 0xe670
leaq -0xc8(%rbp), %rdi
callq 0xe670
leaq -0x80(%rbp), %rdi
callq 0xeb00
jmp 0x1acd1
leaq -0x48(%rbp), %rdi
callq 0xe670
movsd -0x8(%rbp), %xmm0
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0xe670
movq -0x50(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)
nopl (%rax)