// File: /workspace/github_c_makefile_star2_O3/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 0x89b4(%rip), %rax      # 0xbca0
movaps 0x3dad(%rip), %xmm0     # 0x70a0
movaps %xmm0, (%rcx,%rax)
addq $0x10, %rcx
cmpq $0x1e0, %rcx            # imm = 0x1E0
jne 0x32f3
xorl %ecx, %ecx
leaq 0x20a33(%rip), %rdx     # 0x23d40
movslq (%rcx,%rdx), %rsi
movl $0x0, (%rax,%rsi,4)
addq $0x4, %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x330d
xorps %xmm0, %xmm0
movaps %xmm0, 0x8b51(%rip)     # 0xbe80
xorl %ecx, %ecx
movq %rcx, 0x8b58(%rip)      # 0xbe90
movups %xmm0, 0x8b89(%rip)     # 0xbec8
movups %xmm0, 0x8b92(%rip)     # 0xbed8
movups %xmm0, 0x8b9b(%rip)     # 0xbee8
movups %xmm0, 0x8ba4(%rip)     # 0xbef8
movl $0x0, 0x8baa(%rip)      # 0xbf08
movaps 0x3dcb(%rip), %xmm1     # 0x7130
movups %xmm1, 0x8b2c(%rip)     # 0xbe98
movq %rcx, 0x8b4d(%rip)      # 0xbec0
movups %xmm0, 0x8b2e(%rip)     # 0xbea8
movl $0x7, %ecx
leaq 0x20a9a(%rip), %rdx     # 0x23e20
leaq 0x3db3(%rip), %rsi      # 0x7140
leaq 0x3e3c(%rip), %r8       # 0x71d0
xorl %r9d, %r9d
movzbl (%rdi), %r14d
movl $0x1, %r10d
cmpl $0x61, %r14d
jg 0x33cd
leal -0x2f(%r14), %r11d
cmpl $0x23, %r11d
ja 0x3477
movslq (%rsi,%r11,4), %r11
addq %rsi, %r11
jmpq *%r11
addl $-0x30, %r14d
xorl %r11d, %r11d
movb $0x1, %bl
movl %r14d, %r10d
jmp 0x344b
leal -0x6b(%r14), %r11d
cmpl $0x7, %r11d
ja 0x33e9
movslq (%r8,%r11,4), %r11
addq %r8, %r11
jmpq *%r11
movl $0xc, %r11d
jmp 0x3449
cmpl $0x62, %r14d
jne 0x36c4
movl $0x9, %r11d
jmp 0x3449
movl $0x7, %r11d
jmp 0x3449
movl $0xb, %r11d
jmp 0x3449
movl $0x8, %r11d
jmp 0x3449
movl $0xa, %r11d
jmp 0x3449
movl $0x5, %r11d
jmp 0x3449
movl $0x6, %r11d
jmp 0x3449
movl $0x4, %r11d
jmp 0x3449
movl $0x2, %r11d
jmp 0x3449
movl $0x1, %r11d
jmp 0x3449
movl $0x3, %r11d
xorl %ebx, %ebx
movslq %r9d, %r9
leaq (%rdx,%r9,4), %r14
movl %r10d, %r15d
xorl %r10d, %r10d
testb %bl, %bl
jne 0x3464
movslq (%r14,%r10,4), %r12
movl %r11d, (%rax,%r12,4)
incq %r10
cmpl %r10d, %r15d
jne 0x3458
incq %rdi
addq %r10, %r9
jmp 0x3397
testl %r14d, %r14d
je 0x3497
cmpl $0x20, %r14d
jne 0x36c4
incq %rdi
addq $-0x20, %rdx
subq $0x1, %rcx
jae 0x3394
movb (%rdi), %dl
xorl %ecx, %ecx
cmpb $0x77, %dl
setne %cl
movl %ecx, 0x89f9(%rip)      # 0xbea0
leaq 0x2(%rdi), %rsi
movl 0x8a03(%rip), %ecx      # 0xbeb4
addq $0x6, %rdi
xorl %r8d, %r8d
movzbl (%rsi,%r8), %r9d
cmpl $0x6a, %r9d
jg 0x34d7
cmpl $0x4b, %r9d
je 0x34eb
cmpl $0x51, %r9d
jne 0x350d
movl $0x2, %r9d
jmp 0x34f9
cmpl $0x6b, %r9d
je 0x34f3
cmpl $0x71, %r9d
jne 0x3502
movl $0x8, %r9d
jmp 0x34f9
movl $0x1, %r9d
jmp 0x34f9
movl $0x4, %r9d
orl %r9d, %ecx
movl %ecx, 0x89b2(%rip)      # 0xbeb4
incq %r8
cmpl $0x4, %r8d
jne 0x34b8
jmp 0x3519
cmpl $0x20, %r9d
jne 0x3502
addq %r8, %rsi
movq %rsi, %rdi
movsbl 0x1(%rdi), %esi
cmpl $0x2d, %esi
je 0x3538
movsbl 0x2(%rdi), %edi
leal (%rdi,%rdi,4), %edi
leal (%rsi,%rdi,2), %esi
addl $0xfffffdca, %esi       # imm = 0xFFFFFDCA
movl %esi, 0x896c(%rip)      # 0xbea4
leaq 0x20b61(%rip), %rdi     # 0x240a0
xorl %r8d, %r8d
xorl %esi, %esi
movl (%r8,%rax), %r9d
leal -0x63(%r9), %r10d
cmpl $0x2, %r10d
jb 0x3565
testl %r9d, %r9d
je 0x3565
movslq %r9d, %r9
imulq $0x3c0, %r9, %r9        # imm = 0x3C0
xorq (%rdi,%r9), %rsi
addq $0x8, %rdi
addq $0x4, %r8
cmpq $0x1e0, %r8             # imm = 0x1E0
jne 0x3544
cmpb $0x77, %dl
jne 0x3584
movq 0x23bde(%rip), %rdx     # 0x27160
jmp 0x3586
xorl %edx, %edx
xorq %rsi, %rdx
movslq 0x8914(%rip), %rsi      # 0xbea4
cmpq $0x63, %rsi
je 0x35a1
leaq 0x20b03(%rip), %rdi     # 0x240a0
xorq (%rdi,%rsi,8), %rdx
movslq %ecx, %rcx
leaq 0x20a75(%rip), %rsi     # 0x24020
xorq (%rsi,%rcx,8), %rdx
movq %rdx, 0x890a(%rip)      # 0xbec0
xorl %ecx, %ecx
leaq 0x3db1(%rip), %rdx      # 0x7370
movl $0x1f7c, %esi           # imm = 0x1F7C
leaq 0x3de5(%rip), %rdi      # 0x73b0
leaq 0x82fe(%rip), %r8       # 0xb8d0
leaq 0x20567(%rip), %r9      # 0x23b40
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq (%rcx,%rax), %r15
testq %r15, %r15
je 0x36b3
cmpl $0x64, %r15d
je 0x36b3
movslq (%rdx,%r15,4), %r12
btq %r15, %rsi
jae 0x360d
incl 0x25c(%rax,%r12,4)
cmpl $0xc, %r15d
je 0x3622
cmpl $0x6, %r15d
jne 0x364b
movl %r10d, 0x8878(%rip)     # 0xbe98
jmp 0x3629
movl %r10d, 0x8873(%rip)     # 0xbe9c
movslq 0x228(%rax,%r15,4), %r12
leaq (%r15,%r15,4), %r13
leaq (%rax,%r13,8), %r13
movl %r10d, 0xc270(%r13,%r12,4)
incl 0x228(%rax,%r15,4)
jmp 0x36b3
movl (%rdi,%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 %r10d, 0xc270(%r13,%r12,4)
incl 0x228(%rax,%r15,4)
cmpl $0x7, %r15d
je 0x3697
cmpl $0x1, %r15d
jne 0x36b3
movslq (%rcx,%r8), %r15
movq (%r9,%r15,8), %r15
orq %r15, %r14
movq %r14, 0x87eb(%rip)      # 0xbe80
jmp 0x36a9
movslq (%rcx,%r8), %r15
movq (%r9,%r15,8), %r15
orq %r15, %r11
movq %r11, 0x87df(%rip)      # 0xbe88
orq %r15, %rbx
movq %rbx, 0x87dd(%rip)      # 0xbe90
incq %r10
addq $0x4, %rcx
cmpq $0x78, %r10
jne 0x35e4
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq