// File: /workspace/github_cpp_makefile_star2_O2/Kei18[P]pibt/./src/solver/pps.cpp
// Function: PPS::CLEAR(S*)
bool PPS::CLEAR(S* s) {
  Agent* aH = s->agents[0];
  Agent* aL = s->agents[1];
  Node* target = s->esv[0];

  // error check
  if (s->phase != SWAPPHASE::GO_TARGET) {
    std::cout << "error@PPS::CLEAR, phase is invalid" << "\n";
    std::exit(1);
  }
  if (s->agents.size() != 2) {
    std::cout << "error@PPS::CLEAR, the number of agent is not two" << "\n";
    std::exit(1);
  }
  if (aH->getNode() != target) {
    std::cout << "error@PPS::CLEAR, "
              << "agent " << aH->getId() << " is not on node "
              << target->getId() << "\n";
    std::exit(1);
  }

  Nodes neighbor = G->neighbor(target);
  Node* evacH = nullptr;
  Node* evacL = nullptr;
  Node* aLPos = aL->getNode();

  // case 1. free nodes
  for (auto v : neighbor) {
    if (v == aLPos) continue;
    if (!isFree(v)) continue;

    if (!evacH) {
      evacH = v;
    } else if (!evacL) {
      evacL = v;
      s->phase = SWAPPHASE::EVAC_H;
      break;
    }
  }

  // case 2. try push
  if (!evacL) {
    for (auto v : neighbor) {
      if (inArray(v, L)) continue;
      if (v == aLPos) continue;
      if (evacH && v == evacH) continue;

      Agent* a = *std::find_if(A.begin(), A.end(),
                               [v](Agent* a) { return a->getNode() == v; });
      H.push_back(aH);
      H.push_back(aL);
      Nodes T = { evacH };
      RES attempt = PUSH(a, T, true);

      if (attempt != RES::SUCCESS) continue;

      if (!evacH) {
        evacH = v;
      } else {
        evacL = v;
        s->phase = SWAPPHASE::EVAC_H;
        break;
      }
    }
  }

  // case 3. evac
  if (evacH && !evacL) {
    for (auto v : neighbor) {
      if (inArray(v, L)) continue;
      if (v == aLPos) continue;

      // occuping agents
      auto itrU = std::find_if(A.begin(), A.end(),
                               [v](Agent* a) { return a->getNode() == v; });
      if (itrU == A.end()) continue;
      Agent* a3 = *itrU;

      if (inArray(a3, M)) continue;
      if (inS(a3)) {
        if (CHECK_PRIORITY(s, a3) == CHECK::INVALID) {
          continue;
        }
      }

      Node* a3target = nullptr;
      Nodes evacHneighbor = G->neighbor(evacH);
      for (auto u : evacHneighbor) {
        if (u == target) continue;
        if (u == a3->getNode()) continue;
        a3target = u;
        break;
      }
      if (!a3target) continue;

      Nodes pi = SHORTEST_PATH(evacH, a3target, { aLPos });
      if (pi.size() < 2) continue;  // there is no path

      // temporary change goal node
      aH->setGoal(aL->getNode());

      for (auto u : G->neighbor(aLPos)) {
        if (u == a3target) continue;
        if (u == target) continue;
        // temporary change goal node
        aL->setGoal(u);

        Nodes prohibited = { a3target, target, evacH };
        RES attempt = PUSH(aL, prohibited, true);

        if (attempt != RES::SUCCESS) continue;

        // resolve swap target
        if (inS(a3)) {
          S* t = getS(a3);
          doneSwapers.push_back(t);
          for (auto b : t->agents) {
            b->setGoal(goals[b->getId()]);
            isTmpGoals[b->getId()] = false;
            if (b != a3) pushers.push_back(b);
          }
        }

        auto itrU = std::find(U.begin(), U.end(), a3);
        if (itrU != U.end()) U.erase(itrU);

        PUSH(aH, true);  // to aLPos

        a3->setGoal(pi[1]);
        isTmpGoals[a3->getId()] = true;
        pusherToSwaper.push_back(a3);
        s->agents.push_back(a3);
        s->phase = SWAPPHASE::CLEARING;
        PUSH(a3, true);  // to target

        evacL = a3->getNode();

        break;
      }

      // resolve
      aL->setGoal(goals[aL->getId()]);
      aH->setGoal(goals[aH->getId()]);

      if (evacL) break;
    }
  }

  // CLEAR is success
  if (s->phase == SWAPPHASE::EVAC_H) {
    s->target = aH->getNode();
    s->origin = aL->getNode();
    s->evacH = evacH;
    s->evacL = evacL;

    L.push_back(s->target);
    L.push_back(s->origin);
    L.push_back(s->evacH);
    L.push_back(s->evacL);

    return true;

  } else if (s->phase == SWAPPHASE::CLEARING) {

    s->area = { aH->getNode(), aL->getNode(), evacL };
    return true;

  }

  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdi, (%rsp)
movq 0x8(%rsi), %rax
movq (%rax), %rbx
movq %rbx, 0x18(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
cmpl $0x0, 0x78(%rsi)
jne 0x30b19
movq %rsi, %rbp
movq 0x10(%rsi), %rcx
subq %rax, %rcx
cmpq $0x10, %rcx
jne 0x30b29
movq 0x28(%rbp), %rax
movq (%rax), %rdx
cmpq %rdx, 0x8(%rbx)
jne 0x30b3e
leaq 0x8(%rbp), %rax
movq %rax, 0xf0(%rsp)
movq (%rsp), %rax
movq 0x38(%rax), %rsi
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0x40(%rsp)
callq 0x164ca
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
movq (%rbx), %r15
movq 0x8(%rbx), %r13
xorl %r14d, %r14d
cmpq %r13, %r15
je 0x3034b
movq (%r15), %r12
cmpq 0x28(%rsp), %r12
je 0x30335
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x30f96
testb %al, %al
je 0x30335
testq %r14, %r14
jne 0x3033b
movq %r12, %r14
addq $0x8, %r15
jmp 0x3030e
movl $0x2, 0x78(%rbp)
testq %r12, %r12
jne 0x30a26
movq %rbp, 0x38(%rsp)
movq 0xb8(%rsp), %r15
movq 0xc0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq (%rsp), %rbx
leaq 0x110(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0x128(%rbx), %rbp
cmpq 0x30(%rsp), %r15
je 0x304d5
movq (%r15), %r12
movq %r12, %rdi
movq 0x68(%rsp), %rsi
callq 0x2fc19
cmpq 0x28(%rsp), %r12
sete %cl
orb %cl, %al
jne 0x304cc
testq %r14, %r14
setne %al
cmpq %r14, %r12
sete %cl
testb %cl, %al
jne 0x304cc
movq 0x20(%rbx), %rcx
movq 0x28(%rbx), %rax
movq %rax, %rdx
subq %rcx, %rdx
movq %rdx, %rsi
sarq $0x5, %rsi
testq %rsi, %rsi
jle 0x30409
movq (%rcx), %rdi
cmpq %r12, 0x8(%rdi)
je 0x30456
movq 0x8(%rcx), %rdi
cmpq %r12, 0x8(%rdi)
je 0x30446
movq 0x10(%rcx), %rdi
cmpq %r12, 0x8(%rdi)
je 0x3044c
movq 0x18(%rcx), %rdi
cmpq %r12, 0x8(%rdi)
je 0x30452
addq $0x20, %rcx
decq %rsi
addq $-0x20, %rdx
jmp 0x303d0
sarq $0x3, %rdx
cmpq $0x1, %rdx
je 0x30439
cmpq $0x2, %rdx
je 0x3042c
cmpq $0x3, %rdx
jne 0x30459
movq (%rcx), %rdx
cmpq %r12, 0x8(%rdx)
je 0x30456
addq $0x8, %rcx
movq (%rcx), %rdx
cmpq %r12, 0x8(%rdx)
je 0x30456
addq $0x8, %rcx
movq (%rcx), %rdx
cmpq %r12, 0x8(%rdx)
cmoveq %rcx, %rax
jmp 0x30459
addq $0x8, %rcx
jmp 0x30456
addq $0x10, %rcx
jmp 0x30456
addq $0x18, %rcx
movq %rcx, %rax
movq (%rax), %r13
movq %rbp, %rbx
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x930e
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x930e
movq %r14, 0x70(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0x88(%rsp), %rcx
callq 0x927a
movq (%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
pushq $0x1
popq %rcx
callq 0x2eaf6
movq %rbx, %rdi
testq %r14, %r14
movq (%rsp), %rbx
je 0x304c1
testl %eax, %eax
je 0x30ae5
testl %eax, %eax
cmoveq %r12, %r14
callq 0x990e
addq $0x8, %r15
jmp 0x3037c
xorl %r12d, %r12d
movq 0x38(%rsp), %rbp
testq %r14, %r14
je 0x30a15
testq %r12, %r12
jne 0x30a15
movq %r14, 0x20(%rsp)
movq 0xb8(%rsp), %r13
movq 0xc0(%rsp), %r12
movq (%rsp), %rcx
leaq 0xe0(%rcx), %rax
movq %rax, 0x108(%rsp)
leaq 0xc8(%rcx), %rax
movq %rax, 0xd8(%rsp)
leaq 0x160(%rcx), %rax
movq %rax, 0x30(%rsp)
leaq 0x68(%rcx), %rax
movq %rax, 0x110(%rsp)
leaq 0xf8(%rcx), %rax
movq %rax, 0xe0(%rsp)
leaq 0x98(%rcx), %rax
movq %rax, 0xe8(%rsp)
leaq 0x118(%rsp), %r14
cmpq %r12, %r13
je 0x30a0d
movq (%r13), %r15
movq %r15, %rdi
movq 0x68(%rsp), %rsi
callq 0x2fc19
cmpq 0x28(%rsp), %r15
sete %cl
orb %cl, %al
jne 0x30a04
movq (%rsp), %rcx
movq 0x20(%rcx), %rax
movq 0x28(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, %rsi
sarq $0x5, %rsi
testq %rsi, %rsi
jle 0x305e0
movq (%rax), %rdi
cmpq %r15, 0x8(%rdi)
je 0x30631
movq 0x8(%rax), %rdi
cmpq %r15, 0x8(%rdi)
je 0x30621
movq 0x10(%rax), %rdi
cmpq %r15, 0x8(%rdi)
je 0x30627
movq 0x18(%rax), %rdi
cmpq %r15, 0x8(%rdi)
je 0x3062d
addq $0x20, %rax
decq %rsi
addq $-0x20, %rdx
jmp 0x305a7
sarq $0x3, %rdx
cmpq $0x1, %rdx
je 0x30614
cmpq $0x2, %rdx
je 0x30607
cmpq $0x3, %rdx
jne 0x30a04
movq (%rax), %rdx
cmpq %r15, 0x8(%rdx)
je 0x30631
addq $0x8, %rax
movq (%rax), %rdx
cmpq %r15, 0x8(%rdx)
je 0x30631
addq $0x8, %rax
movq (%rax), %rdx
cmpq %r15, 0x8(%rdx)
cmovneq %rcx, %rax
jmp 0x30631
addq $0x8, %rax
jmp 0x30631
addq $0x10, %rax
jmp 0x30631
addq $0x18, %rax
cmpq %rcx, %rax
je 0x30a04
movq (%rax), %rdi
movq %rdi, 0x8(%rsp)
movq 0x108(%rsp), %rsi
callq 0x2ea4f
testb %al, %al
jne 0x30a04
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
callq 0x2ea90
testb %al, %al
je 0x30682
movq 0x8(%rsp), %rdx
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x2febe
testl %eax, %eax
jne 0x30a04
movq (%rsp), %rax
movq 0x38(%rax), %rsi
leaq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rdx
callq 0x164ca
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq 0x40(%rsp), %rsi
cmpq %rcx, %rax
je 0x3099b
movq (%rax), %r15
cmpq %rsi, %r15
je 0x306c4
cmpq 0x8(%rdx), %r15
jne 0x306ca
addq $0x8, %rax
jmp 0x306ad
testq %r15, %r15
je 0x3099b
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x927a
movq %r12, 0xd0(%rsp)
movq (%rsp), %rax
movq 0x38(%rax), %rsi
movq (%rsi), %rax
leaq 0x70(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
movq %rbx, %r8
callq *0x20(%rax)
movq %rbx, %rdi
callq 0x990e
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
cmpq $0x9, %rax
jb 0x309e8
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq (%rsp), %rax
movq 0x38(%rax), %rsi
leaq 0x88(%rsp), %rdi
movq 0x28(%rsp), %rdx
callq 0x164ca
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %r8
xorl %r12d, %r12d
movq 0x40(%rsp), %rsi
movq %r8, 0xf8(%rsp)
cmpq %r8, %rdi
je 0x309a7
movq (%rdi), %rax
cmpq %r15, %rax
sete %cl
cmpq %rsi, %rax
sete %dl
orb %cl, %dl
jne 0x30992
movq %rdi, 0x100(%rsp)
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %r15, 0xa0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq %r14, %rbx
movq %r14, %rdi
leaq 0xa0(%rsp), %rsi
pushq $0x3
popq %rdx
leaq 0x48(%rsp), %rcx
callq 0x927a
movq 0x10(%rsp), %rsi
movq (%rsp), %rdi
movq %rbx, %rdx
pushq $0x1
popq %rcx
callq 0x2eaf6
movl %eax, %ebx
testl %eax, %eax
jne 0x30971
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
callq 0x2ea90
testb %al, %al
je 0x308c1
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
callq 0x2eeac
movq %rax, 0xa0(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x31606
movq 0xa0(%rsp), %rax
movq 0x8(%rax), %r12
movq 0x10(%rax), %rbp
cmpq %rbp, %r12
je 0x308bc
movq (%r12), %rax
movq %rax, 0x48(%rsp)
movslq (%rax), %rsi
movq (%rsp), %rcx
movq 0x148(%rcx), %rcx
movq (%rcx,%rsi,8), %rcx
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rdi
callq 0x315e8
notq %rdx
andq %rdx, (%rax)
movq 0x48(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x308b6
movq 0x110(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x930e
addq $0x8, %r12
jmp 0x30864
movq 0x38(%rsp), %rbp
movq (%rsp), %rax
movq 0xf8(%rax), %rdi
movq 0x100(%rax), %rsi
leaq 0x8(%rsp), %r12
movq %r12, %rdx
callq 0x2dcba
movq (%rsp), %rcx
cmpq 0x100(%rcx), %rax
je 0x308fd
movq 0xe0(%rsp), %rdi
movq %rax, %rsi
callq 0x2d97c
movq 0x18(%rsp), %rsi
movq (%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2ea10
movq 0x8(%rsp), %rax
movq 0x70(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x10(%rax)
movslq (%rax), %rsi
movq 0x30(%rsp), %rdi
callq 0x315e8
orq %rdx, (%rax)
movq 0xe8(%rsp), %rdi
movq %r12, %rsi
callq 0x930e
movq 0xf0(%rsp), %rdi
movq %r12, %rsi
callq 0x930e
movl $0x1, 0x78(%rbp)
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2ea10
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x990e
testl %ebx, %ebx
movq 0x40(%rsp), %rsi
movq 0x100(%rsp), %rdi
movq 0xf8(%rsp), %r8
je 0x309a7
addq $0x8, %rdi
jmp 0x3078c
leaq 0x50(%rsp), %rdi
callq 0x990e
jmp 0x30a04
leaq 0x88(%rsp), %rdi
callq 0x990e
movq 0x10(%rsp), %rax
movslq (%rax), %rcx
movq (%rsp), %rdx
movq 0x148(%rdx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
movslq (%rax), %rcx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x10(%rax)
testq %r12, %r12
jne 0x30b00
leaq 0x70(%rsp), %rdi
callq 0x990e
leaq 0x50(%rsp), %rdi
callq 0x990e
movq 0xd0(%rsp), %r12
addq $0x8, %r13
jmp 0x30564
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
movl 0x78(%rbp), %eax
cmpl $0x1, %eax
je 0x30a8a
cmpl $0x2, %eax
jne 0x30ac2
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
leaq 0x40(%rbp), %rsi
movq %rax, 0x40(%rbp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x48(%rbp)
movq %r14, 0x50(%rbp)
movq %r12, 0x58(%rbp)
movq (%rsp), %rbx
addq $0x110, %rbx            # imm = 0x110
movq %rbx, %rdi
callq 0x171da
leaq 0x48(%rbp), %rsi
movq %rbx, %rdi
callq 0x171da
leaq 0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x171da
addq $0x58, %rbp
movq %rbx, %rdi
movb $0x1, %bl
movq %rbp, %rsi
callq 0x171da
jmp 0x30ac4
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
leaq 0x68(%rsp), %rdx
movq %rax, -0x18(%rdx)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rdx)
movq %r12, -0x8(%rdx)
addq $0x60, %rbp
leaq 0x50(%rsp), %rsi
movq %rbp, %rdi
callq 0x31e94
movb $0x1, %bl
jmp 0x30ac4
xorl %ebx, %ebx
leaq 0xb8(%rsp), %rdi
callq 0x990e
movl %ebx, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %rbp
movl $0x2, 0x78(%rbp)
leaq 0x50(%rsp), %rdi
callq 0x990e
jmp 0x304dd
leaq 0x70(%rsp), %rdi
callq 0x990e
leaq 0x50(%rsp), %rdi
callq 0x990e
jmp 0x30a10
movq 0x29478(%rip), %rdi     # 0x59f98
leaq 0xccee(%rip), %rsi      # 0x3d815
jmp 0x30b37
movq 0x29468(%rip), %rdi     # 0x59f98
leaq 0xcd01(%rip), %rsi      # 0x3d838
callq 0x63a0
jmp 0x30b87
movq 0x29453(%rip), %rdi     # 0x59f98
leaq 0xcd1d(%rip), %rsi      # 0x3d869
movq %rdx, %r14
callq 0x63a0
leaq 0xcd21(%rip), %rsi      # 0x3d87c
movq %rax, %rdi
callq 0x63a0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x6630
leaq 0xcd0f(%rip), %rsi      # 0x3d883
movq %rax, %rdi
callq 0x63a0
movl (%r14), %esi
movq %rax, %rdi
callq 0x6630
leaq 0x99d5(%rip), %rsi      # 0x3a563
movq %rax, %rdi
callq 0x63a0
pushq $0x1
popq %rdi
callq 0x6450
movq %rax, %rbx
jmp 0x30bed
jmp 0x30c09
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
jmp 0x30bf2
jmp 0x30bc5
jmp 0x30bd0
jmp 0x30bd0
jmp 0x30c09
movq %rax, %rbx
jmp 0x30be0
jmp 0x30c09
jmp 0x30bd0
jmp 0x30bd0
movq %rax, %rbx
jmp 0x30bf7
jmp 0x30c09
jmp 0x30bd0
jmp 0x30c09
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x990e
leaq 0x88(%rsp), %rdi
callq 0x990e
leaq 0x70(%rsp), %rdi
callq 0x990e
leaq 0x50(%rsp), %rdi
callq 0x990e
jmp 0x30c0c
jmp 0x30c09
jmp 0x30c09
jmp 0x30c09
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x990e
movq %rbx, %rdi
callq 0x6650
nop