// File: /workspace/github_cpp_makefile_star2_O2/bjohnsonfl[P]Overhead-Chess-Engine-/bitboard.cpp
// Function: initMagicBishop()
void initMagicBishop ()
{
    bitboard blockers[4096], attackers[4096], b, magic;
    bitboard used[4096];
    int size = 0;
    bitboard * idx;
    for ( square sq = a1; sq < numOfSq; sq++)
    {
        magicBishopIndex[sq] = sq == a1 ? magicBishopAttacks : magicBishopIndex[sq - 1] + (size );
        b = 0;
        size = 0;
        do {
            
            blockers[size] = b;
            attackers[size] = squareBitMask[sq]^(diagMasks(sq, b) | antidiagMasks(sq, b));
            size++;
            b = (b - magicBishopMask[sq]) & magicBishopMask[sq];
        } while(b);
        
        int n = bishopShifts [sq];
        int fail = 0;
        unsigned index = 0;
        
        for(int k = 0; k < 1000000000; k++) {
            
            magic = randomU64_fewbits();
            if(popcount((magicBishopMask[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<< "magic: " << magic << " sq: " << sq  << "\n";
              idx = magicBishopIndex[sq];
                for(int i = 0; i < size; i++) {
                    idx[i] = used[i];
                }
                magicBishopNumbers[sq] = magic;
                /*////////////////
                //TEST
               b = 0;
                do {
                    
                   // printBitboard(b);
                  //  printBitboard( *(magicBishopIndex[sq] + magicIndex(b, magic, bishopShifts[sq])));
                   // printBitboard(squareBitMask[sq]^(diagMasks(sq, b) | antidiagMasks(sq, b)));
                    if(( *(magicBishopIndex[sq] + magicIndex(b, magic, bishopShifts[sq]))) != (squareBitMask[sq]^(diagMasks(sq, b) | antidiagMasks(sq, b)))) std::cout << "MAGIC DOESNT WORK \n";
               
                    b = (b - magicBishopMask[sq]) & magicBishopMask[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 0xe32ed(%rip), %rax     # 0xe60e0
testq %rbx, %rbx
je 0x2e15
cmpq $0x40, %rbx
je 0x2f82
movslq %r12d, %rax
shlq $0x3, %rax
leaq 0xe30d0(%rip), %rcx     # 0xe5ee0
addq -0x8(%rcx,%rbx,8), %rax
leaq 0xe30c4(%rip), %rcx     # 0xe5ee0
movq %rax, (%rcx,%rbx,8)
leaq 0x8fb9(%rip), %rax      # 0xbde0
movq (%rax,%rbx,8), %rax
movq %rax, 0x8(%rsp)
leaq 0xe2ca9(%rip), %rax     # 0xe5ae0
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 0x291f
movq %rax, %r15
movl %ebx, %edi
movq %r14, %rsi
callq 0x298c
orq %r15, %rax
xorq 0x8(%rsp), %rax
movq %rax, 0x8010(%rsp,%r12,8)
incq %r12
subq %rbp, %r14
andq %rbp, %r14
jne 0x2e41
pushq $0x40
popq %rax
movl %eax, %r14d
movq %rbx, 0x8(%rsp)
leaq 0x42d0(%rip), %rax      # 0x7160
subl (%rax,%rbx,4), %r14d
leal -0x1(%r12), %ebp
xorl %ebx, %ebx
cmpl $0x3b9aca00, %ebx       # imm = 0x3B9ACA00
je 0x2f75
callq 0x27da
movq %rax, %r15
movq 0x8(%rsp), %rax
leaq 0xe2c25(%rip), %rcx     # 0xe5ae0
movq (%rcx,%rax,8), %rdi
imulq %r15, %rdi
movabsq $-0x100000000000000, %rax # imm = 0xFF00000000000000
andq %rax, %rdi
callq 0x2801
cmpl $0x6, %eax
jl 0x2f3c
xorl %eax, %eax
cmpq $0x1000, %rax           # imm = 0x1000
je 0x2eef
andq $0x0, 0x10(%rsp,%rax,8)
incq %rax
jmp 0x2edc
xorl %eax, %eax
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x2f38
cmpq %rbp, %rax
ja 0x2f38
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 0x2f2c
xorl %ecx, %ecx
cmpq %rsi, %rdx
setne %cl
jmp 0x2f33
movq %rsi, 0x10(%rsp,%rcx,8)
xorl %ecx, %ecx
incq %rax
jmp 0x2ef3
testl %ecx, %ecx
je 0x2f43
incl %ebx
jmp 0x2e9b
movq 0x8(%rsp), %rsi
leaq 0xe2f91(%rip), %rax     # 0xe5ee0
movq (%rax,%rsi,8), %rax
pushq $-0x1
popq %rcx
cmpq %rcx, %r13
je 0x2f6a
movq 0x18(%rsp,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
incq %rcx
jmp 0x2f56
leaq 0xe2d6f(%rip), %rax     # 0xe5ce0
movq %r15, (%rax,%rsi,8)
movq 0x8(%rsp), %rbx
incq %rbx
jmp 0x2dec
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq