// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0xe2f22(%rip), %rcx     # 0xe5ee0
xorl %ebp, %ebp
leaq 0xe3119(%rip), %rax     # 0xe60e0
testq %rbx, %rbx
je 0x2fd8
movslq %ebp, %rax
shlq $0x3, %rax
addq -0x8(%rcx,%rbx,8), %rax
movq %rax, (%rcx,%rbx,8)
leaq 0x8dfd(%rip), %rax      # 0xbde0
movq (%rax,%rbx,8), %rax
movq %rax, (%rsp)
leaq 0xe2aee(%rip), %rax     # 0xe5ae0
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 0x2acc
movq %rax, %r12
movl %ebx, %edi
movq %r15, %rsi
callq 0x2b39
orq %r12, %rax
xorq (%rsp), %rax
movq %rax, 0x8010(%rsp,%rbp,8)
incq %rbp
subq %r13, %r15
andq %r13, %r15
jne 0x2ffb
movl $0x40, %r15d
leaq 0x515c(%rip), %rax      # 0x81a0
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 0xe2a5b(%rip), %rax     # 0xe5ae0
movq (%rax,%rbx,8), %rax
imulq %r12, %rax
movabsq $-0x100000000000000, %rcx # imm = 0xFF00000000000000
andq %rcx, %rax
je 0x310a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x1(%rax), %rdx
incl %ecx
andq %rdx, %rax
jne 0x30a1
cmpl $0x5, %ecx
jb 0x310a
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 0x30f6
cmpq %rcx, %rdx
jne 0x3105
cmpq %r13, %rax
jb 0x3100
jmp 0x3105
movq %rcx, 0x10(%rsp,%rsi,8)
cmpq %r13, %rax
jae 0x311c
incq %rax
jmp 0x30c4
cmpq %rcx, %rdx
je 0x311c
movl (%rsp), %eax
incl %eax
cmpl $0x3b9aca00, %eax       # imm = 0x3B9ACA00
jne 0x3052
jmp 0x3144
leaq 0xe2dbd(%rip), %rax     # 0xe5ee0
movq (%rax,%rbx,8), %rdi
leaq 0x8(,%r14,8), %rdx
leaq 0x10(%rsp), %rsi
callq 0x2100
leaq 0xe2ba0(%rip), %rax     # 0xe5ce0
movq %r12, (%rax,%rbx,8)
incq %rbx
cmpq $0x40, %rbx
leaq 0xe2d8e(%rip), %rcx     # 0xe5ee0
jne 0x2fc0
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq