// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
movaps 0x27ff(%rip), %xmm0     # 0xd1f0
leaq 0xd0(%rsp), %rsi
movaps %xmm0, (%rsi)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movsd 0x27e9(%rip), %xmm0     # 0xd200
movq %rbx, %rdi
callq 0x63d4
xorps %xmm0, %xmm0
testb %al, %al
je 0xae64
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
leaq 0xa8(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rcx
callq 0x7c26
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rdx
subq %rax, %rdx
je 0xaabc
sarq $0x2, %rdx
movq 0x48(%rsp), %rsi
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %edi, %edi
movl (%rax,%rdi,4), %ecx
testl %ecx, %ecx
js 0xaab4
movq (%rbx), %r8
movl $0x1, %r9d
shlq %cl, %r9
movl %ecx, %r10d
shrl $0x6, %r10d
movq (%rsi,%r10,8), %r11
btq %rcx, %r11
jb 0xaab4
orq %r9, %r11
movq %r11, (%rsi,%r10,8)
movl %ecx, %ecx
leaq (%rcx,%rcx,8), %rcx
movl 0x44(%r8,%rcx,8), %ecx
testl %ecx, %ecx
jns 0xaa84
incq %rdi
cmpq %rdx, %rdi
jne 0xaa7a
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
leaq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xafa8
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rdx, %rcx
cmpl $0x2, %ecx
jl 0xab4e
movq 0x48(%rsp), %rdx
movq 0x30(%rsp), %rsi
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x8c, %rax
movl $0x1, %r8d
movslq (%rax), %r9
testq %r9, %r9
js 0xaebb
movl %r8d, %r10d
shrl $0x6, %r10d
movq (%rdx,%r10,8), %r10
btq %r8, %r10
jae 0xab42
incl (%rsi,%r9,4)
incq %r8
addq $0x48, %rax
cmpq %r8, %rdi
jne 0xab21
movslq %ecx, %rsi
leaq 0x90(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xafa8
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
leaq 0x10(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0xa8(%rsp), %rdi
leaq 0xc(%rsp), %rcx
callq 0x7c26
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
testl %eax, %eax
jle 0xadd5
movq 0x48(%rsp), %rdx
xorl %ecx, %ecx
movq 0xa8(%rsp), %rsi
movl %eax, %ebp
movl $0x1, %edi
shlq %cl, %rdi
movl %ecx, %r8d
shrl $0x6, %r8d
movq (%rdx,%r8,8), %r9
btq %rcx, %r9
jae 0xabe3
movq %rdi, %r9
notq %r9
andq (%rsi,%r8,8), %r9
jmp 0xabea
movq (%rsi,%r8,8), %r9
orq %rdi, %r9
movq %r9, (%rsi,%r8,8)
andq %r9, %rdi
cmpq $0x1, %rdi
adcl $-0x1, %ebp
incl %ecx
cmpl %ecx, %eax
jne 0xabbe
testl %ebp, %ebp
jle 0xadd5
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
leaq 0x3f(%r12), %r13
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
movq %rax, %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
imull $0x38e38e39, %edx, %edx # imm = 0x38E38E39
testl %edx, %edx
jle 0xadcd
xorl %edi, %edi
movq 0xa8(%rsp), %rdx
movslq %edi, %rsi
leal 0x3f(%rsi), %r8d
testl %esi, %esi
cmovnsl %esi, %r8d
sarl $0x6, %r8d
movslq %r8d, %r8
leaq (%rdx,%r8,8), %r8
movq %rsi, %r9
andq %r13, %r9
xorl %r10d, %r10d
cmpq %r12, %r9
setbe %r10b
movq -0x8(%r8,%r10,8), %r8
btq %rsi, %r8
jb 0xad5b
movq 0x30(%rsp), %r8
cmpl $0x0, (%r8,%rsi,4)
jne 0xad5b
leaq (%rsi,%rsi,8), %rdi
movslq 0x44(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
js 0xaceb
movq 0x48(%rsp), %rdi
movl %ecx, %r8d
shrl $0x6, %r8d
movq (%rdi,%r8,8), %rdi
btq %rcx, %rdi
jae 0xae9c
movq 0x90(%rsp), %rdi
movl (%rdi,%rcx,4), %r8d
movl (%rdi,%rsi,4), %esi
cmpl %esi, %r8d
cmovgl %r8d, %esi
incl %esi
movl %esi, (%rdi,%rcx,4)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xad08
movl 0xc(%rsp), %edi
movl %edi, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xad23
movq %r14, %rdi
movq %r15, %rdx
callq 0x7abc
movl 0xc(%rsp), %edi
movq 0xa8(%rsp), %rdx
movq 0x8(%rbx), %rax
movslq %edi, %rcx
leal 0x3f(%rcx), %esi
testl %ecx, %ecx
cmovnsl %ecx, %esi
sarl $0x6, %esi
movslq %esi, %rsi
movq %rcx, %r8
andq %r13, %r8
xorl %r9d, %r9d
cmpq %r12, %r8
setbe %r9b
movl $0x1, %r8d
shlq %cl, %r8
leaq (%rdx,%rsi,8), %rcx
orq %r8, -0x8(%rcx,%r9,8)
decl %ebp
movq (%rbx), %rcx
incl %edi
movl %edi, 0xc(%rsp)
movq %rax, %rsi
subq %rcx, %rsi
shrq $0x3, %rsi
imull $0x38e38e39, %esi, %esi # imm = 0x38E38E39
cmpl %esi, %edi
jl 0xac61
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
je 0xadbb
sarq $0x2, %rax
movq 0x30(%rsp), %rdx
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %esi, %esi
movslq (%rdi,%rsi,4), %r8
leaq (%r8,%r8,8), %r8
movslq 0x44(%rcx,%r8,8), %r8
testq %r8, %r8
js 0xadb1
decl (%rdx,%r8,4)
incq %rsi
cmpq %rsi, %rax
jne 0xad9b
jmp 0xadc0
testq %rdi, %rdi
je 0xadcd
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
testl %ebp, %ebp
jg 0xac1f
movq 0x90(%rsp), %rdi
xorps %xmm0, %xmm0
cvtsi2sdl (%rdi), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0xae17
movq 0xc8(%rsp), %rsi
subq %rax, %rsi
movq %rax, %rdi
callq 0x31e0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xae27
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xae3e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xae5e
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq $0x0, 0x48(%rsp)
movsd 0x28(%rsp), %xmm0
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0xae8a
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
movsd %xmm0, 0x28(%rsp)
callq 0x31e0
movsd 0x28(%rsp), %xmm0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2537(%rip), %rdi      # 0xd3da
leaq 0x2390(%rip), %rsi      # 0xd23a
leaq 0x24e0(%rip), %rcx      # 0xd391
movl $0xf0, %edx
callq 0x3100
leaq 0x24b5(%rip), %rdi      # 0xd377
leaq 0x2371(%rip), %rsi      # 0xd23a
leaq 0x24c1(%rip), %rcx      # 0xd391
movl $0xdc, %edx
callq 0x3100
movq %rax, %rbx
jmp 0xaf2a
movq %rax, %rbx
jmp 0xaf47
movq %rax, %rbx
jmp 0xaf5e
jmp 0xaeeb
movq %rax, %rbx
jmp 0xaf86
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xaf0d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0xaf2a
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0xaf47
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xaf5e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xaf86
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq $0x0, 0x48(%rsp)
movl $0x0, 0x50(%rsp)
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0xafa0
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq %rbx, %rdi
callq 0x3360