// File: /workspace/github_cpp_makefile_star2_O1/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 %ebp, %ebp
leaq 0xe3001(%rip), %rcx     # 0xe5ee0
movq $0x0, 0x8(%rsp)
leaq 0xe31f1(%rip), %rax     # 0xe60e0
testq %rbp, %rbp
je 0x2f02
movslq 0x8(%rsp), %rax
shlq $0x3, %rax
addq -0x8(%rcx,%rbp,8), %rax
movq %rax, (%rcx,%rbp,8)
leaq 0x8ed3(%rip), %rax      # 0xbde0
movq (%rax,%rbp,8), %rbx
leaq 0xe2bc8(%rip), %rax     # 0xe5ae0
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 0x2a41
movq %rax, %r12
movl %ebp, %edi
movq %r15, %rsi
callq 0x2aa8
orq %r12, %rax
xorq %rbx, %rax
movq %rax, 0x8010(%rsp,%r14,8)
leaq 0x1(%r14), %rax
subq %r13, %r15
andq %r13, %r15
jne 0x2f21
movq %rax, 0x8(%rsp)
movl $0x40, %r15d
movq %rbp, (%rsp)
leaq 0x51bd(%rip), %rax      # 0x8130
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 0xe2b3b(%rip), %rcx     # 0xe5ae0
movq (%rcx,%rax,8), %rax
imulq %r12, %rax
movabsq $-0x100000000000000, %rcx # imm = 0xFF00000000000000
andq %rcx, %rax
je 0x3032
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x1(%rax), %rdx
incl %ecx
andq %rdx, %rax
jne 0x2fc1
cmpl $0x5, %ecx
jb 0x3032
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 0x3019
cmpq %rsi, %rdx
je 0x301e
movl $0x1, %eax
jmp 0x301e
movq %rsi, 0x10(%rsp,%rcx,8)
testl %eax, %eax
jne 0x302e
leaq 0x1(%r13), %rcx
cmpq %rbx, %r13
movq %rcx, %r13
jb 0x2fe7
testl %eax, %eax
je 0x3042
incl %ebp
cmpl $0x3b9aca00, %ebp       # imm = 0x3B9ACA00
jne 0x2f7c
jmp 0x306e
movq (%rsp), %rbx
leaq 0xe2e93(%rip), %rax     # 0xe5ee0
movq (%rax,%rbx,8), %rdi
leaq 0x8(,%r14,8), %rdx
leaq 0x10(%rsp), %rsi
callq 0x2100
leaq 0xe2c76(%rip), %rax     # 0xe5ce0
movq %r12, (%rax,%rbx,8)
movq (%rsp), %rbp
incq %rbp
cmpq $0x40, %rbp
leaq 0xe2e60(%rip), %rcx     # 0xe5ee0
jne 0x2ee8
addq $0x18018, %rsp          # imm = 0x18018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq