// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
pushq $-0x1
popq %r15
cmpl $0x3f, %esi
jg 0x3a7c
movl %esi, %r13d
movq %rdi, %rbx
movslq %esi, %r14
pushq $-0x2
popq %r15
cmpb $0x0, 0x208(%rdi,%r14)
je 0x3a7c
movl %ecx, %r12d
movl %edx, -0x2c(%rbp)
movq 0x8(%rbx,%r14,8), %rdi
callq 0x569e
movzbl %al, %eax
cmpl %r12d, %eax
jne 0x3a7c
leaq 0x208(%rbx), %rax
movslq -0x2c(%rbp), %r15
movq %rax, -0x40(%rbp)
movb (%rax,%r15), %al
cmpb $0x1, %al
movq %r15, -0x38(%rbp)
jne 0x38f5
movq 0x8(%rbx,%r15,8), %rdi
callq 0x569e
movl %eax, %r15d
movq 0x8(%rbx,%r14,8), %rdi
callq 0x569e
cmpb %al, %r15b
je 0x3965
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %r15
movb (%rax,%r15), %al
movb %al, -0x2d(%rbp)
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5694
cmpb $0x70, %al
jne 0x391e
movl -0x2c(%rbp), %edi
callq 0x356a
movl %eax, %r15d
movl %r13d, %edi
callq 0x356a
cmpl %eax, %r15d
jne 0x396c
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rdx
movq 0x8(%rbx,%r14,8), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
pushq $-0x1
popq %r15
testb %al, %al
je 0x3a7c
cmpb $0x0, -0x2d(%rbp)
je 0x3983
movq -0x38(%rbp), %r15
movq 0x8(%rbx,%r15,8), %rdi
testq %rdi, %rdi
je 0x3956
movq (%rdi), %rax
callq *0x20(%rax)
orl $-0x1, 0x27c(%rbx)
movl $0xc8, %r13d
jmp 0x398a
pushq $-0x1
jmp 0x3a7a
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rdx
cmpb $0x0, (%rdx,%rax)
je 0x3a8e
movl -0x2c(%rbp), %esi
jmp 0x3925
xorl %r13d, %r13d
movq -0x38(%rbp), %r15
movb $0x0, 0x208(%rbx,%r14)
movb $0x1, 0x208(%rbx,%r15)
movq 0x8(%rbx,%r14,8), %rdi
movq %rdi, 0x8(%rbx,%r15,8)
callq 0x5694
cmpb $0x70, %al
jne 0x3a12
movl -0x2c(%rbp), %edi
callq 0x3558
cmpl $0x7, %eax
je 0x39c8
movl -0x2c(%rbp), %edi
callq 0x3558
testl %eax, %eax
jne 0x3a12
movq 0x8(%rbx,%r15,8), %rdi
callq 0x569e
movq %r15, %rcx
movl %eax, %r15d
movq 0x8(%rbx,%rcx,8), %rdi
testq %rdi, %rdi
je 0x39e8
movq (%rdi), %rax
callq *0x20(%rax)
pushq $0x18
popq %rdi
callq 0x2100
movq %rax, %r12
movzbl %r15b, %edx
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x56d4
movq -0x38(%rbp), %r15
movq %r12, 0x8(%rbx,%r15,8)
orl $-0x1, 0x27c(%rbx)
movq %r15, %r14
leaq 0x248(%rbx), %r15
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5694
leaq -0x2e(%rbp), %rsi
movb %al, (%rsi)
movq %r15, %rdi
callq 0x4d3e
leaq 0x260(%rbx), %rdi
addl -0x2c(%rbp), %r13d
leaq -0x44(%rbp), %rsi
movl %r13d, (%rsi)
callq 0x4e1e
incl 0x278(%rbx)
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5694
cmpb $0x70, %al
jne 0x3a6a
orl $-0x1, 0x27c(%rbx)
xorl %eax, %eax
jmp 0x3a72
movl 0x27c(%rbx), %eax
incl %eax
movl %eax, 0x27c(%rbx)
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x2c(%rbp), %esi
movl %esi, %ecx
subl %r12d, %ecx
subl %r12d, %ecx
incl %ecx
movslq %ecx, %r13
cmpb $0x1, (%rdx,%r13)
jne 0x3925
movq 0x8(%rbx,%r13,8), %rdi
callq 0x56b8
xorl %r15d, %r15d
testl %eax, %eax
je 0x3a7c
movq 0x8(%rbx,%r13,8), %rdi
callq 0x569e
movzbl %al, %eax
cmpl %r12d, %eax
je 0x3a7c
movq -0x38(%rbp), %rcx
movb $0x1, 0x208(%rbx,%rcx)
movq 0x8(%rbx,%r13,8), %rax
movq %rax, 0x8(%rbx,%rcx,8)
movb $0x0, 0x208(%rbx,%r13)
movb $0x1, -0x2d(%rbp)
jmp 0x391e
movq %rax, %rbx
movq %r12, %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x21b0
nop