// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18018, %rsp          # imm = 0x18018
xorl %ebp, %ebp
leaq 0x1a82e(%rip), %rcx     # 0x1d8e0
movq $0x0, 0x8(%rsp)
leaq 0x1aa1e(%rip), %rax     # 0x1dae0
testq %rbp, %rbp
je 0x30d5
movslq 0x8(%rsp), %rax
shlq $0x3, %rax
addq -0x8(%rcx,%rbp,8), %rax
movq %rax, (%rcx,%rbp,8)
leaq 0x8d00(%rip), %rax      # 0xbde0
movq (%rax,%rbp,8), %rbx
leaq 0x1a3f5(%rip), %rax     # 0x1d4e0
movq (%rax,%rbp,8), %r13
xorl %eax, %eax
xorl %r15d, %r15d
movq %rax, %r14
movq %r15, 0x10010(%rsp,%rax,8)
movl %ebp, %edi
movq %r15, %rsi
callq 0x3474
movq %rax, %r12
movl %ebp, %edi
movq %r15, %rsi
callq 0x341f
orq %r12, %rax
xorq %rbx, %rax
movq %rax, 0x8010(%rsp,%r14,8)
leaq 0x1(%r14), %rax
subq %r13, %r15
andq %r13, %r15
jne 0x30f4
movq %rax, 0x8(%rsp)
movl $0x40, %r15d
movq %rbp, (%rsp)
leaq 0x4eea(%rip), %rax      # 0x8030
subl (%rax,%rbp,4), %r15d
movl %r14d, %ebx
xorl %ebp, %ebp
callq 0x2860
movq %rax, %r12
callq 0x2860
movq %rax, %r13
andq %r12, %r13
callq 0x2860
movq %rax, %r12
andq %r13, %r12
movq (%rsp), %rax
leaq 0x1a368(%rip), %rcx     # 0x1d4e0
movq (%rcx,%rax,8), %rax
imulq %r12, %rax
movabsq $-0x100000000000000, %rcx # imm = 0xFF00000000000000
andq %rcx, %rax
je 0x3205
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x1(%rax), %rdx
incl %ecx
andq %rdx, %rax
jne 0x3194
cmpl $0x5, %ecx
jb 0x3205
xorl %r13d, %r13d
movl $0x8000, %edx           # imm = 0x8000
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x20a0
xorl %eax, %eax
movq 0x10010(%rsp,%r13,8), %rdx
imulq %r12, %rdx
movl %r15d, %ecx
shrq %cl, %rdx
movl %edx, %ecx
movq 0x10(%rsp,%rcx,8), %rdx
movq 0x8010(%rsp,%r13,8), %rsi
testq %rdx, %rdx
je 0x31ec
cmpq %rsi, %rdx
je 0x31f1
movl $0x1, %eax
jmp 0x31f1
movq %rsi, 0x10(%rsp,%rcx,8)
testl %eax, %eax
jne 0x3201
leaq 0x1(%r13), %rcx
cmpq %rbx, %r13
movq %rcx, %r13
jb 0x31ba
testl %eax, %eax
je 0x3215
incl %ebp
cmpl $0x3b9aca00, %ebp       # imm = 0x3B9ACA00
jne 0x314f
jmp 0x3241
movq (%rsp), %rbx
leaq 0x1a6c0(%rip), %rax     # 0x1d8e0
movq (%rax,%rbx,8), %rdi
leaq 0x8(,%r14,8), %rdx
leaq 0x10(%rsp), %rsi
callq 0x2100
leaq 0x1a4a3(%rip), %rax     # 0x1d6e0
movq %r12, (%rax,%rbx,8)
movq (%rsp), %rbp
incq %rbp
cmpq $0x40, %rbp
leaq 0x1a68d(%rip), %rcx     # 0x1d8e0
jne 0x30bb
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq