// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %r12d, %r12d
leaq 0x1ab2f(%rip), %rax     # 0x1dae0
testq %rbx, %rbx
je 0x2fd3
cmpq $0x40, %rbx
je 0x3140
movslq %r12d, %rax
shlq $0x3, %rax
leaq 0x1a912(%rip), %rcx     # 0x1d8e0
addq -0x8(%rcx,%rbx,8), %rax
leaq 0x1a906(%rip), %rcx     # 0x1d8e0
movq %rax, (%rcx,%rbx,8)
leaq 0x8dfb(%rip), %rax      # 0xbde0
movq (%rax,%rbx,8), %rax
movq %rax, 0x8(%rsp)
leaq 0x1a4eb(%rip), %rax     # 0x1d4e0
movq (%rax,%rbx,8), %rbp
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r12, %r13
movq %r14, 0x10010(%rsp,%r12,8)
movl %ebx, %edi
movq %r14, %rsi
callq 0x335f
movq %rax, %r15
movl %ebx, %edi
movq %r14, %rsi
callq 0x330a
orq %r15, %rax
xorq 0x8(%rsp), %rax
movq %rax, 0x8010(%rsp,%r12,8)
incq %r12
subq %rbp, %r14
andq %rbp, %r14
jne 0x2fff
pushq $0x40
popq %rax
movl %eax, %r14d
movq %rbx, 0x8(%rsp)
leaq 0x4012(%rip), %rax      # 0x7060
subl (%rax,%rbx,4), %r14d
leal -0x1(%r12), %ebp
xorl %ebx, %ebx
cmpl $0x3b9aca00, %ebx       # imm = 0x3B9ACA00
je 0x3133
callq 0x27da
movq %rax, %r15
movq 0x8(%rsp), %rax
leaq 0x1a467(%rip), %rcx     # 0x1d4e0
movq (%rcx,%rax,8), %rdi
imulq %r15, %rdi
movabsq $-0x100000000000000, %rax # imm = 0xFF00000000000000
andq %rax, %rdi
callq 0x2801
cmpl $0x6, %eax
jl 0x30fa
xorl %eax, %eax
cmpq $0x1000, %rax           # imm = 0x1000
je 0x30ad
andq $0x0, 0x10(%rsp,%rax,8)
incq %rax
jmp 0x309a
xorl %eax, %eax
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x30f6
cmpq %rbp, %rax
ja 0x30f6
movq 0x10010(%rsp,%rax,8), %rdx
imulq %r15, %rdx
movl %r14d, %ecx
shrq %cl, %rdx
movl %edx, %ecx
movq 0x10(%rsp,%rcx,8), %rdx
movq 0x8010(%rsp,%rax,8), %rsi
testq %rdx, %rdx
je 0x30ea
xorl %ecx, %ecx
cmpq %rsi, %rdx
setne %cl
jmp 0x30f1
movq %rsi, 0x10(%rsp,%rcx,8)
xorl %ecx, %ecx
incq %rax
jmp 0x30b1
testl %ecx, %ecx
je 0x3101
incl %ebx
jmp 0x3059
movq 0x8(%rsp), %rsi
leaq 0x1a7d3(%rip), %rax     # 0x1d8e0
movq (%rax,%rsi,8), %rax
pushq $-0x1
popq %rcx
cmpq %rcx, %r13
je 0x3128
movq 0x18(%rsp,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
incq %rcx
jmp 0x3114
leaq 0x1a5b1(%rip), %rax     # 0x1d6e0
movq %r15, (%rax,%rsi,8)
movq 0x8(%rsp), %rbx
incq %rbx
jmp 0x2faa
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq