// File: Kei18[P]pibt/./src/solver/pibt.cpp
// Function: PIBT::chooseNode(Agent*, std::vector<Node*, std::allocator<Node*>>)
Node* PIBT::chooseNode(Agent* a, Nodes C) {
  if (C.empty()) {
    std::cout << "error@PIBT::chooseNode, C is empty" << "\n";
    std::exit(1);
  }

  // randomize
  std::shuffle(C.begin(), C.end(), *MT);

  if (!a->hasGoal()) {
    if (inArray(a->getNode(), C)) {  // try to stay
      return a->getNode();
    } else {
      return C[0];  // random walk
    }
  }

  Nodes cs;
  int minCost = 10000;
  int cost;
  Node* g = a->getGoal();

  // fast implementation
  Nodes p = G->getPath(a->getNode(), g);
  if (p.size() > 1 && inArray(p[1], C)) return p[1];

  for (auto v : C) {
    cost = pathDist(v, g);
    if (cost < minCost) {
      minCost = cost;
      cs.clear();
      cs.push_back(v);
    } else if (cost == minCost) {
      cs.push_back(v);
    }
  }

  if (cs.size() == 1) return cs[0];

  // tie break
  bool contained;
  for (auto v : cs) {  // avoid tabu list
    contained = std::any_of(A.begin(), A.end(),
                            [v](Agent* b){ return b->getNode() == v; });
    if (!contained) return v;
  }

  return cs[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdx, -0x100(%rbp)
movq %rdi, %rax
movq -0x100(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
movq %rax, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdi, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf0(%rbp)
callq 0x3ec00
testb $0x1, %al
jne 0x8ab45
jmp 0x8ab71
movq 0x7644c(%rip), %rdi     # 0x100f98
leaq 0x2bf13(%rip), %rsi     # 0xb6a66
callq 0x84d0
movq %rax, %rdi
leaq 0x28a55(%rip), %rsi     # 0xb35b7
callq 0x84d0
movl $0x1, %edi
callq 0x85d0
movq -0xf8(%rbp), %rdi
callq 0x3b730
movq -0xf8(%rbp), %rdi
movq %rax, -0x28(%rbp)
callq 0x3b760
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0x18(%rax), %rdx
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3f7c0
movq -0x18(%rbp), %rdi
callq 0x3ae10
testb $0x1, %al
jne 0x8ac05
movq -0x18(%rbp), %rdi
callq 0x51cb0
movq -0xf8(%rbp), %rsi
movq %rax, %rdi
callq 0x8aa90
testb $0x1, %al
jne 0x8abd7
jmp 0x8abe9
movq -0x18(%rbp), %rdi
callq 0x51cb0
movq %rax, -0x8(%rbp)
jmp 0x8af72
movq -0xf8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xda90
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x8af72
leaq -0x48(%rbp), %rdi
callq 0x3e880
movl $0x2710, -0x4c(%rbp)    # imm = 0x2710
movq -0x18(%rbp), %rdi
callq 0x56820
movq %rax, -0x108(%rbp)
jmp 0x8ac27
movq -0xf0(%rbp), %rax
movq -0x108(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x118(%rbp)
movq -0x18(%rbp), %rdi
callq 0x51cb0
movq %rax, -0x110(%rbp)
jmp 0x8ac56
movq -0x110(%rbp), %rdx
movq -0x118(%rbp), %rsi
movq -0x58(%rbp), %rcx
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq -0x80(%rbp), %rdi
callq *%rax
jmp 0x8ac77
leaq -0x80(%rbp), %rdi
callq 0x3b0e0
cmpq $0x1, %rax
jbe 0x8ad0a
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0xda90
movq -0xf8(%rbp), %rsi
movq (%rax), %rdi
callq 0x8aa90
movb %al, -0x119(%rbp)
jmp 0x8acaf
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x8acbb
jmp 0x8ad0a
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0xda90
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x8af53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x8af67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xd7d0
jmp 0x8af67
movq -0xf8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x3b730
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
callq 0x3b760
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3b790
testb $0x1, %al
jne 0x8ad5a
jmp 0x8adfe
leaq -0x98(%rbp), %rdi
callq 0x3bb00
movq -0xf0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x9d410
movl %eax, -0x120(%rbp)
jmp 0x8ad8f
movl -0x120(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x8adcd
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x3e9f0
movq -0x128(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3f490
jmp 0x8adcb
jmp 0x8adeb
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x8ade9
leaq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3f490
jmp 0x8ade7
jmp 0x8ade9
jmp 0x8adeb
jmp 0x8aded
leaq -0x98(%rbp), %rdi
callq 0x3bad0
jmp 0x8ad3e
leaq -0x48(%rbp), %rdi
callq 0x3b0e0
cmpq $0x1, %rax
jne 0x8ae30
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xda90
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x8af53
leaq -0x48(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x3b730
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x3b760
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x3b790
testb $0x1, %al
jne 0x8ae7d
jmp 0x8af35
leaq -0xc0(%rbp), %rdi
callq 0x3bb00
movq -0xf0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
addq $0x20, %rdi
movq %rdi, -0x138(%rbp)
callq 0x39b60
movq -0x138(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0x399f0
movq %rax, -0xe0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
callq 0x8af90
movb %al, -0x129(%rbp)
jmp 0x8aef4
movb -0x129(%rbp), %al
andb $0x1, %al
movb %al, -0xa9(%rbp)
testb $0x1, -0xa9(%rbp)
jne 0x8af22
movq -0xd0(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x8af53
jmp 0x8af24
leaq -0xc0(%rbp), %rdi
callq 0x3bad0
jmp 0x8ae61
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xda90
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xd7d0
leaq -0x48(%rbp), %rdi
callq 0xd7d0
jmp 0x8af72
leaq -0x48(%rbp), %rdi
callq 0xd7d0
jmp 0x8af7f
movq -0x8(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x8880
nopl (%rax,%rax)