// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa0, %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 0x1ccf2
movl %ebp, 0x192c(%rbx)
movl 0x8(%rsp,%r15,4), %esi
cmpl $0x10, %esi
je 0x20a3b
xorl $0x8, %esi
movl %r15d, %edx
xorl $0x38, %edx
movq %rbx, %rdi
callq 0x1cdea
incq %r15
cmpq $0x40, %r15
jne 0x20a20
movl 0x1098(%rsp), %eax
xorl $0x1, %eax
movl %eax, 0x1090(%rbx)
testb $0x1, 0x1890(%rsp)
je 0x20a69
movq 0x1938(%rbx), %rax
orl $0x2, 0x10(%rax)
testb $0x4, 0x1890(%rsp)
je 0x20a7e
movq 0x1938(%rbx), %rax
orl $0x8, 0x10(%rax)
testb $0x2, 0x1890(%rsp)
je 0x20a93
movq 0x1938(%rbx), %rax
orl $0x1, 0x10(%rax)
testb $0x8, 0x1890(%rsp)
je 0x20aa8
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 0x20b33
xorl $0x38, %eax
movq 0x1938(%rbx), %rcx
movl %eax, 0x20(%rcx)
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 0x397622(%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 0x20b9d
xorl %r14d, %r14d
xorq (%r10,%r14,8), %rdi
incq %r14
cmpq %r14, %r11
jne 0x20b91
incq %r8
addq $0x200, %r10            # imm = 0x200
cmpq $0x6, %r8
jne 0x20b85
leaq 0x1(%rsi), %r8
addq $0x1000, %rdx           # imm = 0x1000
testq %rsi, %rsi
movq %r8, %rsi
je 0x20b72
movq %rdi, 0x8(%rcx)
movq %rbx, %rdi
callq 0x1d292
movq 0x1938(%rbx), %rcx
movl %eax, 0x24(%rcx)
movdqu 0x39a64a(%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 0x39a603(%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)
addq $0x1948, %rsp           # imm = 0x1948
popq %rbx
popq %r14
popq %r15
popq %rbp
retq