// File: /workspace/github_cpp_makefile_star2_O3/jswaff[P]prophet3/src/pos.cpp
// Function: set_pos(position*, char const*)
bool set_pos(position *pos,const char *fen) {
    char *my_fen = strdup(fen),*fen_part;
    int32 sq;

    for (sq=A8;sq<=H1;sq++) {
        pos->piece[sq] = NO_PIECE;
    }
    sq = A8;

    for (int32 i=NO_PIECE;i<=KING;i++) {
        pos->piece_counts[WHITE][i] = 0;
        pos->piece_counts[BLACK][i] = 0;
    }

    pos->white_pawns = pos->white_knights = pos->white_bishops = pos->white_rooks
            = pos->white_queens = pos->white_pieces = 0;
    pos->black_pawns = pos->black_knights = pos->black_bishops = pos->black_rooks
            = pos->black_queens = pos->black_pieces = 0;

    // Part 1 - Piece Placement
    fen_part = strtok(my_fen," ");
    for (uint32 c=0;c<strlen(fen_part);c++) {
        char ch = *(fen_part+c);
        if (ch == '/') continue;
        if (ch >= '1' && ch <= '8') {
            sq += ((int)ch)-48;  // '1' == ASC 48, '8' == ASC 56
        } else {
            int32 piece=NO_PIECE;
            if (ch=='K') {
                pos->white_king = (square_t)sq;
                piece = KING;
            } else if (ch=='k') {
                pos->black_king = (square_t)sq;
                piece = -KING;
            } else if (ch=='Q') {
                piece = QUEEN;
            } else if (ch=='q') {
                piece = -QUEEN;
            } else if (ch=='R') {
                piece = ROOK;
            } else if (ch=='r') {
                piece = -ROOK;
            } else if (ch=='B') {
                piece = BISHOP;
            } else if (ch=='b') {
                piece = -BISHOP;
            } else if (ch=='N') {
                piece = KNIGHT;
            } else if (ch=='n') {
                piece = -KNIGHT;
            } else if (ch=='P') {
                piece = PAWN;
            } else if (ch=='p') {
                piece = -PAWN;
            }
            assert(piece != NO_PIECE);
            add_piece(pos,piece,(square_t)sq);
            sq++;
        }
    }
    assert(sq==64);
    // Part 2 - Active Color
    fen_part = strtok(NULL," ");
    if (*fen_part=='w' || *fen_part=='W') {
        pos->player = WHITE;
    } else if (*fen_part=='b' || *fen_part=='B') {
        pos->player = BLACK;
    } else {
        error("active color - fen: %s\n",fen);
        return false;
    }

    // Part 3 - Castling Availability
    fen_part = strtok(NULL," ");
    if (!fen_part) {
        error("castling availability - fen: %s\n",fen);
        return false;
    }
    pos->castling_rights = 0;
    if (strchr(fen_part,'K'))
        pos->castling_rights |= CASTLE_WK;
    if (strchr(fen_part,'Q'))
        pos->castling_rights |= CASTLE_WQ;
    if (strchr(fen_part,'k'))
        pos->castling_rights |= CASTLE_BK;
    if (strchr(fen_part,'q'))
        pos->castling_rights |= CASTLE_BQ;

    // Part 4 - En Passant Target Square
    fen_part = strtok(NULL," ");
    if (!fen_part) {
        error("en passant target square - fen: %s\n",fen);
        return false;
    }
    pos->ep_sq = str_to_sq(fen_part);

    // Part 5 - Halfmove clock (Note: not part of the EPD spec)
    fen_part = strtok(NULL," ");
    if (fen_part) {
        pos->fifty_counter=atoi(fen_part);
    } else {
        pos->fifty_counter=0;
    }

    // Part 6 - Full Move Counter (Note: not part of the EPD spec)
    // It starts at 1, and is incremented after each Black's move.
    fen_part = strtok(NULL," ");
    if (fen_part) {
        pos->move_counter=(atoi(fen_part)-1)*2;
    } else {
        pos->move_counter=0;
    }
    if (pos->player==BLACK) pos->move_counter++;

    pos->hash_key = build_hash_key(pos);
    pos->pawn_key = build_pawn_key(pos);

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq %rsi, (%rsp)
movq %rsi, %rdi
callq 0x2310
movq %rax, %r12
xorl %r15d, %r15d
movl $0x11c, %edx            # imm = 0x11C
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
xorps %xmm0, %xmm0
movups %xmm0, 0x11c(%rbx)
movq $0x0, 0x12c(%rbx)
movl $0x0, 0x134(%rbx)
movups %xmm0, 0x158(%rbx)
movups %xmm0, 0x168(%rbx)
movups %xmm0, 0x178(%rbx)
movups %xmm0, 0x188(%rbx)
movups %xmm0, 0x198(%rbx)
movups %xmm0, 0x1a8(%rbx)
leaq 0x21cd1(%rip), %rsi     # 0x2c7d2
movq %r12, %rdi
callq 0x22c0
cmpb $0x0, (%rax)
je 0xadce
movq %rax, %r12
movl $0x1, %ebp
leaq 0x20c2b(%rip), %r14     # 0x2b74c
xorl %r13d, %r13d
movzbl (%r12,%r13), %eax
cmpl $0x2f, %eax
je 0xabf7
leal -0x31(%rax), %ecx
cmpb $0x7, %cl
ja 0xab44
addb $-0x30, %al
movzbl %al, %eax
jmp 0xabf4
cmpl $0x61, %eax
jg 0xab6f
leal -0x4b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xab8e
leaq 0x20bd4(%rip), %rdx     # 0x2b72c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl %r15d, 0x14c(%rbx)
movl $0x6, %esi
jmp 0xabe4
leal -0x6b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xab9e
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl %r15d, 0x150(%rbx)
movl $0xfffffffa, %esi       # imm = 0xFFFFFFFA
jmp 0xabe4
cmpl $0x42, %eax
jne 0xaded
movl $0x3, %esi
jmp 0xabe4
cmpl $0x62, %eax
jne 0xaded
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
jmp 0xabe4
movl $0x2, %esi
jmp 0xabe4
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
jmp 0xabe4
movl $0x5, %esi
jmp 0xabe4
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
jmp 0xabe4
movl $0x1, %esi
jmp 0xabe4
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0xabe4
movl $0x4, %esi
jmp 0xabe4
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
movq %rbx, %rdi
movl %r15d, %edx
callq 0xa5f8
movl $0x1, %eax
addl %eax, %r15d
movl %ebp, %r13d
movq %r12, %rdi
callq 0x2080
incl %ebp
cmpq %r13, %rax
ja 0xab24
cmpl $0x40, %r15d
jne 0xadce
leaq 0x21bb4(%rip), %rsi     # 0x2c7d2
xorl %edi, %edi
callq 0x22c0
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl $0x61, %ecx
jg 0xac41
cmpl $0x42, %ecx
je 0xac4f
cmpl $0x57, %ecx
je 0xac51
jmp 0xada0
cmpl $0x77, %ecx
je 0xac51
cmpl $0x62, %ecx
jne 0xada0
xorl %eax, %eax
movl %eax, 0x138(%rbx)
leaq 0x21b74(%rip), %rsi     # 0x2c7d2
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad97
movq %rax, %r15
movl $0x0, 0x140(%rbx)
movq %rax, %rdi
movl $0x4b, %esi
callq 0x2070
xorl %ebp, %ebp
testq %rax, %rax
setne %bpl
movl %ebp, 0x140(%rbx)
movq %r15, %rdi
movl $0x51, %esi
callq 0x2070
testq %rax, %rax
je 0xacb2
orl $0x2, %ebp
movl %ebp, 0x140(%rbx)
movq %r15, %rdi
movl $0x6b, %esi
callq 0x2070
testq %rax, %rax
je 0xaccd
orl $0x4, %ebp
movl %ebp, 0x140(%rbx)
movq %r15, %rdi
movl $0x71, %esi
callq 0x2070
testq %rax, %rax
je 0xace8
orl $0x8, %ebp
movl %ebp, 0x140(%rbx)
leaq 0x21ae3(%rip), %rsi     # 0x2c7d2
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xada9
movq %rax, %rdi
callq 0xdf7d
movl %eax, 0x13c(%rbx)
leaq 0x21abb(%rip), %rsi     # 0x2c7d2
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad2e
movq %rax, %rdi
callq 0x22b0
movl %eax, %r15d
movl %r15d, 0x148(%rbx)
leaq 0x21a96(%rip), %rsi     # 0x2c7d2
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad5b
movq %rax, %rdi
callq 0x22b0
leal -0x2(,%rax,2), %r14d
movl %r14d, 0x144(%rbx)
cmpl $0x0, 0x138(%rbx)
jne 0xad75
incl %r14d
movl %r14d, 0x144(%rbx)
movq %rbx, %rdi
callq 0x5ee8
movq %rax, 0x1b8(%rbx)
movq %rbx, %rdi
callq 0x5fb7
movq %rax, 0x1c0(%rbx)
movb $0x1, %bl
jmp 0xadbd
leaq 0x21062(%rip), %rdi     # 0x2be00
jmp 0xadb0
leaq 0x21041(%rip), %rdi     # 0x2bde8
jmp 0xadb0
leaq 0x21071(%rip), %rdi     # 0x2be21
xorl %ebx, %ebx
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x2330
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2100c(%rip), %rdi     # 0x2bde1
leaq 0x20ab5(%rip), %rsi     # 0x2b891
leaq 0x20fd7(%rip), %rcx     # 0x2bdba
movl $0x13b, %edx            # imm = 0x13B
callq 0x20d0
leaq 0x1fbfd(%rip), %rdi     # 0x2a9f1
leaq 0x20a96(%rip), %rsi     # 0x2b891
leaq 0x20fb8(%rip), %rcx     # 0x2bdba
movl $0x136, %edx            # imm = 0x136
callq 0x20d0