--expandArc: evaluate

let sideToMove = position.sideToMove_1
let nonSideToMove = opposite(sideToMove)
let middleGamePhase = getGamePhase(position)
let occupancy = getOccupancy(position)
let pawns: array[PieceColor.White .. PieceColor.Black, Bitboard] = [
    getBitboard(position, Pawn, White), getBitboard(position, Pawn, Black)]
let rooks: array[PieceColor.White .. PieceColor.Black, Bitboard] = [
    getBitboard(position, Rook, White), getBitboard(position, Rook, Black)]
let kingZones: array[PieceColor.White .. PieceColor.Black, Bitboard] = [
    getKingZoneMask(White, toSquare(getBitboard(position, King, White))),
    getKingZoneMask(Black, toSquare(getBitboard(position, King, Black)))]
let allPawns = pawns[White] or pawns[Black]
let endGamePhase = 24 - middleGamePhase
let scaledMiddleGame = middleGamePhase / 24
let scaledEndGame = endGamePhase / 24
var middleGameScores: array[PieceColor.White .. PieceColor.Black, Score] = [0, 0]
var endGameScores: array[PieceColor.White .. PieceColor.Black, Score] = [0, 0]
var kingAttackers: array[PieceColor.White .. PieceColor.Black, int] = [0, 0]
block :tmp:
  var sq
  var bits = occupancy
  block :tmp_1:
    while bits != 0:
      sq = toSquare(bits)
      let piece = getPiece(position, sq)
      let enemyColor = opposite(piece.color)
      let attacks = getAttackingMoves(position, sq)
      kingAttackers[enemyColor] +=
          countSquares(attacks and kingZones[enemyColor])
      let mobilityMoves = countSquares(getMobility(position, sq))
      middleGameScores[piece.color] +=
          MIDDLEGAME_VALUE_TABLES[piece.color][piece.kind][sq]
      endGameScores[piece.color] +=
          ENDGAME_VALUE_TABLES[piece.color][piece.kind][sq]
      let scores = getMobilityBonus(piece.kind, mobilityMoves)
      middleGameScores[piece.color] += scores.mg
      endGameScores[piece.color] += scores.eg
      bits = bits and bits - 1
block :tmp_2:
  var color_1
  mixin inc
  var res = 0
  block :tmp_3:
    while res <= 1:
      var
        :tmpD
        :tmpD_1
      color_1 = chckRange(res, White, None)
      let side = if color_1 == Black:
        :tmpD = -1.0
        :tmpD else:
        :tmpD_1 = 1.0
        :tmpD_1
      let bishopPair = countSquares(getBitboard(position, Bishop, color_1)) == 2
      if bishopPair:
        middleGameScores[color_1] += 62
        endGameScores[color_1] += 147
      let attacked = clamp(kingAttackers[color_1], 0, 8)
      middleGameScores[color_1] += KING_ZONE_ATTACKS_MIDDLEGAME_BONUS[attacked]
      endGameScores[color_1] += KING_ZONE_ATTACKS_ENDGAME_BONUS[attacked]
      let strongPawns = countSquares((forwardLeftRelativeTo(pawns[color_1],
          color_1) or
          forwardRightRelativeTo(pawns[color_1], color_1)) and
          pawns[color_1])
      middleGameScores[color_1] +=
          22 * chckRange(strongPawns, -2147483648'i32, 2147483647'i32)
      endGameScores[color_1] +=
          24 * chckRange(strongPawns, -2147483648'i32, 2147483647'i32)
      block :tmp_4:
        var pawn
        var :tmp_5
        :tmp_5 = pawns[color_1]
        var bits_1 = :tmp_5
        block :tmp_6:
          while bits_1 != 0:
            var
              :tmpD_2
              :tmpD_3
            pawn = toSquare(bits_1)
            let square = if color_1 == Black:
              :tmpD_2 = flip(pawn)
              :tmpD_2 else:
              :tmpD_3 = pawn
              :tmpD_3
            if (getPassedPawnMask(color_1, pawn) and pawns[opposite(color_1)]) ==_2 0:
              middleGameScores[color_1] +=
                  PASSED_PAWN_MIDDLEGAME_TABLES[color_1][pawn]
              endGameScores[color_1] += PASSED_PAWN_ENDGAME_TABLES[color_1][pawn]
            if (pawns[color_1] and getIsolatedPawnMask(fileFromSquare(pawn))) ==_2 0:
              middleGameScores[color_1] +=
                  ISOLATED_PAWN_MIDDLEGAME_TABLES[color_1][pawn]
              endGameScores[color_1] += ISOLATED_PAWN_ENDGAME_TABLES[color_1][
                  pawn]
            bits_1 = bits_1 and bits_1 - 1
      block :tmp_7:
        var file
        mixin inc
        var res_1 = 0
        block :tmp_8:
          while res_1 <= 7:
            file = T(res_1)
            let fileMask = getFileMask(file)
            let friendlyPawnsOnFile = pawns[color_1] and fileMask
            if countSquares(fileMask and allPawns) == 0:
              block :tmp_9:
                var rook
                var :tmp_10
                :tmp_10 = rooks[color_1] and fileMask
                var bits_2 = :tmp_10
                block :tmp_11:
                  while bits_2 != 0:
                    rook = toSquare(bits_2)
                    middleGameScores[color_1] += 81
                    endGameScores[color_1] += 26
                    bits_2 = bits_2 and bits_2 - 1
            if friendlyPawnsOnFile ==_2 0 and
                countSquares(fileMask and pawns[opposite(color_1)]) == 1:
              block :tmp_12:
                var rook_1
                var :tmp_13
                :tmp_13 = rooks[color_1] and fileMask
                var bits_3 = :tmp_13
                block :tmp_14:
                  while bits_3 != 0:
                    rook_1 = toSquare(bits_3)
                    middleGameScores[color_1] += 32
                    endGameScores[color_1] += 23
                    bits_3 = bits_3 and bits_3 - 1
            inc(res_1, 1)
      inc(res, 1)
let middleGameScore = middleGameScores[sideToMove] -
    middleGameScores[nonSideToMove]
let endGameScore = endGameScores[sideToMove] - endGameScores[nonSideToMove]
result = chckRange((middleGameScore * middleGamePhase +
    endGameScore * endGamePhase) div
    24, -2147483648'i32, 2147483647'i32)
result += 10'i32
-- end of expandArc ------------------------