// File: tlaloc1313[P]chess-project/Board.cpp
// Function: Board::movePiece(int, int, int)
int Board::movePiece(int startSpace, int endSpace, int whiteTurn){
  int pieceTaken;

  if (startSpace > 63){
    return -1;
  }
  //Check that a piece exists at StartSpace
  if (activeArray[startSpace] == 0){
    return -2;
  }
  //Allows white to only move white pieces, and black to only move black pieces.
  if (whiteTurn != pieceArray[startSpace]->getIsWhite()){
    return -2;
  }
  //It is illegal and illogical to take your own pieces:
  if ((activeArray[endSpace])) {
    if ((pieceArray[endSpace]->getIsWhite() == pieceArray[startSpace]->getIsWhite())){
      return -1;
    }
  }

  int spaceWasOccupied = activeArray[endSpace]; //Checks the endSpace

  //Checking for the en passant rule:
  if ( (pieceArray[startSpace]->getType() == 'p')
      && (col(endSpace) != col(startSpace))
      && (activeArray[endSpace] == 0)
      && (activeArray[endSpace+1-(2*whiteTurn)]) ){
    if (pieceArray[endSpace+1-(2*whiteTurn)]->checkEnPassant()
      && (pieceArray[endSpace+1-(2*whiteTurn)]->getIsWhite() != whiteTurn)){
      //Pushes the pawn back to be taken, and continues as usual
      activeArray[endSpace] = 1;
      pieceArray[endSpace] = pieceArray[endSpace+1-(2*whiteTurn)];
      activeArray[endSpace+1-(2*whiteTurn)] = 0;
      spaceWasOccupied = 1;
    } else {
      return 0;
    }
  }

  bool success = pieceArray[startSpace]->move(endSpace, activeArray);
  if (success == 1){
    pieceTaken = 0;
    if (spaceWasOccupied == 1){//If a piece is being taken, deletes that piece and resets 50 moves
      delete pieceArray[endSpace];
      pieceTaken = 1;
      movesSince = -1;
    }
    activeArray[startSpace] = 0; //Clears the startSpace since the piece is moving
    activeArray[endSpace] = 1; //Marks the endSpace as taken
    pieceArray[endSpace] = pieceArray[startSpace]; //Copies the pointer to the piece within the array

    //PROMOTING A PAWN
    if (pieceArray[endSpace]->getType()=='p' && (row(endSpace)==7 || row(endSpace)==0)){
      bool pawnWasWhite = pieceArray[endSpace]->getIsWhite();
      delete pieceArray[endSpace];
      pieceArray[endSpace] = new Queen(endSpace, pawnWasWhite);
      movesSince = -1;
    }

    //If a piece is being moved, adds the move to the history arrays.
    pastPieces.push_back(pieceArray[endSpace]->getType());
    pastMoves.push_back(endSpace+(200*pieceTaken));
    moveNumber++;

    //if needed, resets the 50 move rule.
    if (pieceArray[endSpace]->getType() == 'p') {
      movesSince = -1;
    }

    movesSince++;
    return 1;
  }
  //If the move was illegal
  return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpl $0x3f, -0x14(%rbp)
jle 0x4de2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51f1
movq -0x48(%rbp), %rax
movslq -0x14(%rbp), %rcx
movb 0x208(%rax,%rcx), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4e07
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x51f1
movq -0x48(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, -0x4c(%rbp)
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
movb %al, %cl
movl -0x4c(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x4e3a
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x51f1
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
je 0x4e98
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
movb %al, %cl
movq -0x48(%rbp), %rax
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x50(%rbp)
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
movb %al, %cl
movl -0x50(%rbp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x4e96
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x51f1
jmp 0x4e98
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb 0x208(%rax,%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, -0x24(%rbp)
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x4fd1
movl -0x18(%rbp), %edi
callq 0x4810
movl %eax, -0x54(%rbp)
movl -0x14(%rbp), %edi
callq 0x4810
movl %eax, %ecx
movl -0x54(%rbp), %eax
cmpl %ecx, %eax
je 0x4fd1
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb 0x208(%rax,%rcx), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4fd1
movq -0x48(%rbp), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %edx
shll %edx
subl %edx, %ecx
movslq %ecx, %rcx
testb $0x1, 0x208(%rax,%rcx)
je 0x4fd1
movq -0x48(%rbp), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %edx
shll %edx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98f0
cmpl $0x0, %eax
je 0x4fc3
movq -0x48(%rbp), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %edx
shll %edx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
andb $0x1, %al
movzbl %al, %eax
cmpl -0x1c(%rbp), %eax
je 0x4fc3
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb $0x1, 0x208(%rax,%rcx)
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %edx
shll %edx
subl %edx, %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx,8), %rdx
movslq -0x18(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %edx
shll %edx
subl %edx, %ecx
movslq %ecx, %rcx
movb $0x0, 0x208(%rax,%rcx)
movl $0x1, -0x24(%rbp)
jmp 0x4fcf
movl $0x0, -0x4(%rbp)
jmp 0x51f1
jmp 0x4fd1
movq -0x48(%rbp), %rdx
movslq -0x14(%rbp), %rax
movq 0x8(%rdx,%rax,8), %rdi
movl -0x18(%rbp), %esi
addq $0x208, %rdx            # imm = 0x208
movq (%rdi), %rax
callq *0x8(%rax)
andb $0x1, %al
movb %al, -0x25(%rbp)
movb -0x25(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x51ea
movl $0x0, -0x20(%rbp)
cmpl $0x1, -0x24(%rbp)
jne 0x5047
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x5032
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movq -0x48(%rbp), %rax
movl $0x1, -0x20(%rbp)
movl $0xffffffff, 0x27c(%rax) # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rax
movslq -0x14(%rbp), %rcx
movb $0x0, 0x208(%rax,%rcx)
movslq -0x18(%rbp), %rcx
movb $0x1, 0x208(%rax,%rcx)
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdx
movslq -0x18(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x5144
movl -0x18(%rbp), %edi
callq 0x47f0
cmpl $0x7, %eax
je 0x50ad
movl -0x18(%rbp), %edi
callq 0x47f0
cmpl $0x0, %eax
jne 0x5144
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
movb %al, %cl
movq -0x48(%rbp), %rax
andb $0x1, %cl
movb %cl, -0x26(%rbp)
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x50e8
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movl $0x18, %edi
callq 0x2150
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movl -0x18(%rbp), %esi
movzbl -0x26(%rbp), %edx
andl $0x1, %edx
callq 0x9940
jmp 0x510d
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rdx
movslq -0x18(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movl $0xffffffff, 0x27c(%rax) # imm = 0xFFFFFFFF
jmp 0x5144
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2130
jmp 0x51fd
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x248, %rcx            # imm = 0x248
movq %rcx, -0x78(%rbp)
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movq -0x78(%rbp), %rdi
movb %al, -0x35(%rbp)
leaq -0x35(%rbp), %rsi
callq 0x6c30
movq -0x48(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
movl -0x18(%rbp), %eax
imull $0xc8, -0x20(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x6c60
movq -0x48(%rbp), %rax
movl 0x278(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x278(%rax)
movslq -0x18(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x51ce
movq -0x48(%rbp), %rax
movl $0xffffffff, 0x27c(%rax) # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rax
movl 0x27c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x27c(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x51f1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x2270
nopw %cs:(%rax,%rax)