// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %ecx, %ecx
cmpq $0x40, %rcx
je 0xa1e3
andl $0x0, (%rbx,%rcx,4)
incq %rcx
jmp 0xa1d4
pushq $0x40
popq %rcx
cmpq $0x47, %rcx
je 0xa1fa
andl $0x0, 0x1c(%rbx,%rcx,4)
andl $0x0, (%rbx,%rcx,4)
incq %rcx
jmp 0xa1e6
xorps %xmm0, %xmm0
movups %xmm0, 0x1a8(%rbx)
movups %xmm0, 0x198(%rbx)
movups %xmm0, 0x188(%rbx)
movups %xmm0, 0x178(%rbx)
movups %xmm0, 0x168(%rbx)
movups %xmm0, 0x158(%rbx)
leaq 0x20554(%rip), %rsi     # 0x2a782
movq %rax, %rdi
callq 0x22c0
movq %rax, %r15
xorl %ebp, %ebp
pushq $0x1
popq %r14
leaq 0x1f496(%rip), %r12     # 0x296dc
xorl %r13d, %r13d
movl %r13d, %r13d
movq %r15, %rdi
callq 0x2080
cmpq %r13, %rax
jbe 0xa303
movzbl (%r15,%r13), %eax
cmpl $0x2f, %eax
je 0xa2fb
leal -0x31(%rax), %ecx
cmpb $0x7, %cl
ja 0xa27a
addb $-0x30, %al
movzbl %al, %eax
jmp 0xa2f9
leal -0x4b(%rax), %ecx
cmpl $0x7, %ecx
jbe 0xa2a4
leal -0x6b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xa2ba
leaq 0x1f46b(%rip), %rdx     # 0x296fc
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl %ebp, 0x150(%rbx)
pushq $-0x6
jmp 0xa2eb
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
pushq $0x5
popq %rsi
jmpq *%rax
movl %ebp, 0x14c(%rbx)
pushq $0x6
jmp 0xa2eb
cmpl $0x42, %eax
je 0xa2e9
cmpl $0x62, %eax
jne 0xa4cb
pushq $-0x3
jmp 0xa2eb
pushq $0x4
jmp 0xa2eb
pushq $0x2
jmp 0xa2eb
movl %r14d, %esi
jmp 0xa2ec
pushq $-0x4
jmp 0xa2eb
pushq $-0x2
jmp 0xa2eb
pushq $-0x1
jmp 0xa2eb
pushq $-0x5
jmp 0xa2eb
pushq $0x3
popq %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x9db0
movl %r14d, %eax
addl %eax, %ebp
incl %r13d
jmp 0xa249
cmpl $0x40, %ebp
jne 0xa4ac
leaq 0x2046f(%rip), %rsi     # 0x2a782
xorl %edi, %edi
callq 0x22c0
movzbl (%rax), %ecx
cmpl $0x42, %ecx
je 0xa338
pushq $0x1
popq %rax
cmpl $0x57, %ecx
je 0xa33a
cmpl $0x77, %ecx
je 0xa33a
cmpl $0x62, %ecx
jne 0xa47e
xorl %eax, %eax
movl %eax, 0x138(%rbx)
leaq 0x2043b(%rip), %rsi     # 0x2a782
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xa475
movq %rax, %r15
andl $0x0, 0x140(%rbx)
pushq $0x4b
popq %rsi
movq %rax, %rdi
callq 0x2070
xorl %ebp, %ebp
testq %rax, %rax
setne %bpl
movl %ebp, 0x140(%rbx)
pushq $0x51
popq %rsi
movq %r15, %rdi
callq 0x2070
testq %rax, %rax
je 0xa394
orl $0x2, %ebp
movl %ebp, 0x140(%rbx)
pushq $0x6b
popq %rsi
movq %r15, %rdi
callq 0x2070
testq %rax, %rax
je 0xa3ad
orl $0x4, %ebp
movl %ebp, 0x140(%rbx)
pushq $0x71
popq %rsi
movq %r15, %rdi
callq 0x2070
testq %rax, %rax
je 0xa3c6
orl $0x8, %ebp
movl %ebp, 0x140(%rbx)
leaq 0x203b5(%rip), %rsi     # 0x2a782
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xa487
movq %rax, %rdi
callq 0xd4e1
movl %eax, 0x13c(%rbx)
leaq 0x2038d(%rip), %rsi     # 0x2a782
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xa40c
movq %rax, %rdi
callq 0x22b0
movl %eax, %r15d
movl %r15d, 0x148(%rbx)
leaq 0x20368(%rip), %rsi     # 0x2a782
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xa439
movq %rax, %rdi
callq 0x22b0
leal -0x2(,%rax,2), %r14d
movl %r14d, 0x144(%rbx)
cmpl $0x0, 0x138(%rbx)
jne 0xa453
incl %r14d
movl %r14d, 0x144(%rbx)
movq %rbx, %rdi
callq 0x5a50
movq %rax, 0x1b8(%rbx)
movq %rbx, %rdi
callq 0x5b17
movq %rax, 0x1c0(%rbx)
movb $0x1, %bl
jmp 0xa49b
leaq 0x1f934(%rip), %rdi     # 0x29db0
jmp 0xa48e
leaq 0x1f913(%rip), %rdi     # 0x29d98
jmp 0xa48e
leaq 0x1f943(%rip), %rdi     # 0x29dd1
xorl %ebx, %ebx
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x622a
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f8de(%rip), %rdi     # 0x29d91
leaq 0x1f387(%rip), %rsi     # 0x29841
leaq 0x1f8a9(%rip), %rcx     # 0x29d6a
movl $0x13b, %edx            # imm = 0x13B
callq 0x20c0
leaq 0x1e4cf(%rip), %rdi     # 0x289a1
leaq 0x1f368(%rip), %rsi     # 0x29841
leaq 0x1f88a(%rip), %rcx     # 0x29d6a
movl $0x136, %edx            # imm = 0x136
callq 0x20c0