// File: tlaloc1313[P]chess-project/Board.cpp
// Function: Board::castle(int, int)
int Board::castle (int castleColour, int castleKingSide){
  int rook, king, numSquares, s1, s2, s3, checkTest[3];
  //Sets up the correct squares to check, and checksChecks
  if (castleColour){
      king = 32;
    if (castleKingSide){// White kingside
      rook = 56;
      numSquares = 2;
      s1 = 40;
      s2 = 48;
    } else { // White Queenside
      rook = 0;
      numSquares = 3;
      s1 = 24;
      s2 = 16;
      s3 = 8;
    }
  } else {
      king = 39;
    if (castleKingSide) { //Black kingside
      rook = 63;
      numSquares = 2;
      s1 = 47;
      s2 = 55;
    } else { //Black queenside
      rook = 7;
      numSquares = 3;
      s1 = 31;
      s2 = 23;
      s3 = 15;
    }
  }
  //If pieces are not in place, fails.
  if (!(activeArray[king] && activeArray[rook])){
    return 0;
  }
  //If pieces are in the way, fails.
  if (numSquares == 2) {
    if (activeArray[s1] || activeArray[s2]){
      return 0;
    }
  } else {
    if (activeArray[s1] || activeArray[s2] || activeArray[s3]){
      return 0;
    }
  }
  //Check pieces aren't the wrong colour or type
  if (!(pieceArray[king]->getIsWhite()==castleColour && pieceArray[rook]->getIsWhite()==castleColour && pieceArray[king]->getType()=='k' && pieceArray[rook]->getType()=='r' )){
    return 0;
  }
  //If pieces have moved, casling fails
  if (pieceArray[king]->getHasMoved() || pieceArray[rook]->getHasMoved()){
    return 0;
  }
  //Checks that the king isn't moving into, through, or out of check.
  if (castleColour) {
    checkTest[0] = checkCheck(king, -1);
    checkTest[1] = checkCheck(s1, -1);
    checkTest[2] = checkCheck(s2, -1);
    for (int i = 0; i < 3; i++) {
      if ((checkTest[i] == 1) || (checkTest[i] == 3)) {
        return -5;
      }
    }
  } else {
    checkTest[0] = checkCheck(-1, king);
    checkTest[1] = checkCheck(-1, s1);
    checkTest[2] = checkCheck(-1, s2);
    for (int i = 0; i < 3; i++) {
      if ((checkTest[i] == 2) || (checkTest[i] == 3)) {
        return -5;
      }
    }
  }
  //Attempts to castle
	pieceArray[king]->castle(s2);
	pieceArray[rook]->castle(s1);
  activeArray[king] = 0; //Clears the startSpace since the piece is moving
  activeArray[rook] = 0;
  activeArray[s2] = 1; //Marks the endSpace as taken
  activeArray[s1] = 1;
  pieceArray[s1] = pieceArray[rook]; //Copies the pointer to the piece within the array
  pieceArray[s2] = pieceArray[king];

  //Adds move to history arrays
  pastPieces.push_back('x');
  if (castleKingSide){
    pastMoves.push_back(69);
  } else {
    pastMoves.push_back(70);
  }
  movesSince++;
  moveNumber++;
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x64f0
movl $0x20, -0x20(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x64cb
movl $0x38, -0x1c(%rbp)
movl $0x2, -0x24(%rbp)
movl $0x28, -0x28(%rbp)
movl $0x30, -0x2c(%rbp)
jmp 0x64ee
movl $0x0, -0x1c(%rbp)
movl $0x3, -0x24(%rbp)
movl $0x18, -0x28(%rbp)
movl $0x10, -0x2c(%rbp)
movl $0x8, -0x30(%rbp)
jmp 0x6540
movl $0x27, -0x20(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x651b
movl $0x3f, -0x1c(%rbp)
movl $0x2, -0x24(%rbp)
movl $0x2f, -0x28(%rbp)
movl $0x37, -0x2c(%rbp)
jmp 0x653e
movl $0x7, -0x1c(%rbp)
movl $0x3, -0x24(%rbp)
movl $0x1f, -0x28(%rbp)
movl $0x17, -0x2c(%rbp)
movl $0xf, -0x30(%rbp)
jmp 0x6540
movq -0x58(%rbp), %rax
movslq -0x20(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
je 0x6564
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
jne 0x6570
movl $0x0, -0x4(%rbp)
jmp 0x6897
cmpl $0x2, -0x24(%rbp)
jne 0x65a8
movq -0x58(%rbp), %rax
movslq -0x28(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
jne 0x659a
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
je 0x65a6
movl $0x0, -0x4(%rbp)
jmp 0x6897
jmp 0x65ec
movq -0x58(%rbp), %rax
movslq -0x28(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
jne 0x65de
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
jne 0x65de
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
testb $0x1, 0x208(%rax,%rcx)
je 0x65ea
movl $0x0, -0x4(%rbp)
jmp 0x6897
jmp 0x65ec
movq -0x58(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
andb $0x1, %al
movzbl %al, %eax
cmpl -0x14(%rbp), %eax
jne 0x6658
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98a0
andb $0x1, %al
movzbl %al, %eax
cmpl -0x14(%rbp), %eax
jne 0x6658
movq -0x58(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movsbl %al, %eax
cmpl $0x6b, %eax
jne 0x6658
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x9880
movsbl %al, %eax
cmpl $0x72, %eax
je 0x6664
movl $0x0, -0x4(%rbp)
jmp 0x6897
movq -0x58(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98c0
testb $0x1, %al
jne 0x6692
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x98c0
testb $0x1, %al
jne 0x6692
jmp 0x669e
movl $0x0, -0x4(%rbp)
jmp 0x6897
cmpl $0x0, -0x14(%rbp)
je 0x671e
movq -0x58(%rbp), %rdi
movl -0x20(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5590
movq -0x58(%rbp), %rdi
movl %eax, -0x3c(%rbp)
movl -0x28(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5590
movq -0x58(%rbp), %rdi
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x5590
movl %eax, -0x34(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0x3, -0x40(%rbp)
jge 0x671c
movslq -0x40(%rbp), %rax
cmpl $0x1, -0x3c(%rbp,%rax,4)
je 0x6703
movslq -0x40(%rbp), %rax
cmpl $0x3, -0x3c(%rbp,%rax,4)
jne 0x670f
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x6897
jmp 0x6711
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x66e7
jmp 0x6798
movq -0x58(%rbp), %rdi
movl -0x20(%rbp), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5590
movq -0x58(%rbp), %rdi
movl %eax, -0x3c(%rbp)
movl -0x28(%rbp), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5590
movq -0x58(%rbp), %rdi
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5590
movl %eax, -0x34(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x3, -0x44(%rbp)
jge 0x6796
movslq -0x44(%rbp), %rax
cmpl $0x2, -0x3c(%rbp,%rax,4)
je 0x677d
movslq -0x44(%rbp), %rax
cmpl $0x3, -0x3c(%rbp,%rax,4)
jne 0x6789
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x6897
jmp 0x678b
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6761
jmp 0x6798
movq -0x58(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movl -0x28(%rbp), %esi
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x58(%rbp), %rdi
movslq -0x20(%rbp), %rax
movb $0x0, 0x208(%rdi,%rax)
movslq -0x1c(%rbp), %rax
movb $0x0, 0x208(%rdi,%rax)
movslq -0x2c(%rbp), %rax
movb $0x1, 0x208(%rdi,%rax)
movslq -0x28(%rbp), %rax
movb $0x1, 0x208(%rdi,%rax)
movslq -0x1c(%rbp), %rax
movq 0x8(%rdi,%rax,8), %rcx
movslq -0x28(%rbp), %rax
movq %rcx, 0x8(%rdi,%rax,8)
movslq -0x20(%rbp), %rax
movq 0x8(%rdi,%rax,8), %rcx
movslq -0x2c(%rbp), %rax
movq %rcx, 0x8(%rdi,%rax,8)
addq $0x248, %rdi            # imm = 0x248
movb $0x78, -0x45(%rbp)
leaq -0x45(%rbp), %rsi
callq 0x6c30
cmpl $0x0, -0x18(%rbp)
je 0x6853
movq -0x58(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
movl $0x45, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x6c60
jmp 0x686e
movq -0x58(%rbp), %rdi
addq $0x260, %rdi            # imm = 0x260
movl $0x46, -0x50(%rbp)
leaq -0x50(%rbp), %rsi
callq 0x6c60
movq -0x58(%rbp), %rax
movl 0x27c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x27c(%rax)
movl 0x278(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x278(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq