// File: /workspace/github_c_makefile_star2_O1/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
xorl %r8d, %r8d
cmpl $0x63, %ecx
setne %r8b
movslq %edx, %rbx
movl %esi, -0x2c(%rsp)
testl %esi, %esi
je 0x5b91
leal 0xb(%rdx), %r9d
movl %edx, %eax
leaq 0x613e(%rip), %rsi      # 0xbca0
cmpl %ecx, %r9d
je 0x5b7f
cmpl $0x7, 0x2c(%rsi,%rax,4)
jne 0x5b7f
xorl %r8d, %r8d
cmpl $0x63, %ecx
setne %r8b
movl %r9d, (%rdi,%r8,4)
incl %r8d
leal 0x9(%rdx), %r9d
cmpl %ecx, %r9d
je 0x5bd1
cmpl $0x7, 0x24(%rsi,%rax,4)
je 0x5bc7
jmp 0x5bd1
leal -0xb(%rdx), %esi
leaq 0x6105(%rip), %rax      # 0xbca0
cmpl %ecx, %esi
je 0x5bb7
cmpl $0x1, -0x2c(%rax,%rbx,4)
jne 0x5bb7
xorl %r8d, %r8d
cmpl $0x63, %ecx
setne %r8b
movl %esi, (%rdi,%r8,4)
incl %r8d
leal -0x9(%rdx), %r9d
cmpl %ecx, %r9d
je 0x5bd1
cmpl $0x1, -0x24(%rax,%rbx,4)
jne 0x5bd1
movl %r8d, %eax
movl %r9d, (%rdi,%rax,4)
incl %r8d
xorl %eax, %eax
leaq 0x1666(%rip), %r10      # 0x7240
leaq 0x60bf(%rip), %r9       # 0xbca0
movl -0x2c(%rsp), %r11d
leaq 0x1783(%rip), %r15      # 0x7370
movslq (%rax,%r10), %r14
addq %rbx, %r14
movslq (%r9,%r14,4), %rsi
cmpq $0x8, %rsi
je 0x5c03
cmpl $0x2, %esi
jne 0x5c18
cmpl %ecx, %r14d
je 0x5c18
cmpl %r11d, (%r15,%rsi,4)
jne 0x5c18
movslq %r8d, %r8
movl %r14d, (%rdi,%r8,4)
incl %r8d
addq $0x4, %rax
cmpq $0x20, %rax
jne 0x5bed
movq %rbx, -0x18(%rsp)
movl %ecx, -0x28(%rsp)
movslq %ecx, %rax
movq %rax, -0x20(%rsp)
xorl %eax, %eax
leaq 0x15d4(%rip), %rcx      # 0x7210
movl $0x15d7, %r12d          # imm = 0x15D7
movl -0x2c(%rsp), %ebp
leaq 0x1723(%rip), %r13      # 0x7370
movslq (%rcx,%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
movslq (%r9,%rsi), %rsi
testq %rsi, %rsi
je 0x5c9c
cmpl $0x64, %esi
je 0x5ca4
btq %rsi, %r12
jb 0x5c97
cmpq %r15, %r14
je 0x5c97
cmpl %ebp, (%r13,%rsi,4)
jne 0x5c97
movslq %r8d, %r8
movl %r11d, (%rdi,%r8,4)
incl %r8d
jmp 0x5c9c
cmpq %r15, %r14
jne 0x5ca4
addq %r10, %r15
addl %r10d, %r11d
jmp 0x5c67
incq %rax
cmpq $0x4, %rax
jne 0x5c4d
cmpl $0x0, -0x2c(%rsp)
movl $0x5, %eax
movl $0xb, %ebx
cmovel %eax, %ebx
movl $0x3, %eax
movl $0x9, %ecx
cmovel %eax, %ecx
movl %ecx, -0x24(%rsp)
xorl %eax, %eax
movq %rdx, -0x10(%rsp)
movq %rax, -0x8(%rsp)
leaq 0x157d(%rip), %rcx      # 0x7260
movslq (%rcx,%rax,4), %r15
leal (%r15,%rdx), %eax
xorl %r11d, %r11d
movl %r8d, %r10d
movslq %eax, %rcx
movq -0x20(%rsp), %r14
subq %rcx, %r14
shlq $0x2, %rcx
movq %r10, %rdx
shlq $0x20, %rdx
xorl %r13d, %r13d
movq %r10, %r8
leaq (%rcx,%r13,4), %rsi
movslq (%r9,%rsi), %rbp
testq %rbp, %rbp
je 0x5e05
cmpl $0x64, %ebp
je 0x5e0d
leal -0x4(%rbp), %esi
cmpl $0x7, %esi
ja 0x5dfd
leaq 0x14b6(%rip), %r12      # 0x71f0
movslq (%r12,%rsi,4), %rsi
addq %r12, %rsi
jmpq *%rsi
movl -0x2c(%rsp), %esi
leaq 0x1622(%rip), %r12      # 0x7370
cmpl %esi, (%r12,%rbp,4)
jne 0x5db8
cmpq %r13, %r14
je 0x5db8
testl %r10d, %r10d
jle 0x5dac
movslq -0x4(%rdi,%r8,4), %rbp
movl (%r9,%rbp,4), %r12d
movl %r12d, %esi
xorl %ebx, %esi
orl %r11d, %esi
je 0x5dec
cmpl -0x24(%rsp), %r12d
jne 0x5dac
movslq -0x8(%rdi,%r8,4), %rsi
cmpl %ebx, (%r9,%rsi,4)
jne 0x5dac
movl %eax, -0x8(%rdi,%r8,4)
movl %esi, -0x4(%rdi,%r8,4)
movl %ebp, (%rdi,%r8,4)
incq %r8
addl %r15d, %eax
addq %r15, %r13
movabsq $0x100000000, %rsi      # imm = 0x100000000
addq %rsi, %rdx
jmp 0x5d0d
sarq $0x1e, %rdx
movl %eax, (%rdi,%rdx)
incl %r8d
jmp 0x5e05
cmpl $0x5, %ebp
je 0x5dc2
cmpl $0xb, %ebp
jne 0x5dfd
movl -0x28(%rsp), %ecx
movl -0x2c(%rsp), %edx
leaq 0x159f(%rip), %rsi      # 0x7370
cmpl %edx, (%rsi,%rbp,4)
jne 0x5e01
cmpl %ecx, %eax
je 0x5e01
movslq %r8d, %r8
movl %eax, (%rdi,%r8,4)
incl %r8d
movl $0x1, %r11d
jmp 0x5e05
movl %eax, -0x4(%rdi,%r8,4)
movl %ebp, (%rdi,%r8,4)
incl %r8d
xorl %r11d, %r11d
jmp 0x5e05
movl -0x28(%rsp), %ecx
cmpl %ecx, %eax
jne 0x5e0d
addl %r15d, %eax
jmp 0x5cee
movq -0x8(%rsp), %rax
incq %rax
cmpq $0x4, %rax
movq -0x10(%rsp), %rdx
jne 0x5cd7
xorl %eax, %eax
leaq 0x13f3(%rip), %rcx      # 0x7220
movl -0x28(%rsp), %r10d
movq -0x18(%rsp), %rbx
movl -0x2c(%rsp), %r11d
leaq 0x152d(%rip), %r14      # 0x7370
movslq (%rax,%rcx), %rdx
addq %rbx, %rdx
movslq (%r9,%rdx,4), %rsi
cmpq $0xc, %rsi
je 0x5e59
cmpl $0x6, %esi
jne 0x5e6e
cmpl %r10d, %edx
je 0x5e6e
cmpl %r11d, (%r14,%rsi,4)
jne 0x5e6e
movslq %r8d, %r8
movl %edx, (%rdi,%r8,4)
incl %r8d
addq $0x4, %rax
cmpq $0x20, %rax
jne 0x5e43
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq