// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r15, %rdi
movq %rsi, 0x10(%rsp)
movl $0x8, %edx
callq 0x5720
movq %rbx, %rdi
callq 0x1ccf2
movl $0x38, %r12d
leaq 0x1(%rsp), %r13
leaq 0x39eab5(%rip), %r14    # 0x3bb2f0
leaq 0x39eaa6(%rip), %rbp    # 0x3bb2e8
movq %r15, %rdi
movq %r13, %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1c8d0
movb 0x1(%rsp), %al
cmpb $0x20, %al
je 0x1c8d0
movq 0x39ea8e(%rip), %rdx    # 0x3bb2f8
movq %r14, %rcx
testq %rdx, %rdx
je 0x1c88c
movq %r14, %rcx
xorl %esi, %esi
cmpb %al, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x1c875
cmpq %r14, %rcx
je 0x1c896
cmpb 0x20(%rcx), %al
jge 0x1c8b0
movsbl %al, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0x1c8a6
addl %ecx, %r12d
jmp 0x1c842
cmpb $0x2f, %al
jne 0x1c8f1
addl $-0x10, %r12d
jmp 0x1c842
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20d50
movl (%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1cdea
incl %r12d
jmp 0x1c842
leaq 0x18(%rsp), %rdi
leaq 0x1(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x1c97b
movq 0x1f6a8(%rip), %rdi     # 0x3bfa0
leaq 0x158c1(%rip), %rsi     # 0x321c0
movl $0x15, %edx
callq 0x5460
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x1f684(%rip), %rdi     # 0x3bfa0
callq 0x5460
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5270
movq 0x1f62e(%rip), %rsi     # 0x3bf80
leaq 0x18(%rsp), %rdi
callq 0x55b0
leaq 0x90(%rsp), %rdi
callq 0x5140
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 0x1c98e
cmpl $0x62, %eax
jne 0x1c8f1
xorl %ecx, %ecx
cmpb $0x77, %al
setne %cl
movl %ecx, 0x1090(%rbx)
leaq 0x18(%rsp), %rdi
leaq 0x1(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1c8f1
cmpb $0x20, 0x1(%rsp)
jne 0x1c8f1
leaq 0x18(%rsp), %r15
leaq 0x1(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1ca06
movb 0x1(%rsp), %al
cmpb $0x20, %al
je 0x1ca06
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1ce66
testb %al, %al
jne 0x1c9d1
jmp 0x1c8f1
leaq 0x18(%rsp), %rdi
leaq 0x2(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1ca4e
movb 0x2(%rsp), %al
addb $-0x61, %al
cmpb $0x7, %al
ja 0x1ca4e
leaq 0x18(%rsp), %rdi
leaq 0x3(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x1cc4f
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x5220
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1ca79
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 0x1cab2
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 0x1cfc6
movq %rbx, %rdi
callq 0x1d136
movq 0x1938(%rbx), %rcx
movq %rax, 0x38(%rcx)
movq %rbx, %rdi
callq 0x1d1bc
movq 0x1938(%rbx), %rcx
movq %rax, (%rcx)
leaq 0x150(%rbx), %rax
leaq 0x39b63c(%rip), %rdx    # 0x3b8190
xorl %esi, %esi
xorl %edi, %edi
movl $0x1, %r8d
movq %rsi, %r9
shlq $0x5, %r9
addq %rax, %r9
movq %rdx, %r10
movslq (%r9,%r8,4), %r11
testq %r11, %r11
jle 0x1cb83
xorl %r14d, %r14d
xorq (%r10,%r14,8), %rdi
incq %r14
cmpq %r14, %r11
jne 0x1cb77
incq %r8
addq $0x200, %r10            # imm = 0x200
cmpq $0x6, %r8
jne 0x1cb6b
leaq 0x1(%rsi), %r8
addq $0x1000, %rdx           # imm = 0x1000
testq %rsi, %rsi
movq %r8, %rsi
je 0x1cb58
movq %rdi, 0x8(%rcx)
movq %rbx, %rdi
callq 0x1d292
movq 0x1938(%rbx), %rcx
movl %eax, 0x24(%rcx)
movdqu 0x39e664(%rip), %xmm0   # 0x3bb228
movdqu 0x158(%rbx), %xmm1
pshufd $0xf5, %xmm0, %xmm2     # xmm2 = xmm0[1,1,3,3]
pmuludq %xmm1, %xmm0
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
pshufd $0xf5, %xmm1, %xmm1     # xmm1 = xmm1[1,1,3,3]
pmuludq %xmm2, %xmm1
pshufd $0xe8, %xmm1, %xmm1     # xmm1 = xmm1[0,2,2,3]
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, 0x28(%rcx)
movdqu 0x39e61d(%rip), %xmm0   # 0x3bb228
movdqu 0x178(%rbx), %xmm1
pshufd $0xf5, %xmm0, %xmm2     # xmm2 = xmm0[1,1,3,3]
pmuludq %xmm1, %xmm0
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
pshufd $0xf5, %xmm1, %xmm1     # xmm1 = xmm1[1,1,3,3]
pmuludq %xmm2, %xmm1
pshufd $0xe8, %xmm1, %xmm1     # xmm1 = xmm1[0,2,2,3]
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, 0x2c(%rcx)
jmp 0x1c94b
movzbl 0x3(%rsp), %eax
cmpl $0x36, %eax
je 0x1cc62
cmpl $0x33, %eax
jne 0x1ca4e
movsbq %al, %rax
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 0x1f338(%rip), %rsi     # 0x3bfd8
movq 0x108(%rbx), %rdi
andq (%rsi,%rdx,8), %rdi
testq %rdi, 0x140(%rbx,%rcx,8)
jne 0x1ca4e
movl $0x40, 0x20(%rax)
jmp 0x1ca4e
jmp 0x1ccc9
jmp 0x1ccc9
movq %rax, %rbx
movq 0x1f2ad(%rip), %rsi     # 0x3bf80
leaq 0x18(%rsp), %rdi
callq 0x55b0
leaq 0x90(%rsp), %rdi
callq 0x5140
movq %rbx, %rdi
callq 0x5790