// File: /workspace/github_cpp_makefile_star2_O2/Kei18[P]pibt/./src/solver/pps.cpp
// Function: PPS::SWAP(S*)
RES PPS::SWAP(S* s) {
  // swapping phase
  if (s->phase == SWAPPHASE::EVAC_H ||
      s->phase == SWAPPHASE::EVAC_L ||
      s->phase == SWAPPHASE::SWAP_DONE) {
    SWAP_PRIMITIVES(s);
    return RES::SUCCESS;
  }

  Agent* aH = s->agents[0];
  Agent* aL = s->agents[1];
  Node* target = s->esv[0];

  if (s->agents.size() == 2) {  // l.1
    if (aH->getNode() != target) {  // l.2
      PUSH(s);  // l.3
    } else {  // swap starts
      if (CLEAR(s)) {  // l.7
        if (s->phase != SWAPPHASE::CLEARING) {
          SWAP_PRIMITIVES(s);  // l.6
        }
      } else {
        if (FIND_NEW_VERTEX(s) == RES::SUCCESS) {
          SWAP(s);
        } else {
          // for avoid collision
          M.push_back(aH);
          M.push_back(aL);
        }
      }
    }
  } else if (s->agents.size() == 3) {
    if (s->phase != SWAPPHASE::CLEARING) {
      std::cout << "error@PPS::SWAP, there are three agents" << "\n";
      std::exit(1);
    }
    Agent* a = s->agents[2];  // l.10
    RES attempt = PUSH(a, s->area, true);  // l.11
    if (attempt == RES::FAIL) {  // l.12

      s->agents.erase(s->agents.end() - 1);  // l.13, pop
      isTmpGoals[a->getId()] = false;
      P->assign(a->getTask());
      a->releaseTask();
      a->setGoal(goals[a->getId()]);
      pushers.push_back(a);

      s->phase = SWAPPHASE::GO_TARGET;

      // avoid collision
      // M.push_back(s->agents[0]);
      // M.push_back(s->agents[1]);

      return FIND_NEW_VERTEX(s);  // l.14

    } else if (attempt == RES::SUCCESS) {

      if (a->getNode() == a->getGoal()) {

        s->agents.erase(s->agents.end() - 1);  // l.17
        P->assign(a->getTask());
        a->releaseTask();
        a->setGoal(goals[a->getId()]);  // l.17
        isTmpGoals[a->getId()] = false;

        swaperToPusher.push_back(a);

        if (s->agents.size() == 2) {
          s->phase = SWAPPHASE::GO_TARGET;
          SWAP(s);
        } else {
          std::cout << "error@PPS::SWAP, "
                    << "the number of agents should be two" << "\n";
          std::exit(1);
        }
      } else {

        PUSH(s);  // l.18

      }
    }
  } else {
    std::cout << "error@PPS::SWAP, "
              << "the number of agents should be"
              << " less than four" << "\n";
    std::exit(1);
  }

  return RES::SUCCESS;  // l.19
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl 0x78(%rsi), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
ja 0x2e77b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x300f4
jmp 0x2e82b
movq 0x8(%r14), %rcx
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rcx), %rsi
movq %rsi, 0x10(%rsp)
movq 0x10(%r14), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
cmpq $0x3, %rsi
je 0x2e7e1
cmpq $0x2, %rsi
jne 0x2e960
movq 0x28(%r14), %rax
movq 0x8(%rdx), %rcx
cmpq (%rax), %rcx
jne 0x2e820
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30274
testb %al, %al
je 0x2e8aa
xorl %ebp, %ebp
cmpl $0x1, 0x78(%r14)
je 0x2e82d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x300f4
jmp 0x2e82d
cmpl $0x1, %eax
jne 0x2e98e
leaq 0x8(%r14), %r12
movq 0x10(%rcx), %r15
movq %r15, 0x8(%rsp)
leaq 0x60(%r14), %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2eaf6
cmpl $0x1, %eax
je 0x2e83c
testl %eax, %eax
jne 0x2e82b
movq 0x8(%r15), %rax
cmpq 0x10(%r15), %rax
je 0x2e8e3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f804
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rsi
addq $-0x8, %rsi
movq %r12, %rdi
callq 0x2d97c
leaq 0x160(%rbx), %rdi
movslq (%r15), %rsi
callq 0x315e8
notq %rdx
andq %rdx, (%rax)
movq 0x10(%rbx), %rdi
movq 0x18(%r15), %rsi
callq 0x1ed7a
movq %r15, %rdi
callq 0x15a0a
movslq (%r15), %rax
movq 0x148(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x10(%r15)
leaq 0x68(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x930e
andl $0x0, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30c22
movl %eax, %ebp
jmp 0x2e82d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30c22
testl %eax, %eax
je 0x2e950
addq $0xe0, %rbx
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x930e
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x930e
jmp 0x2e82b
movq 0x10(%r14), %rsi
addq $-0x8, %rsi
movq %r12, %rdi
callq 0x2d97c
movq 0x10(%rbx), %rdi
movq 0x18(%r15), %rsi
callq 0x1ed7a
movq %r15, %rdi
callq 0x15a0a
movslq (%r15), %rsi
movq 0x148(%rbx), %rax
movq (%rax,%rsi,8), %rax
movq %rax, 0x10(%r15)
leaq 0x160(%rbx), %rdi
callq 0x315e8
notq %rdx
andq %rdx, (%rax)
leaq 0xb0(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x930e
movq 0x10(%r14), %rax
subq 0x8(%r14), %rax
cmpq $0x10, %rax
jne 0x2e9b8
andl $0x0, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e74e
jmp 0x2e82b
movq 0x2b631(%rip), %rdi     # 0x59f98
leaq 0xed1a(%rip), %rsi      # 0x3d688
callq 0x63a0
leaq 0xed43(%rip), %rsi      # 0x3d6bd
movq %rax, %rdi
callq 0x63a0
leaq 0xed53(%rip), %rsi      # 0x3d6dc
movq %rax, %rdi
jmp 0x2e99c
movq 0x2b603(%rip), %rdi     # 0x59f98
leaq 0xecc4(%rip), %rsi      # 0x3d660
callq 0x63a0
leaq 0xbbbb(%rip), %rsi      # 0x3a563
movq %rax, %rdi
callq 0x63a0
pushq $0x1
popq %rdi
callq 0x6450
movq 0x2b5d9(%rip), %rdi     # 0x59f98
leaq 0xecc2(%rip), %rsi      # 0x3d688
callq 0x63a0
leaq 0xecc8(%rip), %rsi      # 0x3d69a
jmp 0x2e989