// File: xukmin[P]rase/routing-metric-calculators.cc
// Function: RobustnessCalculator::CalculateMetric(SensorNetwork const&)
double RobustnessCalculator::CalculateMetric(const SensorNetwork& network) {
  if (network.num_sensors() < 3) {
    return 0.0;
  }

  std::vector<int> sorted;
  SortSensorsByUsage(network, &sorted);

  // Assume the most frequently used sensors (except the base station) is failed.
  std::set<int> disconnected;
  int num_failed = 1;  // TODO: support more than one failed sensors.
  for (int i = 1; i <= num_failed; i++) {
    disconnected.insert(sorted[i]);
  }

  std::set<int> connected;
  connected.insert(0);  // The base station is always connected (with itself).

  for (int i = 1; i < network.num_sensors(); i++) {
    std::set<int> parents;
    int parent = i;
    while (disconnected.find(parent) == disconnected.end() &&
           connected.find(parent) == connected.end()) {
      parents.insert(parent);
      parent = network.GetParent(parent);
      assert(parent >= 0);
    }
    if (disconnected.find(parent) != disconnected.end()) {
      disconnected.insert(parents.begin(), parents.end());
    } else {
      connected.insert(parents.begin(), parents.end());
    }
  }
  return connected.size() * 1.0 / (network.num_sensors() - num_failed);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
cmpl $0x3, %eax
jge 0x1963e
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19ac0
leaq -0x30(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0xe530
movq -0x178(%rbp), %rsi
movq -0x18(%rbp), %rdi
callq 0x193c0
jmp 0x19660
leaq -0x70(%rbp), %rdi
callq 0xe2e0
movl $0x1, -0x74(%rbp)
movl $0x1, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x74(%rbp), %eax
jg 0x196eb
movslq -0x78(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0xe570
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0xe390
movb %dl, -0x181(%rbp)
movq %rax, -0x180(%rbp)
jmp 0x196a7
movb -0x181(%rbp), %al
movq -0x180(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movb %al, -0x80(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x19677
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x19ab5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x19aac
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0xe2e0
movq -0x1a0(%rbp), %rdi
movl $0x0, -0xbc(%rbp)
leaq -0xbc(%rbp), %rsi
callq 0xeb50
movb %dl, -0x191(%rbp)
movq %rax, -0x190(%rbp)
jmp 0x1972a
movb -0x191(%rbp), %al
movq -0x190(%rbp), %rcx
movq %rcx, -0xd0(%rbp)
movb %al, -0xc8(%rbp)
movl $0x1, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x1a8(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x1a4(%rbp)
jmp 0x1976b
movl -0x1a8(%rbp), %eax
movl -0x1a4(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19a16
leaq -0x108(%rbp), %rdi
callq 0xe2e0
movl -0xd4(%rbp), %eax
movl %eax, -0x10c(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
callq 0x1afe0
movq %rax, -0x1b0(%rbp)
jmp 0x197b0
movq -0x1b0(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xe4d0
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x14e80
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x1b1(%rbp)
jne 0x197f2
jmp 0x19848
leaq -0xb8(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
callq 0x1afe0
movq %rax, -0x1c0(%rbp)
jmp 0x1980e
movq -0x1c0(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xe4d0
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x14e80
movb %al, -0x1b1(%rbp)
movb -0x1b1(%rbp), %al
testb $0x1, %al
jne 0x19857
jmp 0x19913
leaq -0x108(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
callq 0xe390
movb %dl, -0x1c9(%rbp)
movq %rax, -0x1c8(%rbp)
jmp 0x19879
movb -0x1c9(%rbp), %al
movq -0x1c8(%rbp), %rcx
movq %rcx, -0x140(%rbp)
movb %al, -0x138(%rbp)
movq -0x18(%rbp), %rdi
movl -0x10c(%rbp), %esi
callq 0xeb20
movl %eax, -0x1d0(%rbp)
jmp 0x198aa
movl -0x1d0(%rbp), %eax
movl %eax, -0x10c(%rbp)
cmpl $0x0, -0x10c(%rbp)
jl 0x198c1
jmp 0x1990e
leaq 0x59ae(%rip), %rdi      # 0x1f276
leaq 0x5913(%rip), %rsi      # 0x1f1e2
movl $0x7c, %edx
leaq 0x59a7(%rip), %rcx      # 0x1f282
callq 0x3140
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x19aa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x62d0
jmp 0x19aa0
jmp 0x19797
leaq -0x70(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
callq 0x1afe0
movq %rax, -0x1d8(%rbp)
jmp 0x1992c
movq -0x1d8(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xe4d0
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xe950
testb $0x1, %al
jne 0x19963
jmp 0x199ab
leaq -0x108(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0xe4a0
movq -0x1e0(%rbp), %rdi
movq %rax, -0x158(%rbp)
callq 0xe4d0
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rsi
movq -0x160(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x1b020
jmp 0x199a9
jmp 0x199f6
leaq -0x108(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0xe4a0
movq -0x1e8(%rbp), %rdi
movq %rax, -0x168(%rbp)
callq 0xe4d0
movq %rax, -0x170(%rbp)
movq -0x168(%rbp), %rsi
movq -0x170(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
callq 0x1b020
jmp 0x199f4
jmp 0x199f6
leaq -0x108(%rbp), %rdi
callq 0x62d0
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x1974e
leaq -0xb8(%rbp), %rdi
callq 0x17010
movq %rax, %xmm0
movaps 0x5752(%rip), %xmm1     # 0x1f180
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x5756(%rip), %xmm1     # 0x1f190
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movq -0x18(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x1ec(%rbp)
jmp 0x19a62
movsd -0x1f8(%rbp), %xmm0
movl -0x1ec(%rbp), %eax
subl -0x74(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x62d0
leaq -0x70(%rbp), %rdi
callq 0x62d0
leaq -0x30(%rbp), %rdi
callq 0xe670
jmp 0x19ac0
leaq -0xb8(%rbp), %rdi
callq 0x62d0
leaq -0x70(%rbp), %rdi
callq 0x62d0
leaq -0x30(%rbp), %rdi
callq 0xe670
jmp 0x19ace
movsd -0x8(%rbp), %xmm0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x33d0
nopw (%rax,%rax)