// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/position.cpp
// Function: Position::from_fen(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
void Position::from_fen(const string& fen, bool isChess960) {
/*
   A FEN string defines a particular position using only the ASCII character set.

   A FEN string contains six fields. The separator between fields is a space. The fields are:

   1) Piece placement (from white's perspective). Each rank is described, starting with rank 8 and ending
      with rank 1; within each rank, the contents of each square are described from file A through file H.
      Following the Standard Algebraic Notation (SAN), each piece is identified by a single letter taken
      from the standard English names. White pieces are designated using upper-case letters ("PNBRQK")
      while Black take lowercase ("pnbrqk"). Blank squares are noted using digits 1 through 8 (the number
      of blank squares), and "/" separate ranks.

   2) Active color. "w" means white moves next, "b" means black.

   3) Castling availability. If neither side can castle, this is "-". Otherwise, this has one or more
      letters: "K" (White can castle kingside), "Q" (White can castle queenside), "k" (Black can castle
      kingside), and/or "q" (Black can castle queenside).

   4) En passant target square in algebraic notation. If there's no en passant target square, this is "-".
      If a pawn has just made a 2-square move, this is the position "behind" the pawn. This is recorded
      regardless of whether there is a pawn in position to make an en passant capture.

   5) Halfmove clock: This is the number of halfmoves since the last pawn advance or capture. This is used
      to determine if a draw can be claimed under the fifty-move rule.

   6) Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move.
*/

  char token;
  int hmc, fmn;
  std::istringstream ss(fen);
  Square sq = SQ_A8;

  clear();

  // 1. Piece placement field
  while (ss.get(token) && token != ' ')
  {
      if (pieceLetters.find(token) != pieceLetters.end())
      {
          put_piece(pieceLetters[token], sq);
          sq++;
      }
      else if (isdigit(token))
          sq += Square(token - '0'); // Skip the given number of files
      else if (token == '/')
          sq -= SQ_A3; // Jump back of 2 rows
      else
          goto incorrect_fen;
  }

  // 2. Active color
  if (!ss.get(token) || (token != 'w' && token != 'b'))
      goto incorrect_fen;

  sideToMove = (token == 'w' ? WHITE : BLACK);

  if (!ss.get(token) || token != ' ')
      goto incorrect_fen;
  
  // 3. Castling availability
  while (ss.get(token) && token != ' ')
      if (!set_castling_rights(token))
          goto incorrect_fen;

  // 4. En passant square
  char col, row;
  if (   (ss.get(col) && (col >= 'a' && col <= 'h'))
      && (ss.get(row) && (row == '3' || row == '6')))
  {
      st->epSquare = make_square(file_from_char(col), rank_from_char(row));

      // Ignore if no capture is possible
      Color them = opposite_color(sideToMove);
      if (!(attacks_from<PAWN>(st->epSquare, them) & pieces(PAWN, sideToMove)))
          st->epSquare = SQ_NONE;
  }
  
  // 5. Halfmove clock
  if (ss >> hmc)
      st->rule50 = hmc;

  // 6. Fullmove number
  if (ss >> fmn)
      startPosPlyCounter = (fmn - 1) * 2 + int(sideToMove == BLACK);
  
  // Various initialisations
  castleRightsMask[make_square(initialKFile,  RANK_1)] ^= WHITE_OO | WHITE_OOO;
  castleRightsMask[make_square(initialKFile,  RANK_8)] ^= BLACK_OO | BLACK_OOO;
  castleRightsMask[make_square(initialKRFile, RANK_1)] ^= WHITE_OO;
  castleRightsMask[make_square(initialKRFile, RANK_8)] ^= BLACK_OO;
  castleRightsMask[make_square(initialQRFile, RANK_1)] ^= WHITE_OOO;
  castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO;

  chess960 = isChess960;
  find_checkers();

  st->key = compute_key();
  st->pawnKey = compute_pawn_key();
  st->materialKey = compute_material_key();
  st->value = compute_value();
  st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
  st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
  
  STARTNEW
  // init neighborhood lists 
//  write_neigh_arrays(neighOccSquares, neighExplValue);
//  assert(neigh_is_ok());
  ENDNEW
  
  return;

incorrect_fen:
  cout << "Error in FEN string: " << fen << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl %edx, 0x4(%rsp)
movq %rdi, %rbx
leaq 0x18(%rsp), %r15
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %rsi, 0x10(%rsp)
callq 0x5730
movq %rbx, %rdi
callq 0x177b4
pushq $0x38
popq %r12
leaq 0x1(%rsp), %r13
leaq 0x39ce97(%rip), %rbp    # 0x3b42e8
leaq 0x39ce98(%rip), %r14    # 0x3b42f0
movq %r15, %rdi
movq %r13, %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x174c1
cmpb $0x20, 0x1(%rsp)
je 0x174c1
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1b288
cmpq %r14, %rax
je 0x174a5
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1aada
movl (%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1789e
incl %r12d
jmp 0x17458
movsbl 0x1(%rsp), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x174b7
addl %ecx, %r12d
jmp 0x17458
cmpb $0x2f, %al
jne 0x174de
addl $-0x10, %r12d
jmp 0x17458
leaq 0x18(%rsp), %rdi
leaq 0x1(%rsp), %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x17522
movq 0x1dabb(%rip), %rdi     # 0x34fa0
leaq 0x12bc4(%rip), %rsi     # 0x2a0b0
callq 0x53b0
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x5340
movq %rax, %rdi
callq 0x51d0
leaq 0x18(%rsp), %rdi
callq 0x51a0
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x1(%rsp), %eax
cmpl $0x77, %eax
je 0x17531
cmpl $0x62, %eax
jne 0x174de
xorl %ecx, %ecx
cmpb $0x77, %al
setne %cl
movl %ecx, 0x1090(%rbx)
leaq 0x18(%rsp), %rdi
leaq 0x1(%rsp), %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x174de
cmpb $0x20, 0x1(%rsp)
jne 0x174de
leaq 0x18(%rsp), %r15
leaq 0x1(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x175a5
movb 0x1(%rsp), %al
cmpb $0x20, %al
je 0x175a5
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1791a
testb %al, %al
jne 0x17570
jmp 0x174de
leaq 0x18(%rsp), %rdi
leaq 0x2(%rsp), %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x175ed
movb 0x2(%rsp), %al
addb $-0x61, %al
cmpb $0x7, %al
ja 0x175ed
leaq 0x18(%rsp), %rdi
leaq 0x3(%rsp), %rsi
callq 0x5560
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x17726
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x5220
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x17618
movl 0xc(%rsp), %eax
movq 0x1938(%rbx), %rcx
movl %eax, 0x14(%rcx)
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5220
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x17651
xorl %eax, %eax
cmpl $0x1, 0x1090(%rbx)
movl 0x8(%rsp), %ecx
sete %al
leal (%rax,%rcx,2), %eax
addl $-0x2, %eax
movl %eax, 0x1928(%rbx)
movslq 0x1918(%rbx), %rax
xorl $0x5, 0x1778(%rbx,%rax,4)
orq $0x38, %rax
shlq $0x2, %rax
xorb $0xa, 0x1778(%rbx,%rax)
movslq 0x191c(%rbx), %rax
xorl $0x1, 0x1778(%rbx,%rax,4)
orq $0x38, %rax
shlq $0x2, %rax
xorb $0x2, 0x1778(%rbx,%rax)
movslq 0x1920(%rbx), %rax
xorl $0x4, 0x1778(%rbx,%rax,4)
orq $0x38, %rax
shlq $0x2, %rax
xorb $0x8, 0x1778(%rbx,%rax)
movl 0x4(%rsp), %eax
movb %al, 0x1924(%rbx)
movq %rbx, %rdi
callq 0x17a70
movq %rbx, %rdi
callq 0x17b74
movq 0x1938(%rbx), %rcx
movq %rax, 0x38(%rcx)
movq %rbx, %rdi
callq 0x17bfc
movq 0x1938(%rbx), %r14
movq %rax, (%r14)
movq %rbx, %rdi
callq 0x17c72
movq %rax, 0x8(%r14)
movq %rbx, %rdi
callq 0x17ce6
movq 0x1938(%rbx), %r14
movl %eax, 0x24(%r14)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x17da0
movl %eax, 0x28(%r14)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x17da0
movl %eax, 0x2c(%r14)
jmp 0x17506
movzbl 0x3(%rsp), %eax
cmpl $0x36, %eax
je 0x17739
cmpl $0x33, %eax
jne 0x175ed
movsbq 0x2(%rsp), %rcx
addq $-0x61, %rcx
leaq -0x188(,%rax,8), %rdx
orq %rcx, %rdx
movq 0x1938(%rbx), %rax
movl %edx, 0x20(%rax)
movl 0x1090(%rbx), %ecx
leal (,%rcx,8), %esi
xorl $0x9, %esi
shlq $0x9, %rsi
addq 0x1d865(%rip), %rsi     # 0x34fd8
movq 0x108(%rbx), %rdi
andq (%rsi,%rdx,8), %rdi
testq %rdi, 0x140(%rbx,%rcx,8)
jne 0x175ed
movl $0x40, 0x20(%rax)
jmp 0x175ed
jmp 0x1779e
jmp 0x1779e
jmp 0x1779e
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x51a0
movq %rbx, %rdi
callq 0x5780
nop