// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20c0
xorps %xmm0, %xmm0
movups %xmm0, 0x100(%rbx)
movups %xmm0, 0x10c(%rbx)
movl $0x47, %eax
movl $0x0, (%rbx,%rax,4)
incq %rax
cmpq $0x4e, %rax
jne 0xaab6
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 0x21c58(%rip), %rsi     # 0x2c752
movq %r15, %rdi
callq 0x22c0
cmpb $0x0, (%rax)
je 0xade5
movq %rax, %r15
xorl %r13d, %r13d
movl $0x1, %r12d
leaq 0x20bae(%rip), %r14     # 0x2b6cc
xorl %ebp, %ebp
movzbl (%r15,%r13), %eax
cmpl $0x2f, %eax
je 0xabf7
leal -0x31(%rax), %ecx
cmpb $0x7, %cl
ja 0xab40
addb $-0x30, %al
movzbl %al, %eax
jmp 0xabf5
cmpl $0x61, %eax
jg 0xab6a
leal -0x4b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xab88
leaq 0x20b58(%rip), %rdx     # 0x2b6ac
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl %ebp, 0x14c(%rbx)
movl $0x6, %esi
jmp 0xabdc
leal -0x6b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xab94
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
movl %ebp, 0x150(%rbx)
movl $0xfffffffa, %esi       # imm = 0xFFFFFFFA
jmp 0xabdc
cmpl $0x42, %eax
jne 0xaba0
movl $0x3, %esi
jmp 0xabdc
cmpl $0x62, %eax
jne 0xaba0
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
jmp 0xabdc
xorl %esi, %esi
movb $0x1, %al
jmp 0xabde
movl $0x4, %esi
jmp 0xabdc
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
jmp 0xabdc
movl $0x5, %esi
jmp 0xabdc
movl $0xfffffffb, %esi       # imm = 0xFFFFFFFB
jmp 0xabdc
movl $0x2, %esi
jmp 0xabdc
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
jmp 0xabdc
movl $0x1, %esi
jmp 0xabdc
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
testb %al, %al
jne 0xadc6
movq %rbx, %rdi
movl %ebp, %edx
callq 0xa63d
movl $0x1, %eax
addl %eax, %ebp
movl %r12d, %r13d
movq %r15, %rdi
callq 0x2080
incl %r12d
cmpq %r13, %rax
ja 0xab20
cmpl $0x40, %ebp
jne 0xade5
leaq 0x21b34(%rip), %rsi     # 0x2c752
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 0xad98
cmpl $0x77, %ecx
je 0xac51
cmpl $0x62, %ecx
jne 0xad98
xorl %eax, %eax
movl %eax, 0x138(%rbx)
leaq 0x21af4(%rip), %rsi     # 0x2c752
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad8f
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 0xaccb
orb $0x4, 0x140(%rbx)
movq %r15, %rdi
movl $0x71, %esi
callq 0x2070
testq %rax, %rax
je 0xace4
orb $0x8, 0x140(%rbx)
leaq 0x21a67(%rip), %rsi     # 0x2c752
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xada1
movq %rax, %rdi
callq 0xdfcd
movl %eax, 0x13c(%rbx)
leaq 0x21a3f(%rip), %rsi     # 0x2c752
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad2a
movq %rax, %rdi
callq 0x22b0
movl %eax, %r15d
movl %r15d, 0x148(%rbx)
leaq 0x21a1a(%rip), %rsi     # 0x2c752
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x22c0
testq %rax, %rax
je 0xad57
movq %rax, %rdi
callq 0x22b0
leal -0x2(,%rax,2), %r14d
movl %r14d, 0x144(%rbx)
cmpl $0x0, 0x138(%rbx)
jne 0xad6d
incl 0x144(%rbx)
movq %rbx, %rdi
callq 0x5e40
movq %rax, 0x1b8(%rbx)
movq %rbx, %rdi
callq 0x5f0f
movq %rax, 0x1c0(%rbx)
movb $0x1, %bl
jmp 0xadb5
leaq 0x20fea(%rip), %rdi     # 0x2bd80
jmp 0xada8
leaq 0x20fc9(%rip), %rdi     # 0x2bd68
jmp 0xada8
leaq 0x20ff9(%rip), %rdi     # 0x2bda1
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 0x1fba4(%rip), %rdi     # 0x2a971
leaq 0x20a3d(%rip), %rsi     # 0x2b811
leaq 0x20f5f(%rip), %rcx     # 0x2bd3a
movl $0x136, %edx            # imm = 0x136
callq 0x20d0
leaq 0x20f75(%rip), %rdi     # 0x2bd61
leaq 0x20a1e(%rip), %rsi     # 0x2b811
leaq 0x20f40(%rip), %rcx     # 0x2bd3a
movl $0x13b, %edx            # imm = 0x13B
callq 0x20d0