// File: /workspace/github_c_makefile_star2_O3/ArchLuke[P]YouSuck/Engine.c
// Function: FillPieces
int FillPieces(const S_BOARD *pos, int pieces[MAXPIECES], const int side, int sq, int fromsq) {
    int pce,index,t_sq,dir;
    int count;

    if(fromsq==NO_SQ)
	count=0;
    else
	count=1;

    // pawn
    if(side == WHITE) {
        if(pos->pieces[sq-11] == wP && ((sq-11) != fromsq)) {
	    pieces[count]=sq-11;
            count ++;
        }
	if(pos->pieces[sq-9]==wP && ((sq-9) != fromsq)){
	    pieces[count]=sq-9;
	    count ++;
	}
    } else {
        if(pos->pieces[sq+11] == bP && ((sq+11) != fromsq)) {
	    pieces[count]=sq+11;
            count ++;
        }   
	if(pos->pieces[sq+9] == bP && ((sq+9) != fromsq)) {
	    pieces[count]=sq+9;
            count ++;
        }    
    }
    
    // knights
    for(index = 0; index < 8; ++index) {        
        pce = pos->pieces[sq + KnDir[index]];
        if(pce != OFFBOARD && IsKn(pce) && PieceCol[pce]==side && ((sq+KnDir[index])!=fromsq)) {
	 
	    pieces[count]=sq+KnDir[index];
            count ++;

        }
    }
     
    
    // bishops
    for(index = 0; index < 4; ++index) {        
        dir = BiDir[index];
        t_sq = sq + dir;
        pce = pos->pieces[t_sq];
        while(pce != OFFBOARD) {
            if(pce != EMPTY) {
                if(IsBQ(pce) && PieceCol[pce] == side && (t_sq!=fromsq)) {
		    pieces[count]=t_sq;
                    count ++;

                }else if(t_sq != fromsq)
			break;
		

                
            }
            t_sq += dir;
            pce = pos->pieces[t_sq];
        }
    }
    int queenBeforeRook=FALSE;
    // rooks, queens
    for(index = 0; index < 4; ++index) { 
	queenBeforeRook=FALSE;
        dir = RkDir[index];
        t_sq = sq + dir;
        pce = pos->pieces[t_sq];
        while(pce != OFFBOARD) {
            if(pce != EMPTY) {
                if(IsRk(pce) && PieceCol[pce] == side && (t_sq != fromsq)) {
		    if(count>0)
		    {
			    if((pos->pieces[pieces[count-1]]==(side?bQ:wQ)) && !queenBeforeRook)
			    {
				int sq=pieces[count-1];
				pieces[count-1]=t_sq;
				pieces[count]=sq;
				
				count++;
		
				t_sq += dir;
				pce=pos->pieces[t_sq];
				continue;
			    }
			    if((pos->pieces[pieces[count-1]]==(side?bB:wB)) && (pos->pieces[pieces[count-2]]==(side?bQ:wQ)))
			    {
				int sq1=pieces[count-2];
				int sq2=pieces[count-1];
				pieces[count-2]=t_sq;
				pieces[count-1]=sq1;
				pieces[count]=sq2;
				
				count ++;
				
				t_sq += dir;
				pce=pos->pieces[t_sq];
				continue;
					
			    }
		     }
		    pieces[count]=t_sq;
                    count ++;
                }else if(IsQn(pce) && PieceCol[pce] == side && (t_sq!=fromsq)) {
		    pieces[count]=t_sq;
                    
		    queenBeforeRook=TRUE;
		    count ++;

		}
		else if(t_sq != fromsq)
			break;
                
            }
            t_sq += dir;
            pce = pos->pieces[t_sq];
        }
    }
       
    // kings
    for(index = 0; index < 8; ++index) {        
        pce = pos->pieces[sq + KiDir[index]];
        if(pce != OFFBOARD && IsKi(pce) && PieceCol[pce]==side && ((sq+KiDir[index])!=fromsq)) {
	    pieces[count]=sq+KiDir[index];
            count ++;

        }
    }
    
    return count;
      
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %ecx, %r10d
xorl %r9d, %r9d
cmpl $0x63, %ecx
setne %r9b
movslq %edx, %r11
movl %esi, -0x28(%rsp)
testl %esi, %esi
je 0x5a7b
leal 0xb(%rdx), %esi
movl %edx, %eax
leaq 0x6256(%rip), %r8       # 0xbca0
cmpl %r10d, %esi
je 0x5a69
cmpl $0x7, 0x2c(%r8,%rax,4)
jne 0x5a69
xorl %r9d, %r9d
cmpl $0x63, %r10d
setne %r9b
movl %esi, (%rdi,%r9,4)
incl %r9d
leal 0x9(%rdx), %esi
cmpl %r10d, %esi
je 0x5abd
cmpl $0x7, 0x24(%r8,%rax,4)
je 0x5ab4
jmp 0x5abd
leal -0xb(%rdx), %esi
leaq 0x621b(%rip), %rax      # 0xbca0
cmpl %r10d, %esi
je 0x5aa4
cmpl $0x1, -0x2c(%rax,%r11,4)
jne 0x5aa4
xorl %r9d, %r9d
cmpl $0x63, %r10d
setne %r9b
movl %esi, (%rdi,%r9,4)
incl %r9d
leal -0x9(%rdx), %esi
cmpl %r10d, %esi
je 0x5abd
cmpl $0x1, -0x24(%rax,%r11,4)
jne 0x5abd
movl %r9d, %eax
movl %esi, (%rdi,%rax,4)
incl %r9d
xorl %eax, %eax
leaq 0x177a(%rip), %rbx      # 0x7240
leaq 0x18a3(%rip), %r14      # 0x7370
movl -0x28(%rsp), %ecx
movslq (%rax,%rbx), %rsi
addq %r11, %rsi
leaq 0x61c1(%rip), %r8       # 0xbca0
movl (%r8,%rsi,4), %r8d
cmpq $0x8, %r8
je 0x5aef
cmpl $0x2, %r8d
jne 0x5b04
cmpl %r10d, %esi
je 0x5b04
cmpl %ecx, (%r14,%r8,4)
jne 0x5b04
movslq %r9d, %r9
movl %esi, (%rdi,%r9,4)
incl %r9d
addq $0x4, %rax
cmpq $0x20, %rax
jne 0x5ad1
movq %r11, -0x18(%rsp)
movl %r10d, -0x24(%rsp)
movslq %r10d, %rax
movq %rax, -0x20(%rsp)
xorl %eax, %eax
leaq 0x16e7(%rip), %r12      # 0x7210
movl $0x15d7, %r8d           # imm = 0x15D7
movl -0x28(%rsp), %ecx
movslq (%r12,%rax,4), %r10
leal (%r10,%rdx), %r11d
movslq %r11d, %rbx
movq -0x20(%rsp), %r14
subq %rbx, %r14
shlq $0x2, %rbx
xorl %r15d, %r15d
leaq (%rbx,%r15,4), %rsi
leaq 0x6148(%rip), %r13      # 0xbca0
movslq (%r13,%rsi), %rsi
testq %rsi, %rsi
je 0x5b91
cmpl $0x64, %esi
je 0x5b99
btq %rsi, %r8
jb 0x5b8c
cmpq %r15, %r14
je 0x5b8c
leaq 0x17f7(%rip), %r13      # 0x7370
cmpl %ecx, (%r13,%rsi,4)
jne 0x5b8c
movslq %r9d, %r9
movl %r11d, (%rdi,%r9,4)
incl %r9d
jmp 0x5b91
cmpq %r15, %r14
jne 0x5b99
addq %r10, %r15
addl %r10d, %r11d
jmp 0x5b4d
incq %rax
cmpq $0x4, %rax
jne 0x5b33
cmpl $0x0, -0x28(%rsp)
movl $0x5, %eax
movl $0xb, %ebx
cmovel %eax, %ebx
movl $0x3, %eax
movl $0x9, %ecx
cmovel %eax, %ecx
xorl %eax, %eax
movq %rdx, -0x10(%rsp)
movq %rax, -0x8(%rsp)
leaq 0x168c(%rip), %rsi      # 0x7260
movslq (%rsi,%rax,4), %r15
leal (%r15,%rdx), %eax
xorl %r11d, %r11d
movl %r9d, %r8d
movslq %eax, %r14
movq -0x20(%rsp), %r10
subq %r14, %r10
shlq $0x2, %r14
movq %r8, %rdx
shlq $0x20, %rdx
xorl %r13d, %r13d
movq %r8, %r9
leaq (%r14,%r13,4), %rsi
leaq 0x6097(%rip), %r12      # 0xbca0
movslq (%r12,%rsi), %rbp
testq %rbp, %rbp
je 0x5d14
cmpl $0x64, %ebp
je 0x5d1c
leal -0x4(%rbp), %esi
cmpl $0x7, %esi
ja 0x5cf6
leaq 0x15be(%rip), %r12      # 0x71f0
movslq (%r12,%rsi,4), %rsi
addq %r12, %rsi
jmpq *%rsi
leaq 0x172e(%rip), %rsi      # 0x7370
movl (%rsi,%rbp,4), %esi
cmpl -0x28(%rsp), %esi
jne 0x5ccf
cmpq %r13, %r10
je 0x5ccf
testl %r8d, %r8d
jle 0x5cb7
movslq -0x4(%rdi,%r9,4), %rbp
leaq 0x603b(%rip), %rsi      # 0xbca0
movl (%rsi,%rbp,4), %r12d
movl %r12d, %esi
xorl %ebx, %esi
orl %r11d, %esi
je 0x5d05
cmpl %ecx, %r12d
jne 0x5cb7
movslq -0x8(%rdi,%r9,4), %rsi
leaq 0x6018(%rip), %r12      # 0xbca0
cmpl %ebx, (%r12,%rsi,4)
jne 0x5cb7
movl %eax, -0x8(%rdi,%r9,4)
movl %esi, -0x4(%rdi,%r9,4)
movl %ebp, (%rdi,%r9,4)
incq %r9
addl %r15d, %eax
addq %r15, %r13
movabsq $0x100000000, %rsi      # imm = 0x100000000
addq %rsi, %rdx
jmp 0x5bfe
sarq $0x1e, %rdx
movl %eax, (%rdi,%rdx)
incl %r9d
jmp 0x5d14
leaq 0x16a6(%rip), %rsi      # 0x7370
movl (%rsi,%rbp,4), %esi
jmp 0x5cd4
cmpl $0x5, %ebp
jne 0x5cf6
movl -0x24(%rsp), %r8d
cmpl -0x28(%rsp), %esi
jne 0x5cfb
cmpl %eax, %r8d
je 0x5cfb
sarq $0x1e, %rdx
movl %eax, (%rdi,%rdx)
incl %r9d
movl $0x1, %r11d
jmp 0x5d14
movl -0x24(%rsp), %r8d
cmpl %eax, %r8d
movl %r8d, %eax
je 0x5d14
jmp 0x5d1c
movl %eax, -0x4(%rdi,%r9,4)
movl %ebp, (%rdi,%r9,4)
incl %r9d
xorl %r11d, %r11d
addl %r15d, %eax
jmp 0x5bdf
movq -0x8(%rsp), %rax
incq %rax
cmpq $0x4, %rax
movq -0x10(%rsp), %rdx
jne 0x5bc8
xorl %eax, %eax
leaq 0x14e4(%rip), %rdx      # 0x7220
movl -0x24(%rsp), %r10d
movq -0x18(%rsp), %r11
leaq 0x1623(%rip), %rbx      # 0x7370
movl -0x28(%rsp), %ecx
movslq (%rax,%rdx), %rsi
addq %r11, %rsi
leaq 0x5f41(%rip), %r8       # 0xbca0
movl (%r8,%rsi,4), %r8d
cmpq $0xc, %r8
je 0x5d6f
cmpl $0x6, %r8d
jne 0x5d84
cmpl %r10d, %esi
je 0x5d84
cmpl %ecx, (%rbx,%r8,4)
jne 0x5d84
movslq %r9d, %r9
movl %esi, (%rdi,%r9,4)
incl %r9d
addq $0x4, %rax
cmpq $0x20, %rax
jne 0x5d51
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq