// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/position.cpp
// Function: Position::flip()
void Position::flip() {

  assert(is_ok());

  // Make a copy of current position before to start changing
  const Position pos(*this, threadID);

  clear();
  threadID = pos.thread();

  // Board
  for (Square s = SQ_A1; s <= SQ_H8; s++)
      if (!pos.square_is_empty(s))
          put_piece(Piece(pos.piece_on(s) ^ 8), flip_square(s));

  // Side to move
  sideToMove = opposite_color(pos.side_to_move());

  // Castling rights
  if (pos.can_castle_kingside(WHITE))  do_allow_oo(BLACK);
  if (pos.can_castle_queenside(WHITE)) do_allow_ooo(BLACK);
  if (pos.can_castle_kingside(BLACK))  do_allow_oo(WHITE);
  if (pos.can_castle_queenside(BLACK)) do_allow_ooo(WHITE);

  initialKFile  = pos.initialKFile;
  initialKRFile = pos.initialKRFile;
  initialQRFile = pos.initialQRFile;

  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;

  // En passant square
  if (pos.st->epSquare != SQ_NONE)
      st->epSquare = flip_square(pos.st->epSquare);

  // Checkers
  find_checkers();

  // Hash keys
  st->key = compute_key();
  st->pawnKey = compute_pawn_key();
  st->materialKey = compute_material_key();

  // Incremental scores
  st->value = compute_value();

  // Material
  st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
  st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);

  assert(is_ok());
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x1940, %rsp           # imm = 0x1940
movq %rdi, %rbx
movl 0x192c(%rdi), %ebp
movq %rsp, %r14
movl $0x1940, %edx           # imm = 0x1940
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5310
movq 0x1938(%r14), %rsi
leaq 0x1878(%rsp), %r14
movl $0x98, %edx
movq %r14, %rdi
callq 0x5310
movq %r14, 0xc0(%r14)
andq $0x0, 0x98(%r14)
movl %ebp, 0xb4(%r14)
andq $0x0, 0xb8(%r14)
movq %rbx, %rdi
callq 0x177b4
movl %ebp, 0x192c(%rbx)
xorl %r14d, %r14d
cmpq $0x40, %r14
je 0x1a8e3
movl (%rsp,%r14,4), %esi
cmpl $0x10, %esi
je 0x1a8de
xorl $0x8, %esi
movl %r14d, %edx
xorl $0x38, %edx
movq %rbx, %rdi
callq 0x1789e
incq %r14
jmp 0x1a8be
movl 0x1090(%rsp), %eax
xorl $0x1, %eax
movl %eax, 0x1090(%rbx)
movl 0x1888(%rsp), %eax
testb $0x1, %al
je 0x1a910
movq 0x1938(%rbx), %rax
orl $0x2, 0x10(%rax)
movl 0x1888(%rsp), %eax
testb $0x4, %al
je 0x1a926
movq 0x1938(%rbx), %rax
orl $0x8, 0x10(%rax)
movl 0x1888(%rsp), %eax
testb $0x2, %al
je 0x1a93c
movq 0x1938(%rbx), %rax
orl $0x1, 0x10(%rax)
movl 0x1888(%rsp), %eax
testb $0x8, %al
je 0x1a94b
movq 0x1938(%rbx), %rax
orl $0x4, 0x10(%rax)
movslq 0x1918(%rsp), %rax
movl %eax, 0x1918(%rbx)
movslq 0x191c(%rsp), %rcx
movl %ecx, 0x191c(%rbx)
movslq 0x1920(%rsp), %rdx
movl %edx, 0x1920(%rbx)
xorb $0x5, 0x1778(%rbx,%rax,4)
orq $0x38, %rax
shlq $0x2, %rax
xorb $0xa, 0x1778(%rbx,%rax)
xorb $0x1, 0x1778(%rbx,%rcx,4)
orq $0x38, %rcx
shlq $0x2, %rcx
xorb $0x2, 0x1778(%rbx,%rcx)
xorb $0x4, 0x1778(%rbx,%rdx,4)
orq $0x38, %rdx
shlq $0x2, %rdx
xorb $0x8, 0x1778(%rbx,%rdx)
movl 0x1898(%rsp), %eax
cmpl $0x40, %eax
je 0x1a9d6
xorl $0x38, %eax
movq 0x1938(%rbx), %rcx
movl %eax, 0x20(%rcx)
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)
addq $0x1940, %rsp           # imm = 0x1940
popq %rbx
popq %r14
popq %rbp
retq
nop