// File: xukmin[P]rase/routing-metric-calculators.cc
// Function: NodeDegreeVarianceCalculator::CalculateMetric(SensorNetwork const&)
double NodeDegreeVarianceCalculator::CalculateMetric(
    const SensorNetwork& network) {
  assert(network.num_sensors() > 0);
  assert(network.IsConnectedWithRoutings());

  std::vector<int> degrees(network.num_sensors());
  int sum = 0;
  // Base station (sensor #0) does not have a parent, so we start at sensor #1.
  for (int i = 1; i < degrees.size(); i++) {
    int parent = network.GetParent(i);
    assert(parent >= 0);
    degrees[parent]++;
    sum++;
  }

  double average = sum / degrees.size();
  double variance = 0.0;
  for (int i = 0; i < degrees.size(); i++) {
    int difference = (degrees[i] - average);
    variance += difference * difference;
  }
  variance /= degrees.size();

  return variance;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xe7c0
cmpl $0x0, %eax
jle 0x19103
jmp 0x19122
leaq 0x60be(%rip), %rdi      # 0x1f1c8
leaq 0x60d1(%rip), %rsi      # 0x1f1e2
movl $0x23, %edx
leaq 0x60e3(%rip), %rcx      # 0x1f200
callq 0x3140
movq -0x10(%rbp), %rdi
callq 0xdaa0
testb $0x1, %al
jne 0x19131
jmp 0x19133
jmp 0x19152
leaq 0x611a(%rip), %rdi      # 0x1f254
leaq 0x60a1(%rip), %rsi      # 0x1f1e2
movl $0x24, %edx
leaq 0x60b3(%rip), %rcx      # 0x1f200
callq 0x3140
movq -0x10(%rbp), %rdi
callq 0xe7c0
cltq
movq %rax, -0x70(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0xedb0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0x1acf0
jmp 0x19181
leaq -0x29(%rbp), %rdi
callq 0x10800
movl $0x0, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movslq -0x44(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x19256
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0xeb20
movl %eax, -0x7c(%rbp)
jmp 0x191ca
movl -0x7c(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jl 0x191d8
jmp 0x1922b
leaq 0x6097(%rip), %rdi      # 0x1f276
leaq 0x5ffc(%rip), %rsi      # 0x1f1e2
movl $0x2b, %edx
leaq 0x600e(%rip), %rcx      # 0x1f200
callq 0x3140
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x10800
jmp 0x19379
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xe670
jmp 0x19379
movslq -0x48(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xe570
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x19198
movslq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, %xmm0
movaps 0x5efb(%rip), %xmm1     # 0x1f180
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x5eff(%rip), %xmm1     # 0x1f190
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xe550
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x19314
movslq -0x5c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xe570
cvtsi2sdl (%rax), %xmm0
subsd -0x50(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
imull -0x60(%rbp), %eax
cvtsi2sd %eax, %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x192b4
leaq -0x28(%rbp), %rdi
callq 0xe550
movq %rax, %xmm1
movaps 0x5e57(%rip), %xmm0     # 0x1f180
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x5e5b(%rip), %xmm0     # 0x1f190
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xe670
movsd -0x98(%rbp), %xmm0
addq $0xa0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)