// File: /workspace/github_cpp_makefile_star2_O3/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 0x1d226
movl $0x38, %r12d
leaq 0x1(%rsp), %r13
leaq 0x39f585(%rip), %r14    # 0x3bc2f0
leaq 0x39f576(%rip), %rbp    # 0x3bc2e8
movq %r15, %rdi
movq %r13, %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1cdfd
movb 0x1(%rsp), %al
cmpb $0x20, %al
je 0x1cdfd
movq 0x39f55e(%rip), %rdx    # 0x3bc2f8
testq %rdx, %rdx
je 0x1cdc3
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 0x1cda2
cmpq %r14, %rcx
je 0x1cdc3
cmpb 0x20(%rcx), %al
jge 0x1cddd
movsbl %al, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0x1cdd3
addl %ecx, %r12d
jmp 0x1cd72
cmpb $0x2f, %al
jne 0x1ce1e
addl $-0x10, %r12d
jmp 0x1cd72
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2123e
movl (%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x1d31e
incl %r12d
jmp 0x1cd72
leaq 0x18(%rsp), %rdi
leaq 0x1(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x1cea8
movq 0x2017b(%rip), %rdi     # 0x3cfa0
leaq 0x16414(%rip), %rsi     # 0x33240
movl $0x15, %edx
callq 0x5460
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x20157(%rip), %rdi     # 0x3cfa0
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 0x20101(%rip), %rsi     # 0x3cf80
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 0x1cebb
cmpl $0x62, %eax
jne 0x1ce1e
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 0x1ce1e
cmpb $0x20, 0x1(%rsp)
jne 0x1ce1e
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 0x1cf33
movb 0x1(%rsp), %al
cmpb $0x20, %al
je 0x1cf33
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1d39a
testb %al, %al
jne 0x1cefe
jmp 0x1ce1e
leaq 0x18(%rsp), %rdi
leaq 0x2(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1cf7b
movb 0x2(%rsp), %al
addb $-0x61, %al
cmpb $0x7, %al
ja 0x1cf7b
leaq 0x18(%rsp), %rdi
leaq 0x3(%rsp), %rsi
callq 0x5570
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x1d186
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x5220
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1cfa6
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 0x1cfdf
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 0x1d4fa
movq %rbx, %rdi
callq 0x1d66a
movq 0x1938(%rbx), %rcx
movq %rax, 0x38(%rcx)
movq %rbx, %rdi
callq 0x1d6f0
movq 0x1938(%rbx), %rcx
movq %rax, (%rcx)
leaq 0x150(%rbx), %rax
movb $0x1, %dl
xorl %esi, %esi
leaq 0x39bf0b(%rip), %rdi    # 0x3b8f90
xorl %r8d, %r8d
movq %rsi, %r9
shlq $0xc, %r9
addq %rdi, %r9
addq $0x200, %r9             # imm = 0x200
movl $0x1, %r10d
shlq $0x5, %rsi
addq %rax, %rsi
movslq (%rsi,%r10,4), %r11
testq %r11, %r11
jle 0x1d0be
xorl %r14d, %r14d
xorq (%r9,%r14,8), %r8
incq %r14
cmpq %r14, %r11
jne 0x1d0b2
incq %r10
addq $0x200, %r9             # imm = 0x200
cmpq $0x6, %r10
jne 0x1d0a6
movl $0x1, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0x1d088
movq %r8, 0x8(%rcx)
movq %rbx, %rdi
callq 0x1d7d4
movq 0x1938(%rbx), %rcx
movl %eax, 0x24(%rcx)
movdqu 0x39f12d(%rip), %xmm0   # 0x3bc228
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 0x39f0e6(%rip), %xmm0   # 0x3bc228
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 0x1ce78
movzbl 0x3(%rsp), %eax
cmpl $0x36, %eax
je 0x1d199
cmpl $0x33, %eax
jne 0x1cf7b
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 0x1fe05(%rip), %rsi     # 0x3cfd8
movq 0x108(%rbx), %rdi
andq (%rsi,%rdx,8), %rdi
testq %rdi, 0x140(%rbx,%rcx,8)
jne 0x1cf7b
movl $0x40, 0x20(%rax)
jmp 0x1cf7b
jmp 0x1d1fc
jmp 0x1d1fc
movq %rax, %rbx
movq 0x1fd7a(%rip), %rsi     # 0x3cf80
leaq 0x18(%rsp), %rdi
callq 0x55b0
leaq 0x90(%rsp), %rdi
callq 0x5140
movq %rbx, %rdi
callq 0x5790
nop