// File: IncredibleHannes[P]QMUL-ChessGame/src/Model/Board.cpp
// Function: Board::undoLastMove()
void Board::undoLastMove() {
  if (previousMoves.size() == 0) {
    return;
  }
  Move *m = previousMoves.back();
  previousMoves.pop_back();

  Position origin = m->getOrigin();
  Position target = m->getTarget();
  Chessman *currentChessman = this->board[target.getX()][target.getY()];
  if (m->hasType(Move::Promotion)) {
    delete currentChessman;
    currentChessman = capturedChessmen.back();
    capturedChessmen.pop_back();
  }

  if (m->hasType(Move::Capture)) {
    this->board[target.getX()][target.getY()] = m->getCapturedChessman()->clone();
    this->board[target.getX()][target.getY()]->unsetCapture();
  } else {
    this->board[target.getX()][target.getY()] = nullptr;
  }
  this->board[origin.getX()][origin.getY()] = currentChessman;
  currentChessman->setCurrentPosition(origin);

  if (m->hasType(Move::Casteling)) {
    this->changeCurrentColour();
    undoLastMove();
  }
  this->changeCurrentColour();
  delete m;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x38(%rbp)
addq $0x28, %rdi
callq 0x9190
cmpq $0x0, %rax
jne 0x869b
jmp 0x8920
movq -0x38(%rbp), %rdi
addq $0x28, %rdi
callq 0x97a0
movq -0x38(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x10(%rbp)
addq $0x28, %rdi
callq 0x9820
movq -0x10(%rbp), %rdi
callq 0x6a10
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6a30
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq (%rax), %rax
movq %rax, -0x48(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ee0
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ef0
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl $0x4, %esi
callq 0x6a70
testb $0x1, %al
jne 0x872c
jmp 0x8765
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x8744
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x38(%rbp), %rdi
addq $0x10, %rdi
callq 0x9860
movq -0x38(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x28(%rbp)
addq $0x10, %rdi
callq 0x98a0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x6a70
testb $0x1, %al
jne 0x877c
jmp 0x8815
movq -0x10(%rbp), %rdi
callq 0x69b0
movq %rax, %rdi
movq (%rdi), %rax
callq *(%rax)
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x70(%rbp)
movq (%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ee0
movl %eax, %ecx
movq -0x78(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ef0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rcx
movl %eax, %edx
movq -0x38(%rbp), %rax
movslq %edx, %rdx
movq %rsi, (%rcx,%rdx,8)
movq (%rax), %rax
movq %rax, -0x60(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ee0
movl %eax, %ecx
movq -0x60(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ef0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x57f0
jmp 0x885a
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ee0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2ef0
movl %eax, %ecx
movq -0x80(%rbp), %rax
movslq %ecx, %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x2ee0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x2ef0
movq -0x98(%rbp), %rdx
movl %eax, %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
callq 0x5930
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x6a70
testb $0x1, %al
jne 0x88dc
jmp 0x88ee
movq -0x38(%rbp), %rdi
callq 0x8630
movq -0x38(%rbp), %rdi
callq 0x8670
movq -0x38(%rbp), %rdi
callq 0x8630
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x8920
movq -0xa8(%rbp), %rdi
callq 0x69d0
movq -0xa8(%rbp), %rdi
callq 0x2140
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)