// File: /workspace/github_c_makefile_star2_O1/ArchLuke[P]YouSuck/Engine.c
// Function: ParseFen
static
int ParseFen(char *fen, S_BOARD *pos)
{
       int  rank = RANK_8;
        int  file = FILE_A;
        int  piece = 0;
        int  count = 0;
        int  i = 0;
    int  sq64 = 0;
    int  sq120 = 0;
    
    ResetBoard(pos);
    
    while ((rank >= RANK_1) && *fen) {
        count = 1;
        switch (*fen) {
            case 'p': piece = bP; break;
            case 'r': piece = bR; break;
            case 'n': piece = bN; break;
            case 'b': piece = bB; break;
            case 'k': piece = bK; break;
            case 'q': piece = bQ; break;
            case 'P': piece = wP; break;
            case 'R': piece = wR; break;
            case 'N': piece = wN; break;
            case 'B': piece = wB; break;
            case 'K': piece = wK; break;
            case 'Q': piece = wQ; break;

            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
                piece = EMPTY;
                count = *fen - '0';
                break;

            case '/':
            case ' ':
                rank--;
                file = FILE_A;
                fen++;
                continue;              

            default:
                return -1;
        }        
        
        for (i = 0; i < count; i++) {            
            sq64 = rank * 8 + file;
            sq120 = SQ120(sq64);
            if (piece != EMPTY) {
                pos->pieces[sq120] = piece;
            }
            file++;
        }
        fen++;
    }
    
    
    pos->side = (*fen == 'w') ? WHITE : BLACK;
    fen += 2;
    
    for (i = 0; i < 4; i++) {
        if (*fen == ' ') {
            break;
        }        
        switch(*fen) {
            case 'K': pos->castlePerm |= WKCA; break;
            case 'Q': pos->castlePerm |= WQCA; break;
            case 'k': pos->castlePerm |= BKCA; break;
            case 'q': pos->castlePerm |= BQCA; break;
            default:         break;
        }
        fen++;
    }
    fen++;
    
    if (*fen != '-') {        
        file = fen[0] - 'a';
        rank = fen[1] - '1';
        
        pos->enPas = FR2SQ(file,rank);        
    }
    
    pos->posKey = GeneratePosKey(pos);
    
    UpdateListsMaterial(pos);
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorl %ecx, %ecx
leaq 0x897f(%rip), %rax      # 0xbca0
movaps 0x3d78(%rip), %xmm0     # 0x70a0
movaps %xmm0, (%rcx,%rax)
addq $0x10, %rcx
cmpq $0x1e0, %rcx            # imm = 0x1E0
jne 0x3328
xorl %ecx, %ecx
leaq 0x209fe(%rip), %rdx     # 0x23d40
movslq (%rcx,%rdx), %rsi
movl $0x0, (%rax,%rsi,4)
addq $0x4, %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x3342
xorps %xmm0, %xmm0
movaps %xmm0, 0x8b1c(%rip)     # 0xbe80
xorl %ecx, %ecx
movq %rcx, 0x8b23(%rip)      # 0xbe90
movups %xmm0, 0x8b54(%rip)     # 0xbec8
movups %xmm0, 0x8b5d(%rip)     # 0xbed8
movups %xmm0, 0x8b66(%rip)     # 0xbee8
movups %xmm0, 0x8b6f(%rip)     # 0xbef8
movl $0x0, 0x8b75(%rip)      # 0xbf08
movaps 0x3d96(%rip), %xmm1     # 0x7130
movups %xmm1, 0x8af7(%rip)     # 0xbe98
movq %rcx, 0x8b18(%rip)      # 0xbec0
movups %xmm0, 0x8af9(%rip)     # 0xbea8
movb $0x1, %r9b
movl $0x7, %edx
leaq 0x20a62(%rip), %rcx     # 0x23e20
leaq 0x3d7b(%rip), %rsi      # 0x7140
leaq 0x3e04(%rip), %r8       # 0x71d0
xorl %r10d, %r10d
movzbl (%rdi), %ebx
movl $0x1, %r11d
cmpl $0x61, %ebx
jg 0x3404
leal -0x2f(%rbx), %r14d
cmpl $0x23, %r14d
ja 0x34ad
movslq (%rsi,%r14,4), %r14
addq %rsi, %r14
jmpq *%r14
movsbl %bl, %r11d
addl $-0x30, %r11d
xorl %ebx, %ebx
movb $0x1, %bpl
jmp 0x3474
leal -0x6b(%rbx), %r14d
cmpl $0x7, %r14d
ja 0x341e
movslq (%r8,%r14,4), %rbx
addq %r8, %rbx
jmpq *%rbx
movl $0xc, %ebx
jmp 0x3472
cmpl $0x62, %ebx
jne 0x36f6
movl $0x9, %ebx
jmp 0x3472
movl $0x7, %ebx
jmp 0x3472
movl $0xb, %ebx
jmp 0x3472
movl $0x8, %ebx
jmp 0x3472
movl $0xa, %ebx
jmp 0x3472
movl $0x5, %ebx
jmp 0x3472
movl $0x6, %ebx
jmp 0x3472
movl $0x4, %ebx
jmp 0x3472
movl $0x2, %ebx
jmp 0x3472
movl $0x1, %ebx
jmp 0x3472
movl $0x3, %ebx
xorl %ebp, %ebp
testl %r11d, %r11d
jle 0x349e
movslq %r10d, %r14
leaq (%rcx,%r14,4), %r14
movl %r11d, %r11d
xorl %r15d, %r15d
testb %bpl, %bpl
jne 0x3493
movslq (%r14,%r15,4), %r12
movl %ebx, (%rax,%r12,4)
incq %r15
cmpl %r15d, %r11d
jne 0x3486
addl %r15d, %r10d
incq %rdi
testb $0x1, %r9b
jne 0x33cf
jmp 0x34d8
testl %ebx, %ebx
je 0x34d8
cmpl $0x20, %ebx
jne 0x36f6
movq %rdx, %r10
addq $-0x1, %r10
setb %r9b
incq %rdi
addq $-0x20, %rcx
testq %rdx, %rdx
movq %r10, %rdx
jne 0x33cc
movb (%rdi), %cl
xorl %edx, %edx
cmpb $0x77, %cl
setne %dl
movl %edx, 0x89b8(%rip)      # 0xbea0
leaq 0x2(%rdi), %rdx
movl 0x89c2(%rip), %esi      # 0xbeb4
addq $0x6, %rdi
xorl %r8d, %r8d
movzbl (%rdx,%r8), %r9d
cmpl $0x6a, %r9d
jg 0x3518
cmpl $0x4b, %r9d
je 0x352c
cmpl $0x51, %r9d
jne 0x354e
movl $0x2, %r9d
jmp 0x353a
cmpl $0x6b, %r9d
je 0x3534
cmpl $0x71, %r9d
jne 0x3543
movl $0x8, %r9d
jmp 0x353a
movl $0x1, %r9d
jmp 0x353a
movl $0x4, %r9d
orl %r9d, %esi
movl %esi, 0x8971(%rip)      # 0xbeb4
incq %r8
cmpl $0x4, %r8d
jne 0x34f9
jmp 0x355a
cmpl $0x20, %r9d
jne 0x3543
addq %r8, %rdx
movq %rdx, %rdi
movsbl 0x1(%rdi), %edx
cmpl $0x2d, %edx
je 0x3579
movsbl 0x2(%rdi), %esi
leal (%rsi,%rsi,4), %esi
leal (%rdx,%rsi,2), %edx
addl $0xfffffdca, %edx       # imm = 0xFFFFFDCA
movl %edx, 0x892b(%rip)      # 0xbea4
leaq 0x20b20(%rip), %rsi     # 0x240a0
xorl %edi, %edi
xorl %edx, %edx
movl (%rdi,%rax), %r8d
leal -0x63(%r8), %r9d
cmpl $0x2, %r9d
jb 0x35a5
testl %r8d, %r8d
je 0x35a5
movslq %r8d, %r8
imulq $0x3c0, %r8, %r8        # imm = 0x3C0
xorq (%rsi,%r8), %rdx
addq $0x8, %rsi
addq $0x4, %rdi
cmpq $0x1e0, %rdi            # imm = 0x1E0
jne 0x3584
cmpb $0x77, %cl
jne 0x35c4
movq 0x23b9e(%rip), %rcx     # 0x27160
jmp 0x35c6
xorl %ecx, %ecx
xorq %rdx, %rcx
movslq 0x88d4(%rip), %rdx      # 0xbea4
cmpq $0x63, %rdx
je 0x35e1
leaq 0x20ac3(%rip), %rsi     # 0x240a0
xorq (%rsi,%rdx,8), %rcx
movslq 0x88cc(%rip), %rdx      # 0xbeb4
leaq 0x20a31(%rip), %rsi     # 0x24020
xorq (%rsi,%rdx,8), %rcx
movq %rcx, 0x88c6(%rip)      # 0xbec0
movq 0x8887(%rip), %rcx      # 0xbe88
movq 0x8888(%rip), %rdx      # 0xbe90
movq 0x8871(%rip), %rsi      # 0xbe80
xorl %edi, %edi
leaq 0x3d58(%rip), %r8       # 0x7370
movl $0x1f7c, %r9d           # imm = 0x1F7C
leaq 0x82ab(%rip), %r10      # 0xb8d0
leaq 0x20514(%rip), %r11     # 0x23b40
leaq 0x3d7d(%rip), %rbx      # 0x73b0
xorl %r14d, %r14d
movslq (%rdi,%rax), %r15
testq %r15, %r15
je 0x36e5
cmpl $0x64, %r15d
je 0x36e5
movslq (%r8,%r15,4), %r12
btq %r15, %r9
jae 0x365f
incl 0x25c(%rax,%r12,4)
cmpl $0xc, %r15d
je 0x3674
cmpl $0x6, %r15d
jne 0x367d
movl %r14d, 0x8826(%rip)     # 0xbe98
jmp 0x3689
movl %r14d, 0x8821(%rip)     # 0xbe9c
jmp 0x3689
movl (%rbx,%r15,4), %ebp
addl %ebp, 0x264(%rax,%r12,4)
movslq 0x228(%rax,%r15,4), %r12
leaq (%r15,%r15,4), %r13
leaq (%rax,%r13,8), %r13
movl %r14d, 0xc270(%r13,%r12,4)
incl 0x228(%rax,%r15,4)
cmpl $0x7, %r15d
je 0x36c9
cmpl $0x1, %r15d
jne 0x36e5
movslq (%rdi,%r10), %r15
movq (%r11,%r15,8), %r15
orq %r15, %rsi
movq %rsi, 0x87b9(%rip)      # 0xbe80
jmp 0x36db
movslq (%rdi,%r10), %r15
movq (%r11,%r15,8), %r15
orq %r15, %rcx
movq %rcx, 0x87ad(%rip)      # 0xbe88
orq %r15, %rdx
movq %rdx, 0x87ab(%rip)      # 0xbe90
incq %r14
addq $0x4, %rdi
cmpq $0x78, %r14
jne 0x3636
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq