// File: /workspace/github_cpp_makefile_star2_O1/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
movabsq $0x4049000000000000, %rax # imm = 0x4049000000000000
leaq 0xd8(%rsp), %rsi
movq %rax, (%rsi)
movq %rax, 0x8(%rsi)
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movsd 0x2785(%rip), %xmm0     # 0xd190
movq %rbx, %rdi
callq 0x641c
xorpd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
testb %al, %al
je 0xae7b
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
leaq 0x60(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
leaq 0x88(%rsp), %rcx
callq 0x7b7c
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rdx
subq %rax, %rdx
je 0xaabd
sarq $0x2, %rdx
movq 0x30(%rsp), %rsi
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %edi, %edi
movl (%rax,%rdi,4), %ecx
testl %ecx, %ecx
js 0xaab5
movq (%rbx), %r8
movl $0x1, %r9d
shlq %cl, %r9
movl %ecx, %r10d
shrl $0x6, %r10d
movq (%rsi,%r10,8), %r11
btq %rcx, %r11
jb 0xaab5
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 0xaa85
incq %rdi
cmpq %rdx, %rdi
jne 0xaa7b
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
leaq 0x88(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0xafd0
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x71c71c71c71c71c7, %r14 # imm = 0x8E38E38E38E38E39
imulq %r14, %rax
cmpl $0x2, %eax
jl 0xab53
movq 0x30(%rsp), %rdx
movq 0x88(%rsp), %rsi
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x8c, %rcx
movl $0x1, %r8d
movslq (%rcx), %r9
testq %r9, %r9
js 0xaecf
movq %r8, %r10
shrq $0x6, %r10
movq (%rdx,%r10,8), %r10
btq %r8, %r10
jae 0xab47
incl (%rsi,%r9,4)
incq %r8
addq $0x48, %rcx
cmpq %r8, %rdi
jne 0xab26
movslq %eax, %rsi
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0xafd0
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movslq %eax, %rsi
movq %rsp, %rdx
movb $0x0, (%rdx)
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rcx
callq 0x7b7c
movq 0x8(%rbx), %r12
subq (%rbx), %r12
sarq $0x3, %r12
imulq %r14, %r12
testl %r12d, %r12d
jle 0xabfd
movq 0x30(%rsp), %rax
movq 0x60(%rsp), %rdx
movl %r12d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movl $0x1, %edi
shlq %cl, %rdi
movq %rcx, %r8
shrq $0x6, %r8
movq (%rax,%r8,8), %r9
btq %rcx, %r9
jae 0xabdf
movq %rdi, %r9
notq %r9
andq (%rdx,%r8,8), %r9
jmp 0xabe6
movq (%rdx,%r8,8), %r9
orq %rdi, %r9
movq %r9, (%rdx,%r8,8)
andq %r9, %rdi
cmpq $0x1, %rdi
adcl $-0x1, %r12d
incq %rcx
cmpq %rcx, %rsi
jne 0xabba
testl %r12d, %r12d
jle 0xadc6
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
leaq 0x3f(%r13), %rbp
movq %rsp, %r14
leaq 0x20(%rsp), %r15
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
imull $0x38e38e39, %ecx, %ecx # imm = 0x38E38E39
testl %ecx, %ecx
jle 0xad69
xorl %ecx, %ecx
movslq %ecx, %rcx
leaq 0x3f(%rcx), %rdx
testq %rcx, %rcx
cmovnsq %rcx, %rdx
sarq $0x6, %rdx
shlq $0x3, %rdx
addq 0x60(%rsp), %rdx
movq %rcx, %rsi
andq %rbp, %rsi
xorl %edi, %edi
cmpq %r13, %rsi
setbe %dil
movq -0x8(%rdx,%rdi,8), %rdx
btq %rcx, %rdx
jb 0xad43
movq 0x88(%rsp), %rdx
cmpl $0x0, (%rdx,%rcx,4)
jne 0xad43
leaq (%rcx,%rcx,8), %rdx
movslq 0x44(%rax,%rdx,8), %rax
testq %rax, %rax
js 0xacdf
movq 0x30(%rsp), %rdx
movq %rax, %rsi
shrq $0x6, %rsi
movq (%rdx,%rsi,8), %rdx
btq %rax, %rdx
jae 0xaeb0
movq 0xc0(%rsp), %rdx
movl (%rdx,%rax,4), %esi
movl (%rdx,%rcx,4), %ecx
cmpl %ecx, %esi
cmovgl %esi, %ecx
incl %ecx
movl %ecx, (%rdx,%rax,4)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xacfc
movl 0x20(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xad07
movq %r14, %rdi
movq %r15, %rdx
callq 0x7a12
movslq 0x20(%rsp), %rcx
leaq 0x3f(%rcx), %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq 0x60(%rsp), %rax
movq %rcx, %rdx
andq %rbp, %rdx
xorl %esi, %esi
cmpq %r13, %rdx
setbe %sil
movl $0x1, %edx
shlq %cl, %rdx
orq %rdx, -0x8(%rax,%rsi,8)
decl %r12d
movl 0x20(%rsp), %ecx
incl %ecx
movl %ecx, 0x20(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
shrq $0x3, %rdx
imull $0x38e38e39, %edx, %edx # imm = 0x38E38E39
cmpl %edx, %ecx
jl 0xac54
movq (%rsp), %rdi
movq 0x8(%rsp), %rcx
subq %rdi, %rcx
je 0xadab
sarq $0x2, %rcx
movq 0x88(%rsp), %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %esi, %esi
movslq (%rdi,%rsi,4), %r8
leaq (%r8,%r8,8), %r8
movslq 0x44(%rax,%r8,8), %r8
testq %r8, %r8
js 0xada3
decl (%rdx,%r8,4)
incq %rsi
cmpq %rsi, %rcx
jne 0xad8d
testq %rdi, %rdi
je 0xadbd
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
testl %r12d, %r12d
jg 0xac1c
movq 0xc0(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xae11
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
movl %eax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0xae2e
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0xae4b
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xae7b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xae98
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movsd 0x58(%rsp), %xmm0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24b3(%rip), %rdi      # 0xd36a
leaq 0x230c(%rip), %rsi      # 0xd1ca
leaq 0x245c(%rip), %rcx      # 0xd321
movl $0xf0, %edx
callq 0x3100
leaq 0x2431(%rip), %rdi      # 0xd307
leaq 0x22ed(%rip), %rsi      # 0xd1ca
leaq 0x243d(%rip), %rcx      # 0xd321
movl $0xdc, %edx
callq 0x3100
movq %rax, %rbx
jmp 0xaf40
movq %rax, %rbx
jmp 0xaf5d
movq %rax, %rbx
jmp 0xaf7a
jmp 0xaeff
movq %rax, %rbx
jmp 0xafaa
movq %rdx, %rbx
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xaf23
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x20(%rsp), %r15
movq %r14, %rdx
movl %ebx, %ecx
movq %r15, %r8
callq 0x3532
movq (%r15), %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0xaf5d
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0xaf7a
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xafaa
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xafc7
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x31e0
movq %rbx, %rdi
callq 0x3360
nop