// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x78(%rax)
je 0x8eddc
movq -0x18(%rbp), %rax
cmpl $0x3, 0x78(%rax)
je 0x8eddc
movq -0x18(%rbp), %rax
cmpl $0x4, 0x78(%rax)
jne 0x8edf8
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x91e50
movl $0x0, -0x4(%rbp)
jmp 0x8f324
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x53720
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x1, %esi
callq 0x53720
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xda90
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x383b0
cmpq $0x2, %rax
jne 0x8ef0c
movq -0x20(%rbp), %rdi
callq 0x51cb0
cmpq -0x30(%rbp), %rax
je 0x8ee7c
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x90dc0
jmp 0x8ef07
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x920b0
testb $0x1, %al
jne 0x8ee92
jmp 0x8eeae
movq -0x18(%rbp), %rax
cmpl $0x1, 0x78(%rax)
je 0x8eeac
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x91e50
jmp 0x8ef05
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x933f0
cmpl $0x0, %eax
jne 0x8eed5
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8eda0
jmp 0x8ef03
movq -0xa8(%rbp), %rdi
addq $0xe0, %rdi
leaq -0x20(%rbp), %rsi
callq 0xdab0
movq -0xa8(%rbp), %rdi
addq $0xe0, %rdi
leaq -0x28(%rbp), %rsi
callq 0xdab0
jmp 0x8ef05
jmp 0x8ef07
jmp 0x8f31d
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x383b0
cmpq $0x3, %rax
jne 0x8f2d1
movq -0x18(%rbp), %rax
cmpl $0x1, 0x78(%rax)
je 0x8ef59
movq 0x72064(%rip), %rdi     # 0x100f98
leaq 0x27c87(%rip), %rsi     # 0xb6bc2
callq 0x84d0
movq %rax, %rdi
leaq 0x2466d(%rip), %rsi     # 0xb35b7
callq 0x84d0
movl $0x1, %edi
callq 0x85d0
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x2, %esi
callq 0x53720
movq -0xa8(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x60, %rdx
movl $0x1, %ecx
callq 0x8f5d0
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
jne 0x8f0e1
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x399f0
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x970c0
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x628b0
movq -0xd0(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x8bf70
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0x60(%rbp)
addq $0x160, %rax            # imm = 0x160
movq %rax, -0xc8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x63b50
movq -0xc8(%rbp), %rdi
movslq %eax, %rsi
callq 0x96f20
movq %rax, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x97070
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x51c90
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x57630
movq -0x38(%rbp), %rdi
callq 0x3a780
movq -0xa8(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, -0xb0(%rbp)
addq $0x148, %rax            # imm = 0x148
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x63b50
movq -0xb8(%rbp), %rdi
movslq %eax, %rsi
callq 0xda90
movq -0xb0(%rbp), %rdi
movq (%rax), %rsi
callq 0x51d10
movq -0xa8(%rbp), %rdi
addq $0x68, %rdi
leaq -0x38(%rbp), %rsi
callq 0xdab0
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl $0x0, 0x78(%rax)
movq -0x18(%rbp), %rsi
callq 0x933f0
movl %eax, -0x4(%rbp)
jmp 0x8f324
cmpl $0x0, -0x3c(%rbp)
jne 0x8f2cd
movq -0x38(%rbp), %rdi
callq 0x51cb0
movq %rax, -0xd8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x56820
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jne 0x8f2bb
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x399f0
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0x970c0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x628b0
movq -0x100(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x8bf70
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0x90(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x51c90
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
callq 0x57630
movq -0x38(%rbp), %rdi
callq 0x3a780
movq -0xa8(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, -0xe8(%rbp)
addq $0x148, %rax            # imm = 0x148
movq %rax, -0xf0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x63b50
movq -0xf0(%rbp), %rdi
movslq %eax, %rsi
callq 0xda90
movq -0xe8(%rbp), %rdi
movq (%rax), %rsi
callq 0x51d10
movq -0xa8(%rbp), %rax
addq $0x160, %rax            # imm = 0x160
movq %rax, -0xe0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x63b50
movq -0xe0(%rbp), %rdi
movslq %eax, %rsi
callq 0x96f20
movq %rax, -0xa0(%rbp)
movq %rdx, -0x98(%rbp)
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
callq 0x97070
movq -0xa8(%rbp), %rdi
addq $0xb0, %rdi
leaq -0x38(%rbp), %rsi
callq 0xdab0
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x383b0
cmpq $0x2, %rax
jne 0x8f27e
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl $0x0, 0x78(%rax)
movq -0x18(%rbp), %rsi
callq 0x8eda0
jmp 0x8f2b9
movq 0x71d13(%rip), %rdi     # 0x100f98
leaq 0x2795e(%rip), %rsi     # 0xb6bea
callq 0x84d0
movq %rax, %rdi
leaq 0x27961(%rip), %rsi     # 0xb6bfc
callq 0x84d0
movq %rax, %rdi
leaq 0x2430d(%rip), %rsi     # 0xb35b7
callq 0x84d0
movl $0x1, %edi
callq 0x85d0
jmp 0x8f2cb
movq -0xa8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x90dc0
jmp 0x8f2cd
jmp 0x8f2cf
jmp 0x8f31b
movq 0x71cc0(%rip), %rdi     # 0x100f98
leaq 0x2790b(%rip), %rsi     # 0xb6bea
callq 0x84d0
movq %rax, %rdi
leaq 0x27931(%rip), %rsi     # 0xb6c1f
callq 0x84d0
movq %rax, %rdi
leaq 0x27941(%rip), %rsi     # 0xb6c3e
callq 0x84d0
movq %rax, %rdi
leaq 0x242ab(%rip), %rsi     # 0xb35b7
callq 0x84d0
movl $0x1, %edi
callq 0x85d0
jmp 0x8f31d
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq