// File: /workspace/github_cpp_makefile_star2_O3/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 %ebx, %ebx
leaq 0x1a75c(%rip), %rcx     # 0x1d8e0
xorl %ebp, %ebp
leaq 0x1a953(%rip), %rax     # 0x1dae0
testq %rbx, %rbx
je 0x319e
movslq %ebp, %rax
shlq $0x3, %rax
addq -0x8(%rcx,%rbx,8), %rax
movq %rax, (%rcx,%rbx,8)
leaq 0x8c37(%rip), %rax      # 0xbde0
movq (%rax,%rbx,8), %rax
movq %rax, (%rsp)
leaq 0x1a328(%rip), %rax     # 0x1d4e0
movq (%rax,%rbx,8), %r13
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rbp, %r14
movq %r15, 0x10010(%rsp,%rbp,8)
movl %ebx, %edi
movq %r15, %rsi
callq 0x3539
movq %rax, %r12
movl %ebx, %edi
movq %r15, %rsi
callq 0x34e4
orq %r12, %rax
xorq (%rsp), %rax
movq %rax, 0x8010(%rsp,%rbp,8)
incq %rbp
subq %r13, %r15
andq %r13, %r15
jne 0x31c1
movl $0x40, %r15d
leaq 0x4e96(%rip), %rax      # 0x80a0
subl (%rax,%rbx,4), %r15d
movl %r14d, %r13d
xorl %eax, %eax
movq %rbx, 0x8(%rsp)
movl %eax, (%rsp)
callq 0x2860
movq %rax, %r12
callq 0x2860
movq %r13, %rbx
movq %rax, %r13
andq %r12, %r13
callq 0x2860
movq %rax, %r12
andq %r13, %r12
movq %rbx, %r13
movq 0x8(%rsp), %rbx
leaq 0x1a295(%rip), %rax     # 0x1d4e0
movq (%rax,%rbx,8), %rax
imulq %r12, %rax
movabsq $-0x100000000000000, %rcx # imm = 0xFF00000000000000
andq %rcx, %rax
je 0x32d0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x1(%rax), %rdx
incl %ecx
andq %rdx, %rax
jne 0x3267
cmpl $0x5, %ecx
jb 0x32d0
movl $0x8000, %edx           # imm = 0x8000
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x20a0
xorl %eax, %eax
movq 0x10010(%rsp,%rax,8), %rdx
imulq %r12, %rdx
movl %r15d, %ecx
shrq %cl, %rdx
movl %edx, %esi
movq 0x10(%rsp,%rsi,8), %rdx
movq 0x8010(%rsp,%rax,8), %rcx
testq %rdx, %rdx
je 0x32bc
cmpq %rcx, %rdx
jne 0x32cb
cmpq %r13, %rax
jb 0x32c6
jmp 0x32cb
movq %rcx, 0x10(%rsp,%rsi,8)
cmpq %r13, %rax
jae 0x32e2
incq %rax
jmp 0x328a
cmpq %rcx, %rdx
je 0x32e2
movl (%rsp), %eax
incl %eax
cmpl $0x3b9aca00, %eax       # imm = 0x3B9ACA00
jne 0x3218
jmp 0x330a
leaq 0x1a5f7(%rip), %rax     # 0x1d8e0
movq (%rax,%rbx,8), %rdi
leaq 0x8(,%r14,8), %rdx
leaq 0x10(%rsp), %rsi
callq 0x2100
leaq 0x1a3da(%rip), %rax     # 0x1d6e0
movq %r12, (%rax,%rbx,8)
incq %rbx
cmpq $0x40, %rbx
leaq 0x1a5c8(%rip), %rcx     # 0x1d8e0
jne 0x3186
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq