// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x1948, %rsp           # imm = 0x1948
movq %rdi, %rbx
movl 0x192c(%rdi), %ebp
leaq 0x8(%rsp), %r14
movl $0x1940, %edx           # imm = 0x1940
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5320
movq 0x1938(%r14), %rsi
leaq 0x1880(%rsp), %r14
movl $0x98, %edx
movq %r14, %rdi
callq 0x5320
movq %r14, 0xc0(%r14)
xorl %r15d, %r15d
movq %r15, 0x98(%r14)
movl %ebp, 0xb4(%r14)
movq %r15, 0xb8(%r14)
movq %rbx, %rdi
callq 0x1d226
movl %ebp, 0x192c(%rbx)
movl 0x8(%rsp,%r15,4), %esi
cmpl $0x10, %esi
je 0x20efd
xorl $0x8, %esi
movl %r15d, %edx
xorl $0x38, %edx
movq %rbx, %rdi
callq 0x1d31e
incq %r15
cmpq $0x40, %r15
jne 0x20ee2
movl 0x1098(%rsp), %eax
xorl $0x1, %eax
movl %eax, 0x1090(%rbx)
movl 0x1890(%rsp), %eax
testb $0x1, %al
je 0x20f33
movq 0x1938(%rbx), %rax
orl $0x2, 0x10(%rax)
movl 0x1890(%rsp), %eax
testb $0x4, %al
je 0x20f49
movq 0x1938(%rbx), %rax
orl $0x8, 0x10(%rax)
movl 0x1890(%rsp), %eax
testb $0x2, %al
je 0x20f5f
movq 0x1938(%rbx), %rax
orl $0x1, 0x10(%rax)
movl 0x1890(%rsp), %eax
testb $0x8, %al
je 0x20f6e
movq 0x1938(%rbx), %rax
orl $0x4, 0x10(%rax)
movslq 0x1920(%rsp), %rax
movl %eax, 0x1918(%rbx)
movslq 0x1924(%rsp), %rcx
movl %ecx, 0x191c(%rbx)
movslq 0x1928(%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 0x18a0(%rsp), %eax
cmpl $0x40, %eax
je 0x20ff9
xorl $0x38, %eax
movq 0x1938(%rbx), %rcx
movl %eax, 0x20(%rcx)
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 0x397f58(%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 0x21071
xorl %r14d, %r14d
xorq (%r9,%r14,8), %r8
incq %r14
cmpq %r14, %r11
jne 0x21065
incq %r10
addq $0x200, %r9             # imm = 0x200
cmpq $0x6, %r10
jne 0x21059
movl $0x1, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0x2103b
movq %r8, 0x8(%rcx)
movq %rbx, %rdi
callq 0x1d7d4
movq 0x1938(%rbx), %rcx
movl %eax, 0x24(%rcx)
movdqu 0x39b17a(%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 0x39b133(%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)
addq $0x1948, %rsp           # imm = 0x1948
popq %rbx
popq %r14
popq %r15
popq %rbp
retq