// File: /workspace/github_cpp_makefile_star2_O1/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 %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movl 0x78(%rsi), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
ja 0x39a1b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3bcd0
jmp 0x39ccf
movq 0x8(%r14), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rcx), %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%r14), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
cmpq $0x3, %rsi
je 0x39a6c
cmpq $0x2, %rsi
jne 0x39d1a
movq 0x28(%r14), %rax
movq 0x8(%rdx), %rcx
cmpq (%rax), %rcx
je 0x39b73
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b14c
jmp 0x39ccf
cmpl $0x1, %eax
jne 0x39d48
movq 0x10(%rcx), %rsi
movq %rsi, (%rsp)
leaq 0x60(%r14), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0x39f0e
movl %eax, %ebp
cmpl $0x1, %eax
jne 0x39b49
movb $0x1, %al
testb %al, %al
jne 0x39ab2
movq 0x10(%r14), %rsi
leaq -0x8(%rsi), %rdi
movq %rsi, %rdx
subq %rsi, %rdx
callq 0x65b0
addq $-0x8, 0x10(%r14)
movq (%rsp), %rax
movslq (%rax), %rcx
leaq 0x3f(%rcx), %rdx
testq %rcx, %rcx
cmovnsq %rcx, %rdx
sarq $0x6, %rdx
shlq $0x3, %rdx
addq 0x160(%rbx), %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
leaq 0x3f(%rsi), %rdi
andq %rcx, %rdi
xorl %r8d, %r8d
cmpq %rsi, %rdi
setbe %r8b
movq $-0x2, %rsi
rolq %cl, %rsi
andq %rsi, -0x8(%rdx,%r8,8)
movq 0x10(%rbx), %rdi
movq 0x18(%rax), %rsi
callq 0x25982
movq (%rsp), %rdi
callq 0x1934a
movq (%rsp), %rax
movslq (%rax), %rcx
movq 0x148(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x10(%rax)
movq 0x70(%rbx), %rsi
cmpq 0x78(%rbx), %rsi
je 0x39bd1
movq %rax, (%rsi)
addq $0x8, 0x70(%rbx)
jmp 0x39bdd
testl %ebp, %ebp
jne 0x39cca
movq (%rsp), %rax
movq 0x8(%rax), %rcx
cmpq 0x10(%rax), %rcx
je 0x39c05
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b14c
jmp 0x39cca
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3bed0
testb %al, %al
je 0x39b92
cmpl $0x1, 0x78(%r14)
jne 0x39a0b
jmp 0x39ccf
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cc6a
testl %eax, %eax
je 0x39bf5
leaq 0xe0(%rbx), %r14
movq 0xe8(%rbx), %rsi
cmpq 0xf0(%rbx), %rsi
je 0x39cdc
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0xe8(%rbx)
jmp 0x39ce9
leaq 0x68(%rbx), %rdi
movq %rsp, %rdx
callq 0x18edc
movl $0x0, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cc6a
jmp 0x39cca
movq %rbx, %rdi
movq %r14, %rsi
callq 0x399f0
jmp 0x39ccf
movb $0x1, %al
testb %al, %al
jne 0x39c1e
movq 0x10(%r14), %rsi
leaq -0x8(%rsi), %rdi
movq %rsi, %rdx
subq %rsi, %rdx
callq 0x65b0
addq $-0x8, 0x10(%r14)
movq 0x10(%rbx), %rdi
movq %rsp, %r15
movq (%r15), %rax
movq 0x18(%rax), %rsi
callq 0x25982
movq (%r15), %rdi
callq 0x1934a
movq (%r15), %rax
movslq (%rax), %rcx
movq 0x148(%rbx), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x10(%rax)
leaq 0x3f(%rcx), %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq 0x160(%rbx), %rax
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
leaq 0x3f(%rdx), %rsi
andq %rcx, %rsi
xorl %edi, %edi
cmpq %rdx, %rsi
setbe %dil
movq $-0x2, %rdx
rolq %cl, %rdx
andq %rdx, -0x8(%rax,%rdi,8)
leaq 0xb0(%rbx), %rdi
movq %r15, %rsi
callq 0x3d92c
movq 0x10(%r14), %rax
subq 0x8(%r14), %rax
cmpq $0x10, %rax
jne 0x39d74
movl $0x0, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x399f0
cmpl $0x1, %ebp
je 0x39cd1
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x18edc
movq 0xe8(%rbx), %rsi
cmpq 0xf0(%rbx), %rsi
je 0x39d0b
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0xe8(%rbx)
jmp 0x39ccf
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x18edc
jmp 0x39ccf
movq 0x28287(%rip), %rdi     # 0x61fa8
leaq 0x11788(%rip), %rsi     # 0x4b4b0
callq 0x6360
leaq 0x117b1(%rip), %rsi     # 0x4b4e5
movq %rax, %rdi
callq 0x6360
leaq 0x117c1(%rip), %rsi     # 0x4b504
movq %rax, %rdi
jmp 0x39d56
movq 0x28259(%rip), %rdi     # 0x61fa8
leaq 0x11732(%rip), %rsi     # 0x4b488
callq 0x6360
leaq 0xe801(%rip), %rsi      # 0x48563
movq %rax, %rdi
callq 0x6360
movl $0x1, %edi
callq 0x6440
movq 0x2822d(%rip), %rdi     # 0x61fa8
leaq 0x1172e(%rip), %rsi     # 0x4b4b0
callq 0x6360
leaq 0x11734(%rip), %rsi     # 0x4b4c2
jmp 0x39d43