// File: /workspace/github_cpp_makefile_star2_O3/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)
movl $0x3, %eax
xorl %ecx, %ecx
leaq 0x5ff5(%rip), %r8       # 0xcb20
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
movzbl -0x3(%rdi,%rax), %r11d
leal -0x31(%r11), %edx
cmpl $0x21, %edx
ja 0x6b6b
movslq (%r8,%rdx,4), %rdx
addq %r8, %rdx
jmpq *%rdx
addq %r11, %r12
addq $-0x30, %r12
jmp 0x6d05
leal -0x6b(%r11), %edx
cmpl $0x7, %edx
ja 0x6b9c
leaq 0x602d(%rip), %r11      # 0xcba8
movslq (%r11,%rdx,4), %rdx
addq %r11, %rdx
jmpq *%rdx
leaq 0x6045(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r15
movq %r15, 0x28(%rbx)
jmp 0x6cfa
cmpl $0x62, %r11d
jne 0x6d05
leaq 0x6023(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
movq -0x30(%rbp), %r11
movq %rsi, %r8
movq %r15, %rsi
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 %rsi, %r15
movq %r8, %rsi
leaq 0x5f37(%rip), %r8       # 0xcb20
jmp 0x6cfa
leaq 0x5fdb(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r15
movq %r15, 0x28(%rbx)
jmp 0x6c9f
leaq 0x5fc3(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r9
movq %r9, 0x18(%rbx)
jmp 0x6c9f
leaq 0x5fab(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r10
movq %r10, 0x8(%rbx)
jmp 0x6c9f
leaq 0x5f96(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
movq -0x30(%rbp), %r11
movq %rsi, %r8
movq %r15, %rsi
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 %rsi, %r15
movq %r8, %rsi
leaq 0x5eaa(%rip), %r8       # 0xcb20
jmp 0x6c9f
leaq 0x5f51(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r14
movq %r14, 0x20(%rbx)
jmp 0x6c9f
leaq 0x5f3c(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %rsi
movq %rsi, (%rbx)
btsq %rdx, %r13
movq %r13, 0x30(%rbx)
jmp 0x6d02
leaq 0x5f20(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r9
movq %r9, 0x18(%rbx)
jmp 0x6cfa
leaq 0x5f0b(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r14
movq %r14, 0x20(%rbx)
jmp 0x6cfa
leaq 0x5ef6(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %r10
movq %r10, 0x8(%rbx)
jmp 0x6cfa
leaq 0x5ee1(%rip), %rdx      # 0xcbd0
movb (%r12,%rdx), %dl
btsq %rdx, %rsi
movq %rsi, (%rbx)
btsq %rdx, %rcx
movq %rcx, 0x38(%rbx)
incq %r12
incq %rax
cmpq $0x40, %r12
jb 0x6b47
movb -0x2(%rdi,%rax), %dl
movb $0x0, 0x40(%rbx)
xorl %ecx, %ecx
leaq 0x1(%rax), %r8
movb (%rdi,%rax), %sil
movzbl %sil, %r9d
cmpl $0x50, %r9d
jle 0x6d4b
cmpl $0x51, %r9d
je 0x6d62
cmpl $0x6b, %r9d
je 0x6d6d
cmpl $0x71, %r9d
jne 0x6d76
orb $0x8, %cl
movb %cl, 0x40(%rbx)
movb $0x71, %sil
jmp 0x6d76
cmpl $0x2d, %r9d
je 0x6d81
cmpl $0x4b, %r9d
jne 0x6d76
orb $0x1, %cl
movb %cl, 0x40(%rbx)
movb $0x4b, %sil
jmp 0x6d76
orb $0x2, %cl
movb %cl, 0x40(%rbx)
movb $0x51, %sil
jmp 0x6d76
orb $0x4, %cl
movb %cl, 0x40(%rbx)
movb $0x6b, %sil
movq %r8, %rax
cmpb $0x20, %sil
jne 0x6d1c
jmp 0x6d8c
movb 0x1(%rdi,%rax), %sil
addq $0x2, %rax
jmp 0x6d79
movb (%rdi,%rax), %sil
cmpb $0x2d, %sil
jne 0x6da1
movb $0x40, %sil
movl $0x2, %r8d
jmp 0x6db7
movb 0x1(%rdi,%rax), %r8b
shlb $0x3, %r8b
addb %r8b, %sil
addb $0x17, %sil
movl $0x3, %r8d
movb %sil, 0x42(%rbx)
addq %rax, %rdi
movb 0x1(%r8,%rdi), %al
movzbl (%r8,%rdi), %edi
leal -0x30(%rdi), %r8d
addl %edi, %edi
leal (%rdi,%rdi,4), %edi
addb %al, %dil
addb $-0x10, %dil
cmpb $0x20, %al
movzbl %dil, %eax
movzbl %r8b, %edi
cmovnel %eax, %edi
movb %dil, 0x43(%rbx)
movb $0x0, 0x41(%rbx)
cmpb $0x62, %dl
jne 0x6e5f
movq $-0x8, %rax
movq 0x8(%rbx,%rax), %rdx
bswapq %rdx
movq %rdx, 0x8(%rbx,%rax)
addq $0x8, %rax
cmpq $0x28, %rax
jb 0x6df9
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rdx
movq %rax, 0x38(%rbx)
movq %rdx, 0x30(%rbx)
movl $0x28, %eax
movq 0x8(%rbx,%rax), %rdx
bswapq %rdx
movq %rdx, 0x8(%rbx,%rax)
addq $0x8, %rax
cmpq $0x38, %rax
jb 0x6e25
cmpb $0x40, %sil
je 0x6e4a
xorb $0x38, %sil
movb %sil, 0x42(%rbx)
leal (,%rcx,4), %eax
andb $0xc, %al
shrb $0x2, %cl
xorb %al, %cl
movb %cl, 0x40(%rbx)
movb $0x1, 0x41(%rbx)
movq %rbx, %rdi
callq 0x6952
leaq 0x50(%rbx), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
callq 0x73f6
movq 0x50(%rbx), %rax
movq 0x58(%rbx), %rsi
cmpq %rax, %rsi
je 0x6e8c
movq %rax, 0x58(%rbx)
movq %rax, %rsi
cmpq 0x60(%rbx), %rsi
je 0x6eb0
movq 0x48(%rbx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x48, %rbx
movq %r14, %rdi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x543c