// File: 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:
subq $0x328, %rsp            # imm = 0x328
movb %dl, %al
movq %rdi, 0x320(%rsp)
movq %rsi, 0x318(%rsp)
andb $0x1, %al
movb %al, 0x317(%rsp)
movq 0x320(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x318(%rsp), %rsi
leaq 0x188(%rsp), %rdi
movl $0x8, %edx
callq 0x68e0
movq 0x148(%rsp), %rdi
movl $0x38, 0x184(%rsp)
callq 0x36f10
jmp 0x365e6
jmp 0x365e8
leaq 0x188(%rsp), %rdi
leaq 0x316(%rsp), %rsi
callq 0x6720
movq %rax, 0x140(%rsp)
jmp 0x36607
movq 0x140(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0x13f(%rsp)
jmp 0x36627
movb 0x13f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x13e(%rsp)
jne 0x3663e
jmp 0x36653
movsbl 0x316(%rsp), %eax
cmpl $0x20, %eax
setne %al
movb %al, 0x13e(%rsp)
movb 0x13e(%rsp), %al
testb $0x1, %al
jne 0x36663
jmp 0x36799
leaq 0x3b4e2e(%rip), %rdi    # 0x3eb498
leaq 0x316(%rsp), %rsi
callq 0x3ddc0
movq %rax, 0x130(%rsp)
jmp 0x36681
movq 0x130(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x3b4e00(%rip), %rdi    # 0x3eb498
callq 0x3ddf0
movq %rax, 0x160(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x3dda0
testb $0x1, %al
jne 0x366c0
jmp 0x3673a
leaq 0x3b4dd1(%rip), %rdi    # 0x3eb498
leaq 0x316(%rsp), %rsi
callq 0x3de20
movq %rax, 0x128(%rsp)
jmp 0x366de
movq 0x148(%rsp), %rdi
movq 0x128(%rsp), %rax
movl (%rax), %esi
movl 0x184(%rsp), %edx
callq 0x370f0
jmp 0x366fe
leaq 0x184(%rsp), %rdi
xorl %esi, %esi
callq 0xe5c0
jmp 0x3670f
jmp 0x36794
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x6230
jmp 0x36f00
movsbl 0x316(%rsp), %edi
callq 0x6430
cmpl $0x0, %eax
je 0x36768
movsbl 0x316(%rsp), %esi
addl $-0x30, %esi
leaq 0x184(%rsp), %rdi
callq 0xd7a0
jmp 0x36766
jmp 0x36792
movsbl 0x316(%rsp), %eax
cmpl $0x2f, %eax
jne 0x3678b
leaq 0x184(%rsp), %rdi
movl $0x10, %esi
callq 0x3df10
jmp 0x36789
jmp 0x36790
jmp 0x36e9a
jmp 0x36792
jmp 0x36794
jmp 0x365e8
leaq 0x188(%rsp), %rdi
leaq 0x316(%rsp), %rsi
callq 0x6720
movq %rax, 0x120(%rsp)
jmp 0x367b8
movq 0x120(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6220
movb %al, 0x11f(%rsp)
jmp 0x367d8
movb 0x11f(%rsp), %al
testb $0x1, %al
jne 0x367ff
jmp 0x367e5
movsbl 0x316(%rsp), %eax
cmpl $0x77, %eax
je 0x36804
movsbl 0x316(%rsp), %eax
cmpl $0x62, %eax
je 0x36804
jmp 0x36e9a
movq 0x148(%rsp), %rax
movb 0x316(%rsp), %cl
subb $0x77, %cl
setne %cl
movzbl %cl, %ecx
movl %ecx, 0x1090(%rax)
leaq 0x188(%rsp), %rdi
leaq 0x316(%rsp), %rsi
callq 0x6720
movq %rax, 0x110(%rsp)
jmp 0x36841
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6220
movb %al, 0x10f(%rsp)
jmp 0x36861
movb 0x10f(%rsp), %al
testb $0x1, %al
jne 0x3687b
jmp 0x3686e
movsbl 0x316(%rsp), %eax
cmpl $0x20, %eax
je 0x36880
jmp 0x36e9a
jmp 0x36882
leaq 0x188(%rsp), %rdi
leaq 0x316(%rsp), %rsi
callq 0x6720
movq %rax, 0x100(%rsp)
jmp 0x368a1
movq 0x100(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0xff(%rsp)
jmp 0x368c1
movb 0xff(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0xfe(%rsp)
jne 0x368d8
jmp 0x368ed
movsbl 0x316(%rsp), %eax
cmpl $0x20, %eax
setne %al
movb %al, 0xfe(%rsp)
movb 0xfe(%rsp), %al
testb $0x1, %al
jne 0x368fa
jmp 0x3692f
movq 0x148(%rsp), %rdi
movsbl 0x316(%rsp), %esi
callq 0x371f0
movb %al, 0xfd(%rsp)
jmp 0x36918
movb 0xfd(%rsp), %al
testb $0x1, %al
jne 0x3692a
jmp 0x36925
jmp 0x36e9a
jmp 0x36882
leaq 0x188(%rsp), %rdi
leaq 0x15f(%rsp), %rsi
callq 0x6720
movq %rax, 0xf0(%rsp)
jmp 0x3694e
movq 0xf0(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0xef(%rsp)
jmp 0x3696e
movb 0xef(%rsp), %al
testb $0x1, %al
jne 0x3697e
jmp 0x36b0b
movsbl 0x15f(%rsp), %eax
cmpl $0x61, %eax
jl 0x36b0b
movsbl 0x15f(%rsp), %eax
cmpl $0x68, %eax
jg 0x36b0b
leaq 0x188(%rsp), %rdi
leaq 0x15e(%rsp), %rsi
callq 0x6720
movq %rax, 0xe0(%rsp)
jmp 0x369bf
movq 0xe0(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0xdf(%rsp)
jmp 0x369df
movb 0xdf(%rsp), %al
testb $0x1, %al
jne 0x369ef
jmp 0x36b0b
movsbl 0x15e(%rsp), %eax
cmpl $0x33, %eax
je 0x36a0d
movsbl 0x15e(%rsp), %eax
cmpl $0x36, %eax
jne 0x36b0b
movsbl 0x15f(%rsp), %edi
callq 0x3df40
movl %eax, 0xd8(%rsp)
jmp 0x36a23
movsbl 0x15e(%rsp), %edi
callq 0x3df60
movl %eax, 0xd4(%rsp)
jmp 0x36a39
movl 0xd4(%rsp), %esi
movl 0xd8(%rsp), %edi
callq 0xd740
movl %eax, 0xd0(%rsp)
jmp 0x36a55
movq 0x148(%rsp), %rax
movl 0xd0(%rsp), %edx
movq 0x1938(%rax), %rcx
movl %edx, 0x20(%rcx)
movl 0x1090(%rax), %edi
callq 0x122b0
movl %eax, 0xcc(%rsp)
jmp 0x36a82
movq 0x148(%rsp), %rdi
movl 0xcc(%rsp), %eax
movl %eax, 0x158(%rsp)
movq 0x1938(%rdi), %rax
movl 0x20(%rax), %esi
movl 0x158(%rsp), %edx
callq 0x1b460
movq %rax, 0xc0(%rsp)
jmp 0x36ab8
movq 0x148(%rsp), %rdi
movl 0x1090(%rdi), %edx
movl $0x1, %esi
callq 0x16530
movq %rax, 0xb8(%rsp)
jmp 0x36ada
movq 0xb8(%rsp), %rcx
movq 0xc0(%rsp), %rax
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x36b09
movq 0x148(%rsp), %rax
movq 0x1938(%rax), %rax
movl $0x40, 0x20(%rax)
jmp 0x36b0b
leaq 0x188(%rsp), %rdi
leaq 0x310(%rsp), %rsi
callq 0x62c0
movq %rax, 0xb0(%rsp)
jmp 0x36b2a
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0xaf(%rsp)
jmp 0x36b4a
movb 0xaf(%rsp), %al
testb $0x1, %al
jne 0x36b57
jmp 0x36b70
movq 0x148(%rsp), %rax
movl 0x310(%rsp), %ecx
movq 0x1938(%rax), %rax
movl %ecx, 0x14(%rax)
leaq 0x188(%rsp), %rdi
leaq 0x30c(%rsp), %rsi
callq 0x62c0
movq %rax, 0xa0(%rsp)
jmp 0x36b8f
movq 0xa0(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, 0x9f(%rsp)
jmp 0x36baf
movb 0x9f(%rsp), %al
testb $0x1, %al
jne 0x36bbc
jmp 0x36be8
movq 0x148(%rsp), %rax
movl 0x30c(%rsp), %ecx
subl $0x1, %ecx
shll %ecx
cmpl $0x1, 0x1090(%rax)
sete %dl
andb $0x1, %dl
movzbl %dl, %edx
addl %edx, %ecx
movl %ecx, 0x1928(%rax)
movq 0x148(%rsp), %rax
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x90(%rsp)
movl 0x1918(%rax), %edi
xorl %esi, %esi
callq 0xd740
movl %eax, 0x98(%rsp)
jmp 0x36c18
movq 0x148(%rsp), %rax
movq 0x90(%rsp), %rcx
movl 0x98(%rsp), %edx
movslq %edx, %rdx
movb (%rcx,%rdx,4), %sil
xorb $0x5, %sil
movb %sil, (%rcx,%rdx,4)
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x80(%rsp)
movl 0x1918(%rax), %edi
movl $0x7, %esi
callq 0xd740
movl %eax, 0x8c(%rsp)
jmp 0x36c69
movq 0x148(%rsp), %rax
movq 0x80(%rsp), %rcx
movl 0x8c(%rsp), %edx
movslq %edx, %rdx
movb (%rcx,%rdx,4), %sil
xorb $0xa, %sil
movb %sil, (%rcx,%rdx,4)
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x70(%rsp)
movl 0x191c(%rax), %edi
xorl %esi, %esi
callq 0xd740
movl %eax, 0x7c(%rsp)
jmp 0x36cb1
movq 0x148(%rsp), %rax
movq 0x70(%rsp), %rcx
movl 0x7c(%rsp), %edx
movslq %edx, %rdx
movb (%rcx,%rdx,4), %sil
xorb $0x1, %sil
movb %sil, (%rcx,%rdx,4)
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x60(%rsp)
movl 0x191c(%rax), %edi
movl $0x7, %esi
callq 0xd740
movl %eax, 0x6c(%rsp)
jmp 0x36cf6
movq 0x148(%rsp), %rax
movq 0x60(%rsp), %rcx
movl 0x6c(%rsp), %edx
movslq %edx, %rdx
movb (%rcx,%rdx,4), %sil
xorb $0x2, %sil
movb %sil, (%rcx,%rdx,4)
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x50(%rsp)
movl 0x1920(%rax), %edi
xorl %esi, %esi
callq 0xd740
movl %eax, 0x5c(%rsp)
jmp 0x36d38
movq 0x148(%rsp), %rax
movq 0x50(%rsp), %rcx
movl 0x5c(%rsp), %edx
movslq %edx, %rdx
movb (%rcx,%rdx,4), %sil
xorb $0x4, %sil
movb %sil, (%rcx,%rdx,4)
movq %rax, %rcx
addq $0x1778, %rcx           # imm = 0x1778
movq %rcx, 0x40(%rsp)
movl 0x1920(%rax), %edi
movl $0x7, %esi
callq 0xd740
movl %eax, 0x4c(%rsp)
jmp 0x36d7d
movq 0x148(%rsp), %rdi
movq 0x40(%rsp), %rax
movl 0x4c(%rsp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx,4), %dl
xorb $0x8, %dl
movb %dl, (%rax,%rcx,4)
movb 0x317(%rsp), %al
andb $0x1, %al
movb %al, 0x1924(%rdi)
callq 0x37410
jmp 0x36db0
movq 0x148(%rsp), %rdi
callq 0x37630
movq %rax, 0x38(%rsp)
jmp 0x36dc4
movq 0x148(%rsp), %rdi
movq 0x38(%rsp), %rcx
movq 0x1938(%rdi), %rax
movq %rcx, 0x38(%rax)
callq 0x37740
movq %rax, 0x30(%rsp)
jmp 0x36de8
movq 0x148(%rsp), %rdi
movq 0x30(%rsp), %rcx
movq 0x1938(%rdi), %rax
movq %rcx, (%rax)
callq 0x377f0
movq %rax, 0x28(%rsp)
jmp 0x36e0b
movq 0x148(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq 0x1938(%rdi), %rax
movq %rcx, 0x8(%rax)
callq 0x378d0
movl %eax, 0x24(%rsp)
jmp 0x36e2e
movq 0x148(%rsp), %rdi
movl 0x24(%rsp), %ecx
movq 0x1938(%rdi), %rax
movl %ecx, 0x24(%rax)
xorl %esi, %esi
callq 0x379e0
movl %eax, 0x20(%rsp)
jmp 0x36e51
movq 0x148(%rsp), %rdi
movl 0x20(%rsp), %ecx
movq 0x1938(%rdi), %rax
movl %ecx, 0x28(%rax)
movl $0x1, %esi
callq 0x379e0
movl %eax, 0x1c(%rsp)
jmp 0x36e77
movl 0x1c(%rsp), %ecx
movq 0x148(%rsp), %rax
movq 0x1938(%rax), %rax
movl %ecx, 0x2c(%rax)
movl $0x1, 0x154(%rsp)
jmp 0x36eeb
movq 0x35117(%rip), %rdi     # 0x6bfb8
leaq 0x24328(%rip), %rsi     # 0x5b1d0
callq 0x64b0
movq %rax, 0x10(%rsp)
jmp 0x36eb4
movq 0x10(%rsp), %rdi
movq 0x318(%rsp), %rsi
callq 0x6420
movq %rax, 0x8(%rsp)
jmp 0x36ecd
movq 0x8(%rsp), %rdi
movq 0x350b7(%rip), %rsi     # 0x6bf90
callq 0x6520
jmp 0x36ee0
movl $0x0, 0x154(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x6230
addq $0x328, %rsp            # imm = 0x328
retq
movq 0x178(%rsp), %rdi
callq 0x6970
nopl (%rax)