// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
cmpl $0x3f, %esi
jle 0x3da9
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4002
movl %esi, %r13d
movq %rdi, %rbx
movl %edx, -0x30(%rbp)
movslq %esi, %r14
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpb $0x0, 0x208(%rdi,%r14)
je 0x4002
movl %ecx, %r12d
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c0a
movzbl %al, %eax
cmpl %r12d, %eax
jne 0x4002
movq %r14, -0x58(%rbp)
leaq 0x208(%rbx), %rax
movl -0x30(%rbp), %r15d
movslq %r15d, %r14
movq %rax, -0x48(%rbp)
cmpb $0x1, (%rax,%r14)
jne 0x3e28
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c0a
movl %eax, %r15d
movq -0x58(%rbp), %rax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x5c0a
cmpb %al, %r15b
movl -0x30(%rbp), %r15d
je 0x3d9e
movq %r14, -0x40(%rbp)
movb 0x208(%rbx,%r14), %al
movb %al, -0x29(%rbp)
movq -0x58(%rbp), %r14
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c00
cmpb $0x70, %al
jne 0x3e65
movl %r15d, %edi
callq 0x3a29
movl %eax, %r15d
movl %r13d, %edi
callq 0x3a29
cmpl %eax, %r15d
jne 0x3eb5
movl -0x30(%rbp), %r15d
movq -0x48(%rbp), %rdx
movq 0x8(%rbx,%r14,8), %rdi
movq (%rdi), %rax
movl %r15d, %esi
callq *0x8(%rax)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb %al, %al
je 0x4002
cmpb $0x0, -0x29(%rbp)
je 0x3ecd
movq -0x40(%rbp), %r12
movq 0x8(%rbx,%r12,8), %rdi
testq %rdi, %rdi
movl -0x30(%rbp), %r15d
je 0x3ea3
movq (%rdi), %rax
callq *0x20(%rax)
movl $0xffffffff, 0x27c(%rbx) # imm = 0xFFFFFFFF
movl $0xc8, %r13d
jmp 0x3ed8
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rdx
cmpb $0x0, (%rdx,%rax)
je 0x4014
movl -0x30(%rbp), %r15d
jmp 0x3e69
xorl %r13d, %r13d
movl -0x30(%rbp), %r15d
movq -0x40(%rbp), %r12
movb $0x0, 0x208(%rbx,%r14)
movb $0x1, 0x208(%rbx,%r12)
movq 0x8(%rbx,%r14,8), %rdi
movq %rdi, 0x8(%rbx,%r12,8)
callq 0x5c00
cmpb $0x70, %al
jne 0x3f69
movl %r15d, %edi
callq 0x3a14
cmpl $0x7, %eax
je 0x3f16
movl %r15d, %edi
callq 0x3a14
testl %eax, %eax
jne 0x3f69
movq 0x8(%rbx,%r12,8), %rdi
callq 0x5c0a
movl %eax, %r15d
movq 0x8(%rbx,%r12,8), %rdi
testq %rdi, %rdi
je 0x3f33
movq (%rdi), %rax
callq *0x20(%rax)
movl $0x18, %edi
callq 0x2110
movq %rax, %r12
movzbl %r15b, %edx
movq %rax, %rdi
movl -0x30(%rbp), %r15d
movl %r15d, %esi
callq 0x5c40
movq -0x40(%rbp), %rax
movq %r12, 0x8(%rbx,%rax,8)
movq %rax, %r12
movl $0xffffffff, 0x27c(%rbx) # imm = 0xFFFFFFFF
movq 0x8(%rbx,%r12,8), %rdi
callq 0x5c00
movb %al, -0x31(%rbp)
movq 0x250(%rbx), %rsi
cmpq 0x258(%rbx), %rsi
je 0x3f91
movb %al, (%rsi)
incq 0x250(%rbx)
jmp 0x3fa1
leaq 0x248(%rbx), %rdi
leaq -0x31(%rbp), %rdx
callq 0x52a0
addl %r15d, %r13d
movl %r13d, -0x4c(%rbp)
movq 0x268(%rbx), %rsi
cmpq 0x270(%rbx), %rsi
je 0x3fc8
movl %r13d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x268(%rbx)
jmp 0x3fd8
leaq 0x260(%rbx), %rdi
leaq -0x4c(%rbp), %rdx
callq 0x5392
incl 0x278(%rbx)
movq 0x8(%rbx,%r12,8), %rdi
callq 0x5c00
cmpb $0x70, %al
jne 0x3ff6
movl $0xffffffff, 0x27c(%rbx) # imm = 0xFFFFFFFF
incl 0x27c(%rbx)
movl $0x1, %r15d
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
subl %r12d, %ecx
subl %r12d, %ecx
incl %ecx
movslq %ecx, %r13
cmpb $0x1, (%rdx,%r13)
jne 0x3e69
movq 0x8(%rbx,%r13,8), %rdi
callq 0x5c24
xorl %r15d, %r15d
testl %eax, %eax
je 0x4002
movq 0x8(%rbx,%r13,8), %rdi
callq 0x5c0a
movzbl %al, %eax
cmpl %r12d, %eax
je 0x4002
movq -0x40(%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, -0x29(%rbp)
jmp 0x3e61
movq %rax, %rbx
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x21c0
nop