// File: jswaff[P]prophet3/src/pos.cpp
// Function: flip_board(position*)
void flip_board(position *pos) {

    // flip pieces around
    int32 orig_sq[64];
    for (int sq=0;sq<64;sq++) {
        orig_sq[sq] = pos->piece[sq];
        if (pos->piece[sq] != NO_PIECE) {
            remove_piece(pos,(square_t)sq);
        }
    }

    for (int sq=0;sq<64;sq++) {
        if (orig_sq[sq] != NO_PIECE) {
            add_piece(pos,-orig_sq[sq],(square_t)fliprank[sq]);
        }
    }

    square_t orig_white_king_sq = pos->white_king;
    pos->white_king = (square_t)fliprank[pos->black_king];
    pos->black_king = (square_t)fliprank[orig_white_king_sq];

    // swap players
    pos->player = opposite_player(pos->player);

    // flip ep square
    if (pos->ep_sq != NO_SQUARE) {
        pos->ep_sq = (square_t)fliprank[pos->ep_sq];
    }

    // castling rights
    int32 orig_castling_rights = pos->castling_rights;
    pos->castling_rights = 0;
    if (orig_castling_rights & CASTLE_WK) {
        pos->castling_rights |= CASTLE_BK;
    }
    if (orig_castling_rights & CASTLE_WQ) {
        pos->castling_rights |= CASTLE_BQ;
    }
    if (orig_castling_rights & CASTLE_BK) {
        pos->castling_rights |= CASTLE_WK;
    }
    if (orig_castling_rights & CASTLE_BQ) {
        pos->castling_rights |= CASTLE_WQ;
    }

    pos->hash_key = build_hash_key(pos);
    pos->pawn_key = build_pawn_key(pos);
    assert(test_pos_consistency(pos));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movl $0x0, -0x114(%rbp)
cmpl $0x40, -0x114(%rbp)
jge 0xfcf1
movq -0x8(%rbp), %rax
movslq -0x114(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movslq -0x114(%rbp), %rax
movl %ecx, -0x110(%rbp,%rax,4)
movq -0x8(%rbp), %rax
movslq -0x114(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0xfcde
movq -0x8(%rbp), %rdi
movl -0x114(%rbp), %esi
callq 0xee40
jmp 0xfce0
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0xfc99
movl $0x0, -0x118(%rbp)
cmpl $0x40, -0x118(%rbp)
jge 0xfd52
movslq -0x118(%rbp), %rax
cmpl $0x0, -0x110(%rbp,%rax,4)
je 0xfd3f
movq -0x8(%rbp), %rdi
movslq -0x118(%rbp), %rax
xorl %esi, %esi
subl -0x110(%rbp,%rax,4), %esi
movslq -0x118(%rbp), %rcx
leaq 0x28049(%rip), %rax     # 0x37d80
movl (%rax,%rcx,4), %edx
callq 0xeb20
jmp 0xfd41
movl -0x118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x118(%rbp)
jmp 0xfcfb
movq -0x8(%rbp), %rax
movl 0x14c(%rax), %eax
movl %eax, -0x11c(%rbp)
movq -0x8(%rbp), %rax
movl 0x150(%rax), %eax
movl %eax, %ecx
leaq 0x2800b(%rip), %rax     # 0x37d80
movl (%rax,%rcx,4), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x14c(%rax)
movl -0x11c(%rbp), %eax
movl %eax, %ecx
leaq 0x27fef(%rip), %rax     # 0x37d80
movl (%rax,%rcx,4), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x150(%rax)
movq -0x8(%rbp), %rax
movl 0x138(%rax), %edi
callq 0x14130
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x138(%rax)
movq -0x8(%rbp), %rax
cmpl $0x40, 0x13c(%rax)
je 0xfde6
movq -0x8(%rbp), %rax
movl 0x13c(%rax), %eax
movl %eax, %ecx
leaq 0x27fa7(%rip), %rax     # 0x37d80
movl (%rax,%rcx,4), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x13c(%rax)
movq -0x8(%rbp), %rax
movl 0x140(%rax), %eax
movl %eax, -0x120(%rbp)
movq -0x8(%rbp), %rax
movl $0x0, 0x140(%rax)
movl -0x120(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xfe25
movq -0x8(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x140(%rax)
movl -0x120(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xfe46
movq -0x8(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x140(%rax)
movl -0x120(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xfe67
movq -0x8(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x140(%rax)
movl -0x120(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xfe88
movq -0x8(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x140(%rax)
movq -0x8(%rbp), %rdi
callq 0x81f0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x1b8(%rax)
movq -0x8(%rbp), %rdi
callq 0x83a0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x1c0(%rax)
movq -0x8(%rbp), %rdi
callq 0x2e0c0
testb $0x1, %al
jne 0xfec5
jmp 0xfec7
jmp 0xfee6
leaq 0x27e3d(%rip), %rdi     # 0x37d0b
leaq 0x277a5(%rip), %rsi     # 0x3767a
movl $0x1c4, %edx            # imm = 0x1C4
leaq 0x27e44(%rip), %rcx     # 0x37d25
callq 0x20c0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop