// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x3f, %esi
jle 0x3db5
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4007
movl %esi, %r13d
movq %rdi, %rbx
movl %edx, -0x30(%rbp)
movslq %esi, %r14
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpb $0x0, 0x208(%rdi,%r14)
je 0x4007
movl %ecx, %r12d
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c52
movzbl %al, %eax
cmpl %r12d, %eax
jne 0x4007
leaq 0x208(%rbx), %rax
movl -0x30(%rbp), %ecx
movslq %ecx, %r15
movq %rax, -0x48(%rbp)
movb (%rax,%r15), %al
cmpb $0x1, %al
jne 0x3e2c
movq 0x8(%rbx,%r15,8), %rdi
callq 0x5c52
movb %al, -0x40(%rbp)
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c52
cmpb %al, -0x40(%rbp)
je 0x3daa
movq -0x48(%rbp), %rax
movb (%rax,%r15), %al
movb %al, -0x29(%rbp)
movq %r15, -0x40(%rbp)
movq 0x8(%rbx,%r14,8), %rdi
callq 0x5c48
cmpb $0x70, %al
jne 0x3e62
movl -0x30(%rbp), %edi
callq 0x3a35
movl %eax, %r15d
movl %r13d, %edi
callq 0x3a35
cmpl %eax, %r15d
jne 0x3e68
movl -0x30(%rbp), %esi
movq -0x48(%rbp), %rdx
jmp 0x3e7d
movq -0x48(%rbp), %rdx
jmp 0x3e7a
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rdx
cmpb $0x0, (%rdx,%rax)
je 0x4019
movl -0x30(%rbp), %esi
movq 0x8(%rbx,%r14,8), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb %al, %al
je 0x4007
cmpb $0x0, -0x29(%rbp)
je 0x3ec6
movq -0x40(%rbp), %r12
movq 0x8(%rbx,%r12,8), %rdi
testq %rdi, %rdi
movl -0x30(%rbp), %r15d
je 0x3eb4
movq (%rdi), %rax
callq *0x20(%rax)
movl $0xffffffff, 0x27c(%rbx) # imm = 0xFFFFFFFF
movl $0xc8, %r13d
jmp 0x3ed1
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 0x5c48
cmpb $0x70, %al
jne 0x3f62
movl %r15d, %edi
callq 0x3a20
cmpl $0x7, %eax
je 0x3f0f
movl %r15d, %edi
callq 0x3a20
testl %eax, %eax
jne 0x3f62
movq 0x8(%rbx,%r12,8), %rdi
callq 0x5c52
movl %eax, %r15d
movq 0x8(%rbx,%r12,8), %rdi
testq %rdi, %rdi
je 0x3f2c
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 0x5c88
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 0x5c48
movb %al, -0x31(%rbp)
movq 0x250(%rbx), %rsi
cmpq 0x258(%rbx), %rsi
je 0x3f8a
movb %al, (%rsi)
incq 0x250(%rbx)
jmp 0x3f9a
leaq 0x248(%rbx), %rdi
leaq -0x31(%rbp), %rdx
callq 0x52f6
addl %r15d, %r13d
movl %r13d, -0x4c(%rbp)
movq 0x268(%rbx), %rsi
cmpq 0x270(%rbx), %rsi
je 0x3fc1
movl %r13d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x268(%rbx)
jmp 0x3fd1
leaq 0x260(%rbx), %rdi
leaq -0x4c(%rbp), %rdx
callq 0x53e8
incl 0x278(%rbx)
movq 0x8(%rbx,%r12,8), %rdi
callq 0x5c48
cmpb $0x70, %al
jne 0x3ff3
movl $0xffffffff, 0x27c(%rbx) # imm = 0xFFFFFFFF
xorl %eax, %eax
jmp 0x3ffb
movl 0x27c(%rbx), %eax
incl %eax
movl %eax, 0x27c(%rbx)
movl $0x1, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x30(%rbp), %esi
movl %esi, %ecx
subl %r12d, %ecx
subl %r12d, %ecx
incl %ecx
movslq %ecx, %r13
cmpb $0x1, (%rdx,%r13)
jne 0x3e7d
movq 0x8(%rbx,%r13,8), %rdi
callq 0x5c6c
xorl %r15d, %r15d
testl %eax, %eax
je 0x4007
movq 0x8(%rbx,%r13,8), %rdi
callq 0x5c52
movzbl %al, %eax
cmpl %r12d, %eax
je 0x4007
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 0x3e59
movq %rax, %rbx
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x21c0