// File: /workspace/github_cpp_makefile_star2_O2/cpirc[P]Monochrome/src/position.cpp
// Function: parse_fen_to_position(char const*, Position&)
void parse_fen_to_position(const char *fen_str, Position &pos) {
    std::size_t i = 0, square_idx = 0;
    char c;

    std::memset((void *)&pos.pieces, 0, sizeof(pos.pieces));
    std::memset((void *)&pos.colours, 0, sizeof(pos.colours));

    while (square_idx < ARR_LEN(fen_board)) {
        c = fen_str[i++];

        switch (c) {
            case 'p':
                put_piece(pos, (const Square)fen_board[square_idx], PAWN, THEM);
                square_idx++;
                break;
            case 'r':
                put_piece(pos, (const Square)fen_board[square_idx], ROOK, THEM);
                square_idx++;
                break;
            case 'n':
                put_piece(pos, (const Square)fen_board[square_idx], KNIGHT,
                          THEM);
                square_idx++;
                break;
            case 'b':
                put_piece(pos, (const Square)fen_board[square_idx], BISHOP,
                          THEM);
                square_idx++;
                break;
            case 'q':
                put_piece(pos, (const Square)fen_board[square_idx], QUEEN,
                          THEM);
                square_idx++;
                break;
            case 'k':
                put_piece(pos, (const Square)fen_board[square_idx], KING, THEM);
                square_idx++;
                break;
            case 'P':
                put_piece(pos, (const Square)fen_board[square_idx], PAWN, US);
                square_idx++;
                break;
            case 'R':
                put_piece(pos, (const Square)fen_board[square_idx], ROOK, US);
                square_idx++;
                break;
            case 'N':
                put_piece(pos, (const Square)fen_board[square_idx], KNIGHT, US);
                square_idx++;
                break;
            case 'B':
                put_piece(pos, (const Square)fen_board[square_idx], BISHOP, US);
                square_idx++;
                break;
            case 'Q':
                put_piece(pos, (const Square)fen_board[square_idx], QUEEN, US);
                square_idx++;
                break;
            case 'K':
                put_piece(pos, (const Square)fen_board[square_idx], KING, US);
                square_idx++;
                break;
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
                square_idx += (c - '0');
                break;
        }
    }

    c = fen_str[++i];
    bool flipped = c == 'b';

    i += 2;
    pos.castle = 0;
    do {
        c = fen_str[i++];

        switch (c) {
            case 'K':
                pos.castle |= US_OO;
                break;
            case 'Q':
                pos.castle |= US_OOO;
                break;
            case 'k':
                pos.castle |= THEM_OO;
                break;
            case 'q':
                pos.castle |= THEM_OOO;
                break;
            case '-':
                c = fen_str[i++];
                break;
        }

    } while (c != ' ');

    switch (fen_str[i]) {
        case '-':
            pos.epsq = INVALID_SQUARE;
            i += 2;
            break;
        default:
            pos.epsq =
                (Square)((fen_str[i] - 'a') + ((fen_str[i + 1] - '1') * 8));
            i += 3;
            break;
    }

    if (fen_str[i + 1] != ' ') {
        pos.halfmoves = (fen_str[i] - '0') * 10 + (fen_str[i + 1] - '0');
    } else {
        pos.halfmoves = fen_str[i] - '0';
    }

    pos.flipped = false;
    if (flipped) flip_position(pos);

    calculate_key(pos);

    pos.history.reserve(256);
    pos.history.clear();
    pos.history.push_back(pos.hash_key);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
pushq $0x3
popq %rax
xorl %ecx, %ecx
leaq 0x571f(%rip), %r8       # 0xaac0
xorl %r9d, %r9d
xorl %r10d, %r10d
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %esi, %esi
cmpq $0x3f, %rsi
ja 0x557e
movzbl -0x3(%rdi,%rax), %r11d
leal -0x31(%r11), %edx
cmpl $0x21, %edx
ja 0x53eb
movslq (%r8,%rdx,4), %rdx
addq %r8, %rdx
jmpq *%rdx
addq %r11, %rsi
addq $-0x30, %rsi
jmp 0x5576
leal -0x6b(%r11), %edx
cmpl $0x7, %edx
ja 0x541b
leaq 0x574d(%rip), %r11      # 0xab48
movslq (%r11,%rdx,4), %rdx
addq %r11, %rdx
jmpq *%rdx
leaq 0x5765(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r15
movq %r15, 0x28(%rbx)
jmp 0x556b
cmpl $0x62, %r11d
jne 0x5576
leaq 0x5744(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
movq -0x30(%rbp), %r11
movq %r12, %r8
movq %r15, %r12
movq %r14, %r15
movq %r10, %r14
movq %r9, %r10
movq %r11, %r9
btsq %rdx, %r9
movq %r9, -0x30(%rbp)
movq %r9, 0x10(%rbx)
movq %r10, %r9
movq %r14, %r10
movq %r15, %r14
movq %r12, %r15
movq %r8, %r12
leaq 0x5659(%rip), %r8       # 0xaac0
jmp 0x556b
leaq 0x56fd(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r15
movq %r15, 0x28(%rbx)
jmp 0x5514
leaq 0x56e6(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r9
movq %r9, 0x18(%rbx)
jmp 0x5514
leaq 0x56d2(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r10
movq %r10, 0x8(%rbx)
jmp 0x5514
leaq 0x56be(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
movq -0x30(%rbp), %r11
movq %r12, %r8
movq %r15, %r12
movq %r14, %r15
movq %r10, %r14
movq %r9, %r10
movq %r11, %r9
btsq %rdx, %r9
movq %r9, -0x30(%rbp)
movq %r9, 0x10(%rbx)
movq %r10, %r9
movq %r14, %r10
movq %r15, %r14
movq %r12, %r15
movq %r8, %r12
leaq 0x55d3(%rip), %r8       # 0xaac0
jmp 0x5514
leaq 0x567a(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r14
movq %r14, 0x20(%rbx)
jmp 0x5514
leaq 0x5666(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r12
movq %r12, (%rbx)
btsq %rdx, %r13
movq %r13, 0x30(%rbx)
jmp 0x5573
leaq 0x564b(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r9
movq %r9, 0x18(%rbx)
jmp 0x556b
leaq 0x5637(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r14
movq %r14, 0x20(%rbx)
jmp 0x556b
leaq 0x5623(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r10
movq %r10, 0x8(%rbx)
jmp 0x556b
leaq 0x560f(%rip), %rdx      # 0xab70
movb (%rsi,%rdx), %dl
btsq %rdx, %r12
movq %r12, (%rbx)
btsq %rdx, %rcx
movq %rcx, 0x38(%rbx)
incq %rsi
incq %rax
jmp 0x53bd
movb -0x2(%rdi,%rax), %cl
movb $0x0, 0x40(%rbx)
xorl %edx, %edx
movzbl (%rdi,%rax), %esi
cmpl $0x2d, %esi
je 0x55d4
incq %rax
cmpl $0x71, %esi
je 0x55b3
cmpl $0x51, %esi
je 0x55c9
cmpl $0x6b, %esi
je 0x55be
cmpl $0x4b, %esi
jne 0x55dd
orb $0x1, %dl
movb %dl, 0x40(%rbx)
movb $0x4b, %sil
jmp 0x55dd
orb $0x8, %dl
movb %dl, 0x40(%rbx)
movb $0x71, %sil
jmp 0x55dd
orb $0x4, %dl
movb %dl, 0x40(%rbx)
movb $0x6b, %sil
jmp 0x55dd
orb $0x2, %dl
movb %dl, 0x40(%rbx)
movb $0x51, %sil
jmp 0x55dd
movb 0x1(%rdi,%rax), %sil
addq $0x2, %rax
cmpb $0x20, %sil
jne 0x5588
movb (%rdi,%rax), %dl
cmpb $0x2d, %dl
jne 0x55f1
movb $0x40, %dl
pushq $0x2
jmp 0x5602
movb 0x1(%rdi,%rax), %sil
shlb $0x3, %sil
addb %sil, %dl
addb $0x17, %dl
pushq $0x3
popq %rsi
movb %dl, 0x42(%rbx)
addq %rax, %rdi
movb 0x1(%rsi,%rdi), %al
movzbl (%rsi,%rdi), %edx
leal -0x30(%rdx), %esi
imull $0xa, %edx, %edx
addb %al, %dl
addb $-0x10, %dl
cmpb $0x20, %al
movzbl %dl, %eax
movzbl %sil, %edx
cmovnel %eax, %edx
movb %dl, 0x43(%rbx)
movb $0x0, 0x41(%rbx)
cmpb $0x62, %cl
jne 0x563c
movq %rbx, %rdi
callq 0x2c63
movq %rbx, %rdi
callq 0x528e
leaq 0x50(%rbx), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
callq 0x5b28
movq 0x50(%rbx), %rax
cmpq %rax, 0x58(%rbx)
je 0x5663
movq %rax, 0x58(%rbx)
addq $0x48, %rbx
movq %r14, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40aa