// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2310
movq %rax, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x3f, -0x2c(%rbp)
jg 0xf424
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xf404
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x6, -0x30(%rbp)
jg 0xf469
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl $0x0, 0x11c(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl $0x0, 0x100(%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xf432
movq -0x10(%rbp), %rax
movq $0x0, 0x1a8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x198(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x188(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x178(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x168(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x158(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x1b0(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x1a0(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x190(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x180(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x170(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x160(%rax)
movq -0x20(%rbp), %rdi
leaq 0x291dd(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2080
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jae 0xf702
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x35(%rbp)
movsbl -0x35(%rbp), %eax
cmpl $0x2f, %eax
jne 0xf573
jmp 0xf6f4
movsbl -0x35(%rbp), %eax
cmpl $0x31, %eax
jl 0xf597
movsbl -0x35(%rbp), %eax
cmpl $0x38, %eax
jg 0xf597
movsbl -0x35(%rbp), %eax
subl $0x30, %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xf6f2
movl $0x0, -0x3c(%rbp)
movsbl -0x35(%rbp), %eax
cmpl $0x4b, %eax
jne 0xf5c0
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14c(%rax)
movl $0x6, -0x3c(%rbp)
jmp 0xf6b3
movsbl -0x35(%rbp), %eax
cmpl $0x6b, %eax
jne 0xf5e2
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x150(%rax)
movl $0xfffffffa, -0x3c(%rbp) # imm = 0xFFFFFFFA
jmp 0xf6b1
movsbl -0x35(%rbp), %eax
cmpl $0x51, %eax
jne 0xf5f7
movl $0x5, -0x3c(%rbp)
jmp 0xf6af
movsbl -0x35(%rbp), %eax
cmpl $0x71, %eax
jne 0xf60c
movl $0xfffffffb, -0x3c(%rbp) # imm = 0xFFFFFFFB
jmp 0xf6ad
movsbl -0x35(%rbp), %eax
cmpl $0x52, %eax
jne 0xf621
movl $0x4, -0x3c(%rbp)
jmp 0xf6ab
movsbl -0x35(%rbp), %eax
cmpl $0x72, %eax
jne 0xf633
movl $0xfffffffc, -0x3c(%rbp) # imm = 0xFFFFFFFC
jmp 0xf6a9
movsbl -0x35(%rbp), %eax
cmpl $0x42, %eax
jne 0xf645
movl $0x3, -0x3c(%rbp)
jmp 0xf6a7
movsbl -0x35(%rbp), %eax
cmpl $0x62, %eax
jne 0xf657
movl $0xfffffffd, -0x3c(%rbp) # imm = 0xFFFFFFFD
jmp 0xf6a5
movsbl -0x35(%rbp), %eax
cmpl $0x4e, %eax
jne 0xf669
movl $0x2, -0x3c(%rbp)
jmp 0xf6a3
movsbl -0x35(%rbp), %eax
cmpl $0x6e, %eax
jne 0xf67b
movl $0xfffffffe, -0x3c(%rbp) # imm = 0xFFFFFFFE
jmp 0xf6a1
movsbl -0x35(%rbp), %eax
cmpl $0x50, %eax
jne 0xf68d
movl $0x1, -0x3c(%rbp)
jmp 0xf69f
movsbl -0x35(%rbp), %eax
cmpl $0x70, %eax
jne 0xf69d
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
jmp 0xf69f
jmp 0xf6a1
jmp 0xf6a3
jmp 0xf6a5
jmp 0xf6a7
jmp 0xf6a9
jmp 0xf6ab
jmp 0xf6ad
jmp 0xf6af
jmp 0xf6b1
jmp 0xf6b3
cmpl $0x0, -0x3c(%rbp)
je 0xf6bb
jmp 0xf6da
leaq 0x272e0(%rip), %rdi     # 0x369a2
leaq 0x27fb1(%rip), %rsi     # 0x3767a
movl $0x136, %edx            # imm = 0x136
leaq 0x28515(%rip), %rcx     # 0x37bea
callq 0x20c0
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0xeb20
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xf6f4
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf538
cmpl $0x40, -0x2c(%rbp)
jne 0xf70a
jmp 0xf729
leaq 0x28500(%rip), %rdi     # 0x37c11
leaq 0x27f62(%rip), %rsi     # 0x3767a
movl $0x13b, %edx            # imm = 0x13B
leaq 0x284c6(%rip), %rcx     # 0x37bea
callq 0x20c0
xorl %eax, %eax
movl %eax, %edi
leaq 0x28fd1(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x77, %eax
je 0xf755
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x57, %eax
jne 0xf765
movq -0x10(%rbp), %rax
movl $0x1, 0x138(%rax)
jmp 0xf7aa
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x62, %eax
je 0xf77d
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x42, %eax
jne 0xf78d
movq -0x10(%rbp), %rax
movl $0x0, 0x138(%rax)
jmp 0xf7a8
movq -0x18(%rbp), %rsi
leaq 0x28480(%rip), %rdi     # 0x37c18
movb $0x0, %al
callq 0x8f00
movb $0x0, -0x1(%rbp)
jmp 0xf9ac
jmp 0xf7aa
xorl %eax, %eax
movl %eax, %edi
leaq 0x28f50(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xf7e0
movq -0x18(%rbp), %rsi
leaq 0x28460(%rip), %rdi     # 0x37c30
movb $0x0, %al
callq 0x8f00
movb $0x0, -0x1(%rbp)
jmp 0xf9ac
movq -0x10(%rbp), %rax
movl $0x0, 0x140(%rax)
movq -0x28(%rbp), %rdi
movl $0x4b, %esi
callq 0x2070
cmpq $0x0, %rax
je 0xf815
movq -0x10(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x140(%rax)
movq -0x28(%rbp), %rdi
movl $0x51, %esi
callq 0x2070
cmpq $0x0, %rax
je 0xf83c
movq -0x10(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x140(%rax)
movq -0x28(%rbp), %rdi
movl $0x6b, %esi
callq 0x2070
cmpq $0x0, %rax
je 0xf863
movq -0x10(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x140(%rax)
movq -0x28(%rbp), %rdi
movl $0x71, %esi
callq 0x2070
cmpq $0x0, %rax
je 0xf88a
movq -0x10(%rbp), %rax
movl 0x140(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x140(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x28e70(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xf8c0
movq -0x18(%rbp), %rsi
leaq 0x283a1(%rip), %rdi     # 0x37c51
movb $0x0, %al
callq 0x8f00
movb $0x0, -0x1(%rbp)
jmp 0xf9ac
movq -0x28(%rbp), %rdi
callq 0x14150
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x13c(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x28e25(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xf907
movq -0x28(%rbp), %rdi
callq 0x22b0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x148(%rax)
jmp 0xf915
movq -0x10(%rbp), %rax
movl $0x0, 0x148(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x28de5(%rip), %rsi     # 0x38705
callq 0x22c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xf94c
movq -0x28(%rbp), %rdi
callq 0x22b0
movl %eax, %ecx
subl $0x1, %ecx
shll %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x144(%rax)
jmp 0xf95a
movq -0x10(%rbp), %rax
movl $0x0, 0x144(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x138(%rax)
jne 0xf97a
movq -0x10(%rbp), %rax
movl 0x144(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x144(%rax)
movq -0x10(%rbp), %rdi
callq 0x81f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1b8(%rax)
movq -0x10(%rbp), %rdi
callq 0x83a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1c0(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)