// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movups %xmm0, 0x30(%rsi)
movq 0x20(%rsi), %rax
movq %rax, -0x38(%rbp)
movq 0x28(%rsi), %rdx
movq 0x10(%rsi), %rax
movq %rax, -0x30(%rbp)
movq 0x18(%rsi), %r8
movq 0x8(%rsi), %r9
movq 0x38(%rsi), %r10
movl $0x3, %eax
xorl %r11d, %r11d
leaq 0x5da3(%rip), %r12      # 0xcbe0
xorl %r13d, %r13d
xorl %r15d, %r15d
movzbl -0x3(%rdi,%rax), %esi
leal -0x31(%rsi), %r14d
cmpl $0x21, %r14d
ja 0x6e6c
movslq (%r12,%r14,4), %r14
addq %r12, %r14
jmpq *%r14
movsbq %sil, %rsi
addq %rsi, %r11
addq $-0x30, %r11
jmp 0x6fc0
leal -0x6b(%rsi), %r14d
cmpl $0x7, %r14d
ja 0x6e9e
leaq 0x5deb(%rip), %rcx      # 0xcc68
movslq (%rcx,%r14,4), %rsi
addq %rcx, %rsi
jmpq *%rsi
leaq 0x5e03(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %rdx
movq %rdx, 0x28(%rbx)
jmp 0x6fb5
cmpl $0x62, %esi
jne 0x6fc0
leaq 0x5de2(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
movq -0x30(%rbp), %r14
btsq %rsi, %r14
movq %r14, -0x30(%rbp)
movq %r14, 0x10(%rbx)
jmp 0x6fb5
leaq 0x5dc2(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %rdx
movq %rdx, 0x28(%rbx)
jmp 0x6f52
leaq 0x5dad(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r8
movq %r8, 0x18(%rbx)
jmp 0x6f52
leaq 0x5d98(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r9
movq %r9, 0x8(%rbx)
jmp 0x6f52
leaq 0x5d83(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
movq -0x30(%rbp), %r14
btsq %rsi, %r14
movq %r14, -0x30(%rbp)
movq %r14, 0x10(%rbx)
jmp 0x6f52
leaq 0x5d66(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
movq -0x38(%rbp), %rcx
btsq %rsi, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, 0x20(%rbx)
jmp 0x6f52
leaq 0x5d49(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r15
movq %r15, (%rbx)
btsq %rsi, %r13
movq %r13, 0x30(%rbx)
jmp 0x6fbd
leaq 0x5d2d(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r8
movq %r8, 0x18(%rbx)
jmp 0x6fb5
leaq 0x5d18(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
movq -0x38(%rbp), %rcx
btsq %rsi, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, 0x20(%rbx)
jmp 0x6fb5
leaq 0x5cfb(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r9
movq %r9, 0x8(%rbx)
jmp 0x6fb5
leaq 0x5ce6(%rip), %rsi      # 0xcc90
movb (%r11,%rsi), %sil
btsq %rsi, %r15
movq %r15, (%rbx)
btsq %rsi, %r10
movq %r10, 0x38(%rbx)
incq %r11
incq %rax
cmpq $0x40, %r11
jb 0x6e43
movb -0x2(%rdi,%rax), %cl
movb $0x0, 0x40(%rbx)
xorl %edx, %edx
leaq 0x1(%rax), %r8
movb (%rdi,%rax), %sil
movzbl %sil, %r9d
cmpl $0x50, %r9d
jle 0x7000
cmpl $0x51, %r9d
je 0x7011
cmpl $0x6b, %r9d
je 0x7016
cmpl $0x71, %r9d
jne 0x701c
orb $0x8, %dl
jmp 0x7019
cmpl $0x2d, %r9d
je 0x7027
cmpl $0x4b, %r9d
jne 0x701c
orb $0x1, %dl
jmp 0x7019
orb $0x2, %dl
jmp 0x7019
orb $0x4, %dl
movb %dl, 0x40(%rbx)
movq %r8, %rax
cmpb $0x20, %sil
jne 0x6fd7
jmp 0x7032
movb 0x1(%rdi,%rax), %sil
addq $0x2, %rax
jmp 0x701f
movb (%rdi,%rax), %dl
cmpb $0x2d, %dl
jne 0x7043
movl $0x2, %esi
movb $0x40, %dl
jmp 0x7057
movb 0x1(%rdi,%rax), %sil
shlb $0x3, %sil
addb %sil, %dl
addb $0x17, %dl
movl $0x3, %esi
movb %dl, 0x42(%rbx)
addq %rax, %rdi
movb 0x1(%rsi,%rdi), %al
movzbl (%rsi,%rdi), %edx
leal -0x30(%rdx), %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %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 0x70f9
movq $-0x8, %rax
movq 0x8(%rbx,%rax), %rcx
bswapq %rcx
movq %rcx, 0x8(%rbx,%rax)
addq $0x8, %rax
cmpq $0x28, %rax
jb 0x7091
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rax, 0x38(%rbx)
movq %rcx, 0x30(%rbx)
movl $0x28, %eax
movq 0x8(%rbx,%rax), %rcx
bswapq %rcx
movq %rcx, 0x8(%rbx,%rax)
addq $0x8, %rax
cmpq $0x38, %rax
jb 0x70bd
movb 0x42(%rbx), %al
cmpb $0x40, %al
je 0x70e0
xorb $0x38, %al
movb %al, 0x42(%rbx)
movb 0x40(%rbx), %al
leal (,%rax,4), %ecx
andb $0xc, %cl
shrb $0x2, %al
xorb %cl, %al
movb %al, 0x40(%rbx)
movb $0x1, 0x41(%rbx)
movq %rbx, %rdi
callq 0x6c3a
leaq 0x50(%rbx), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
callq 0x768e
movq 0x50(%rbx), %rax
cmpq %rax, 0x58(%rbx)
je 0x7120
movq %rax, 0x58(%rbx)
movq 0x58(%rbx), %rsi
cmpq 0x60(%rbx), %rsi
je 0x7148
movq 0x48(%rbx), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x48, %rbx
movq %r14, %rdi
movq %rbx, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x57ca