// File: bjohnsonfl[P]Overhead-Chess-Engine-/bitboard.cpp
// Function: initMagicRook()
void initMagicRook ()
{
    bitboard blockers[4096], attackers[4096], b, magic;
    bitboard used[4096];
    int size = 0;
    bitboard * idx;
    for ( square sq = a1; sq < numOfSq; sq++)
    {
        magicRookIndex[sq] = sq == a1 ? magicRookAttacks : magicRookIndex[sq - 1] + (size );
        b = 0;
        size = 0;
        do {
            
            blockers[size] = b;
            attackers[size] = squareBitMask[sq]^(horizMasks(sq, b) | vertMasks(sq, b));
            size++;
            b = (b - magicRookMask[sq]) & magicRookMask[sq];
        } while(b);
        
        int n = rookShifts [sq];
        int fail = 0;
        unsigned index = 0;
        
        for(int k = 0; k < 1000000000; k++) {
            
            magic = randomU64_fewbits();
            if(popcount((magicRookMask[sq] * magic) & 0xFF00000000000000ULL) < 6) continue;
            // std::cout<< "k: " << k <<" sq: "<<sq << "\n";
            for(int i = 0; i < 4096; i++) used[i] = 0;
            fail = 0;
            for(int i = 0; !fail && i < size; i++) {
                index = magicIndex(blockers[i], magic, n);
                if(used[index] == 0){
                    used[index] = attackers[i];
                }
                else if(used[index] != attackers[i]){
                    fail = 1;
                }
            }
            if(!fail) {
                // return magic;
               // std::cout<< "Rook magic: " << magic << " sq: " << sq  << "\n";
                idx = magicRookIndex[sq];
                for(int i = 0; i < size; i++) {
                    idx[i] = used[i];
                }
                magicRookNumbers[sq] = magic;
                /*////////////////
                //TEST
                b = 0;
                do {
                    
                    // printBitboard(b);
                    //  printBitboard( *(magicRookIndex[sq] + magicIndex(b, magic, rookShifts[sq])));
                    // printBitboard(squareBitMask[sq]^(vertMasks(sq, b) | horizMasks(sq, b)));
                    if(( *(magicRookIndex[sq] + magicIndex(b, magic, rookShifts[sq]))) != (squareBitMask[sq]^(horizMasks(sq, b) | vertMasks(sq, b)))) std::cout << "MAGIC DOESNT WORK \n";
                    
                    b = (b - magicRookMask[sq]) & magicRookMask[sq];
                } while(b);
                ///////////////////
                //////////////////
                 */
                break;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x18060, %rsp          # imm = 0x18060
movl $0x0, -0x18014(%rbp)
movl $0x0, -0x18024(%rbp)
cmpl $0x40, -0x18024(%rbp)
jge 0x539e
cmpl $0x0, -0x18024(%rbp)
jne 0x5015
leaq 0x1fb94(%rip), %rax     # 0x24ba0
movq %rax, -0x18048(%rbp)
jmp 0x5041
movl -0x18024(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x1f978(%rip), %rax     # 0x249a0
movq (%rax,%rcx,8), %rax
movslq -0x18014(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x18048(%rbp)
movq -0x18048(%rbp), %rdx
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0x1f949(%rip), %rax     # 0x249a0
movq %rdx, (%rax,%rcx,8)
movq $0x0, -0x10008(%rbp)
movl $0x0, -0x18014(%rbp)
movq -0x10008(%rbp), %rcx
movslq -0x18014(%rbp), %rax
movq %rcx, -0x8000(%rbp,%rax,8)
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0xde0b(%rip), %rax      # 0x12ea0
movq (%rax,%rcx,8), %rax
movq %rax, -0x18058(%rbp)
movl -0x18024(%rbp), %edi
movq -0x10008(%rbp), %rsi
callq 0x56e0
movq %rax, -0x18050(%rbp)
movl -0x18024(%rbp), %edi
movq -0x10008(%rbp), %rsi
callq 0x5610
movq -0x18058(%rbp), %rcx
movq %rax, %rdx
movq -0x18050(%rbp), %rax
orq %rdx, %rax
xorq %rax, %rcx
movslq -0x18014(%rbp), %rax
movq %rcx, -0x10000(%rbp,%rax,8)
movl -0x18014(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18014(%rbp)
movq -0x10008(%rbp), %rax
movl -0x18024(%rbp), %ecx
movl %ecx, %edx
leaq 0x1f48a(%rip), %rcx     # 0x245a0
subq (%rcx,%rdx,8), %rax
movl -0x18024(%rbp), %ecx
movl %ecx, %edx
leaq 0x1f477(%rip), %rcx     # 0x245a0
andq (%rcx,%rdx,8), %rax
movq %rax, -0x10008(%rbp)
cmpq $0x0, -0x10008(%rbp)
jne 0x5070
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0x8f0f(%rip), %rax      # 0xe060
movl (%rax,%rcx,4), %eax
movl %eax, -0x18028(%rbp)
movl $0x0, -0x1802c(%rbp)
movl $0x0, -0x18030(%rbp)
movl $0x0, -0x18034(%rbp)
cmpl $0x3b9aca00, -0x18034(%rbp) # imm = 0x3B9ACA00
jge 0x5389
callq 0x3f60
movq %rax, -0x10010(%rbp)
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0x1f3fd(%rip), %rax     # 0x245a0
movq (%rax,%rcx,8), %rdi
imulq -0x10010(%rbp), %rdi
movabsq $-0x100000000000000, %rax # imm = 0xFF00000000000000
andq %rax, %rdi
callq 0x3fa0
cmpl $0x6, %eax
jge 0x51cb
jmp 0x5375
movl $0x0, -0x18038(%rbp)
cmpl $0x1000, -0x18038(%rbp) # imm = 0x1000
jge 0x5205
movslq -0x18038(%rbp), %rax
movq $0x0, -0x18010(%rbp,%rax,8)
movl -0x18038(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18038(%rbp)
jmp 0x51d5
movl $0x0, -0x1802c(%rbp)
movl $0x0, -0x1803c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1802c(%rbp)
movb %al, -0x18059(%rbp)
jne 0x523f
movl -0x1803c(%rbp), %eax
cmpl -0x18014(%rbp), %eax
setl %al
movb %al, -0x18059(%rbp)
movb -0x18059(%rbp), %al
testb $0x1, %al
jne 0x524e
jmp 0x52e6
movslq -0x1803c(%rbp), %rax
movq -0x8000(%rbp,%rax,8), %rdi
movq -0x10010(%rbp), %rsi
movl -0x18028(%rbp), %edx
callq 0x58c0
movl %eax, -0x18030(%rbp)
movl -0x18030(%rbp), %eax
cmpq $0x0, -0x18010(%rbp,%rax,8)
jne 0x52a5
movslq -0x1803c(%rbp), %rax
movq -0x10000(%rbp,%rax,8), %rcx
movl -0x18030(%rbp), %eax
movq %rcx, -0x18010(%rbp,%rax,8)
jmp 0x52d0
movl -0x18030(%rbp), %eax
movq -0x18010(%rbp,%rax,8), %rax
movslq -0x1803c(%rbp), %rcx
cmpq -0x10000(%rbp,%rcx,8), %rax
je 0x52ce
movl $0x1, -0x1802c(%rbp)
jmp 0x52d0
jmp 0x52d2
movl -0x1803c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1803c(%rbp)
jmp 0x5219
cmpl $0x0, -0x1802c(%rbp)
jne 0x5373
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0x1f69e(%rip), %rax     # 0x249a0
movq (%rax,%rcx,8), %rax
movq %rax, -0x18020(%rbp)
movl $0x0, -0x18040(%rbp)
movl -0x18040(%rbp), %eax
cmpl -0x18014(%rbp), %eax
jge 0x5357
movslq -0x18040(%rbp), %rax
movq -0x18010(%rbp,%rax,8), %rdx
movq -0x18020(%rbp), %rax
movslq -0x18040(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x18040(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18040(%rbp)
jmp 0x5317
movq -0x10010(%rbp), %rdx
movl -0x18024(%rbp), %eax
movl %eax, %ecx
leaq 0x1f433(%rip), %rax     # 0x247a0
movq %rdx, (%rax,%rcx,8)
jmp 0x5389
jmp 0x5375
movl -0x18034(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18034(%rbp)
jmp 0x5178
jmp 0x538b
leaq -0x18024(%rbp), %rdi
xorl %esi, %esi
callq 0x5a80
jmp 0x4fef
addq $0x18060, %rsp          # imm = 0x18060
popq %rbp
retq
nopw (%rax,%rax)