// File: /workspace/github_cpp_makefile_star2_O2/Kei18[P]pibt/./src/solver/tp.cpp
// Function: TP::getPickDelivPath(Agent*, Node*, Node*, Node*, int, bool)
Nodes TP::getPickDelivPath(Agent* a,
                           Node* s,
                           Node* g1,
                           Node* g2,
                           int startTime,
                           bool futureCollision)
{
  Nodes path;
  Nodes C;
  AN2 *l, *n;
  int t, f, cost;
  std::string key;
  bool prohibited, goalCheck;

  Nodes pathends;
  Node* v;
  for (auto p : paths) {
    v = p[p.size() - 1];
    if (v != s && v != a->getNode()) pathends.push_back(v);
  }
  int cost_g1tog2 = pathDist(g1, g2, pathends);

  std::unordered_set<std::string> OPEN;
  std::unordered_map<std::string, AN2*> table;

  t = startTime - 1;
  l = new AN2 { s, g1, true, startTime - 1,
                pathDist(s, g1) + cost_g1tog2,
                nullptr };
  key = getKey(t, s);
  table.emplace(key, l);
  OPEN = { key };

  while (!OPEN.empty()) {
    // argmin
    auto itr1 = std::min_element(OPEN.begin(), OPEN.end(),
                                 [&table] (std::string a, std::string b)
                                 { auto eleA = table.at(a);
                                   auto eleB = table.at(b);
                                   if (eleA->f == eleB->f) {
                                     return eleA->t > eleB->t;
                                   }
                                   return eleA->f < eleB->f;
                                 });
    key = *itr1;
    n = table.at(key);
    t = n->t;

    // check goal
    if (n->v == n->g && n->g == g2) {
      goalCheck = true;

      if (futureCollision) {
        for (auto p : paths) {
          if (t + 1 >= p.size()) continue;

          for (auto itr = p.begin() + t + 1; itr < p.end(); ++itr) {
            if ((*itr) == n->v) {  // collision in future
              goalCheck = false;
              break;
            }
          }
          if (!goalCheck) break;
        }
      }

      if (goalCheck) break;

    } else if (n->v == n->g && n->g == g1) {
      n->g = g2;  // next target
    }

    // update list
    n->open = false;
    OPEN.erase(itr1);

    // search neighbor
    C = { n->v };
    for (auto u : G->neighbor(n->v)) {
      if (!inArray(u, pathends)) C.push_back(u);
    }

    for (auto m : C) {
      t = n->t + 1;

      if (t > 0) {
        prohibited = false;

        for (auto p : paths) {
          if (t + 1 > p.size()) continue;
          if (p[t] == m) { // collision
            prohibited = true;
            break;
          }
          if (p[t] == n->v && p[t-1] == m) {  // intersection
            prohibited = true;
            break;
          }
        }

        if (prohibited) continue;
      }

      key = getKey(t, m);
      auto itr2 = table.find(key);
      if (itr2 == table.end()) {
        table.emplace(key, new AN2 { m, n->g, true, t, 100000, n });
      }
      l = table.at(key);
      if (!l->open) continue;

      // cost = pathDist(m, n->g, pathends);
      cost = pathDist(m, n->g);
      f = t + cost;
      if (n->g == g1) f += cost_g1tog2;

      if (l->f > f) {
        l->t = t;
        l->f = f;
        l->p = n;
      }

      OPEN.insert(key);
    }
  }

  // back tracking
  if (n->v == g2) {  // check failed or not
    while (n->p) {
      path.push_back(n->v);
      n = n->p;
    }
    path.push_back(s);
    std::reverse(path.begin(), path.end());
  }

  return path;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r9, 0x30(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, %rbx
movq %rdx, %r14
movq %rcx, 0xb8(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rdi)
andq $0x0, 0xa0(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, 0x18(%rsp)
movups %xmm0, (%rdi)
movaps %xmm0, 0x90(%rsp)
movb $0x0, (%rax)
leaq 0xc0(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movq 0x88(%rsi), %r12
movq %rsi, 0x10(%rsp)
movq 0x90(%rsi), %rbp
leaq 0xd8(%rsp), %r13
cmpq %rbp, %r12
je 0x345ed
movq %r13, %rdi
movq %r12, %rsi
callq 0x15d3a
movq 0xe0(%rsp), %rax
movq -0x8(%rax), %rax
movq %rax, 0xb0(%rsp)
cmpq %rbx, %rax
je 0x345df
cmpq 0x8(%r14), %rax
je 0x345df
movq %r15, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x171da
movq %r13, %rdi
callq 0x990e
addq $0x18, %r12
jmp 0x345a0
leaq 0xc0(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x32f04
movl %eax, 0x28(%rsp)
leaq 0x108(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x160(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
pushq $0x28
popq %rdi
callq 0x63b0
movq %rax, %r14
movl 0x1a0(%rsp), %ebp
decl %ebp
movq %rbx, (%rax)
movq 0x38(%rsp), %rdx
movq %rdx, 0x8(%rax)
movb $0x1, 0x10(%rax)
movl %ebp, 0x14(%rax)
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x32cd4
addl 0x28(%rsp), %eax
movl %eax, 0x18(%r14)
andq $0x0, 0x20(%r14)
movq %r14, 0x60(%rsp)
leaq 0x40(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x32f4c
leaq 0x70(%rsp), %rbx
leaq 0x40(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6470
movq %r14, %rdi
callq 0x61d0
leaq 0x130(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movq %rbx, %rsi
callq 0x3631a
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x64e0
leaq 0xd8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x35c92
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x61d0
cmpq $0x0, 0xf0(%rsp)
je 0x34b2c
movq 0xe8(%rsp), %rbx
testq %rbx, %rbx
je 0x347b7
movq %rbx, %r12
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x347ba
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x64e0
leaq 0x8(%r12), %rsi
leaq 0x110(%rsp), %rdi
callq 0x64e0
leaq 0x130(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3670c
movq (%rax), %r14
movq %r13, %rdi
leaq 0x110(%rsp), %rsi
callq 0x3670c
movq (%rax), %rax
movl 0x18(%rax), %ecx
cmpl %ecx, 0x18(%r14)
jne 0x34792
movl 0x14(%r14), %ecx
cmpl 0x14(%rax), %ecx
setg %bpl
jmp 0x34796
setl %bpl
leaq 0x110(%rsp), %rdi
callq 0x61d0
movq %r15, %rdi
callq 0x61d0
testb %bpl, %bpl
cmovneq %rbx, %r12
jmp 0x34729
xorl %r12d, %r12d
leaq 0x8(%r12), %rsi
leaq 0x70(%rsp), %rdi
callq 0x6240
leaq 0x130(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3670c
movq (%rax), %rcx
movq 0x8(%rcx), %rax
movq %rcx, 0x8(%rsp)
movq (%rcx), %rcx
xorq %rax, %rcx
movq %rax, %rdx
xorq 0x30(%rsp), %rdx
orq %rcx, %rdx
jne 0x3488a
cmpb $0x0, 0x1a8(%rsp)
je 0x34b2c
movq 0x8(%rsp), %rax
movslq 0x14(%rax), %rbx
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rbp
movq 0x90(%rax), %r14
leaq 0x1(%rbx), %r13
shlq $0x3, %rbx
cmpq %r14, %rbp
je 0x34b2c
movq %r15, %rdi
movq %rbp, %rsi
callq 0x15d3a
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0x3487c
addq %rbx, %rcx
addq $0x8, %rcx
cmpq %rax, %rcx
jae 0x3487c
movq (%rcx), %rdx
addq $0x8, %rcx
movq 0x8(%rsp), %rsi
cmpq (%rsi), %rdx
jne 0x34864
jmp 0x348a4
movq %r15, %rdi
callq 0x990e
addq $0x18, %rbp
jmp 0x34830
xorq 0x38(%rsp), %rax
orq %rax, %rcx
jne 0x348ac
movq 0x30(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x8(%rcx)
jmp 0x348ac
movq %r15, %rdi
callq 0x990e
movq 0x8(%rsp), %rbx
movb $0x0, 0x10(%rbx)
leaq 0xd8(%rsp), %rdi
movq %r12, %rsi
callq 0x36026
movq (%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
leaq 0x48(%rsp), %rdx
callq 0x31e94
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rsi
movq (%rbx), %rdx
movq %r15, %rdi
callq 0x164ca
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
cmpq %r14, %rbx
je 0x3493c
movq (%rbx), %rdi
movq %rdi, 0x110(%rsp)
leaq 0xc0(%rsp), %rsi
callq 0x353b4
testb %al, %al
jne 0x34936
leaq 0x90(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x171da
addq $0x8, %rbx
jmp 0x34900
movq %r15, %rdi
callq 0x990e
movq 0x90(%rsp), %rbx
movq 0x98(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq 0xa8(%rsp), %rbx
je 0x34706
movq %rbx, 0x68(%rsp)
movq (%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rax
movl 0x14(%rax), %r12d
leal 0x1(%r12), %eax
movl %eax, 0x2c(%rsp)
testl %r12d, %r12d
js 0x34a07
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rbx
movq 0x90(%rax), %r14
leaq 0x2(%r12), %rbp
movl 0x2c(%rsp), %r13d
cmpq %r14, %rbx
je 0x34a07
movq %r15, %rdi
movq %rbx, %rsi
callq 0x15d3a
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jb 0x349f9
movq (%rax,%r13,8), %rcx
cmpq 0x20(%rsp), %rcx
je 0x34b16
movq 0x8(%rsp), %rdx
cmpq (%rdx), %rcx
jne 0x349f9
movq 0x20(%rsp), %rcx
cmpq %rcx, (%rax,%r12,8)
je 0x34b16
movq %r15, %rdi
callq 0x990e
addq $0x18, %rbx
jmp 0x349ab
movq %r15, %rdi
movl 0x2c(%rsp), %ebp
movl %ebp, %esi
movq 0x20(%rsp), %rdx
callq 0x32f4c
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6470
movq %r15, %rdi
callq 0x61d0
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
callq 0x3672a
movq 0x68(%rsp), %rbx
testq %rax, %rax
jne 0x34a95
pushq $0x28
popq %rdi
callq 0x63b0
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, 0x8(%rax)
movb $0x1, 0x10(%rax)
movl %ebp, 0x14(%rax)
movl $0x186a0, 0x18(%rax)    # imm = 0x186A0
movq %rdx, 0x20(%rax)
movq %rax, 0x40(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
callq 0x36758
leaq 0x130(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3670c
movq (%rax), %rax
movq %rax, 0x60(%rsp)
cmpb $0x1, 0x10(%rax)
jne 0x34b23
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x32cd4
addl %ebp, %eax
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %rdx
cmpq %rcx, 0x8(%rdx)
movl $0x0, %ecx
cmovel 0x28(%rsp), %ecx
addl %eax, %ecx
movq 0x60(%rsp), %rax
cmpl %ecx, 0x18(%rax)
jle 0x34b02
movl %ebp, 0x14(%rax)
movl %ecx, 0x18(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rax)
leaq 0xd8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x36252
jmp 0x34b23
movq %r15, %rdi
callq 0x990e
movq 0x68(%rsp), %rbx
addq $0x8, %rbx
jmp 0x3495c
movq 0x30(%rsp), %rax
movq 0x8(%rsp), %rbx
cmpq %rax, (%rbx)
jne 0x34b78
jmp 0x34b4e
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x171da
movq 0x20(%rbx), %rbx
cmpq $0x0, 0x20(%rbx)
jne 0x34b3d
leaq 0xb8(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x171da
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x18697
leaq 0x130(%rsp), %rdi
callq 0x355e0
leaq 0xd8(%rsp), %rdi
callq 0x22af2
leaq 0xc0(%rsp), %rdi
callq 0x990e
leaq 0x70(%rsp), %rdi
callq 0x61d0
leaq 0x90(%rsp), %rdi
callq 0x990e
movq 0x18(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x34c0c
jmp 0x34c40
jmp 0x34c40
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x63c0
jmp 0x34c43
jmp 0x34c03
jmp 0x34c40
jmp 0x34c40
jmp 0x34c40
jmp 0x34c40
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x990e
jmp 0x34c5d
jmp 0x34c40
jmp 0x34c40
movq %rax, %rbx
jmp 0x34c5d
jmp 0x34c40
jmp 0x34c40
movq %rax, %rbx
jmp 0x34c23
jmp 0x34c40
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x61d0
leaq 0x40(%rsp), %rdi
callq 0x61d0
jmp 0x34c43
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x990e
jmp 0x34c43
jmp 0x34c40
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x355e0
leaq 0xd8(%rsp), %rdi
callq 0x22af2
leaq 0xc0(%rsp), %rdi
callq 0x990e
leaq 0x70(%rsp), %rdi
callq 0x61d0
leaq 0x90(%rsp), %rdi
callq 0x990e
movq 0x18(%rsp), %rdi
callq 0x990e
movq %rbx, %rdi
callq 0x6650
nop