// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl $0x7, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x30(%rsp), %rdi
callq 0x4120
xorl %eax, %eax
cmpl $0x0, 0x2c(%rsp)
movb %al, 0x13(%rsp)
jl 0x35ef
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
testb $0x1, %al
jne 0x35fc
jmp 0x3781
movl $0x1, 0x20(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
addl $-0x20, %eax
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0x52, %eax
ja 0x36fd
movq 0x8(%rsp), %rax
leaq 0x7a81(%rip), %rcx      # 0xb0ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x7, 0x24(%rsp)
jmp 0x370a
movl $0xa, 0x24(%rsp)
jmp 0x370a
movl $0x8, 0x24(%rsp)
jmp 0x370a
movl $0x9, 0x24(%rsp)
jmp 0x370a
movl $0xc, 0x24(%rsp)
jmp 0x370a
movl $0xb, 0x24(%rsp)
jmp 0x370a
movl $0x1, 0x24(%rsp)
jmp 0x370a
movl $0x4, 0x24(%rsp)
jmp 0x370a
movl $0x2, 0x24(%rsp)
jmp 0x370a
movl $0x3, 0x24(%rsp)
jmp 0x370a
movl $0x6, 0x24(%rsp)
jmp 0x370a
movl $0x5, 0x24(%rsp)
jmp 0x370a
movl $0x0, 0x24(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, 0x20(%rsp)
jmp 0x370a
movl 0x2c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x35d0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x38f4
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jge 0x376e
movl 0x2c(%rsp), %eax
shll $0x3, %eax
addl 0x28(%rsp), %eax
movl %eax, 0x18(%rsp)
movslq 0x18(%rsp), %rcx
leaq 0x245f9(%rip), %rax     # 0x27d30
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x24(%rsp)
je 0x3756
movl 0x24(%rsp), %edx
movq 0x30(%rsp), %rax
movslq 0x14(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3712
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x35d0
movq 0x38(%rsp), %rax
movsbl (%rax), %edx
movl $0x1, %ecx
xorl %eax, %eax
cmpl $0x77, %edx
cmovel %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x200(%rax)
movq 0x38(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x1c(%rsp)
cmpl $0x4, 0x1c(%rsp)
jge 0x3876
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x37d4
jmp 0x3876
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
addl $-0x4b, %eax
movl %eax, %ecx
movq %rcx, (%rsp)
subl $0x26, %eax
ja 0x3856
movq (%rsp), %rax
leaq 0x781b(%rip), %rcx      # 0xb010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x30(%rsp), %rax
movl 0x214(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x214(%rax)
jmp 0x3858
movq 0x30(%rsp), %rax
movl 0x214(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x214(%rax)
jmp 0x3858
movq 0x30(%rsp), %rax
movl 0x214(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x214(%rax)
jmp 0x3858
movq 0x30(%rsp), %rax
movl 0x214(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x214(%rax)
jmp 0x3858
jmp 0x3858
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x37b7
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
je 0x38c9
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
subl $0x61, %eax
movl %eax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
subl $0x31, %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %ecx
addl $0x15, %ecx
imull $0xa, 0x2c(%rsp), %eax
addl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x204(%rax)
movq 0x30(%rsp), %rdi
callq 0x42d0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x220(%rax)
movq 0x30(%rsp), %rdi
callq 0x43d0
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax)