// File: jswaff[P]prophet3/src/magic.cpp
// Function: init_bishop_occupancy_variations()
void init_bishop_occupancy_variations() {
    for (uint32 sq=0;sq<64;sq++) {
        uint64 mask = bb_bishop_masks[sq];
        uint32 num_mask_bits = get_num_bits(mask);
        uint32 num_variations = 1 << num_mask_bits; //pow(2,num_mask_bits);
        assert(num_variations <= 1024);

        for (uint32 i=0;i<num_variations;i++) {
            bb_bishop_occ_var[sq][i] = bb_bishop_attack_set[sq][i] = 0;

            // map the index to an occupancy variation
            int32 index = i;
            while (index) {
                uint32 index_bit = get_lsb(index);
                bb_bishop_occ_var[sq][i] |= isolate_lsb_bit(mask,index_bit);
                index ^= 1 << index_bit;
            }

            // sanity check
            for (uint32 j=0;j<i;j++) {
                assert(bb_bishop_occ_var[sq][i] != bb_bishop_occ_var[sq][j]);
            }

            // create the attack set.  that's the first "blocker" in every direction
            square_t to = northeast((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_bishop_occ_var[sq][i] & bb_squares[to]) {
                    bb_bishop_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = northeast(to);
            }
            to = southeast((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_bishop_occ_var[sq][i] & bb_squares[to]) {
                    bb_bishop_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = southeast(to);
            }
            to = southwest((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_bishop_occ_var[sq][i] & bb_squares[to]) {
                    bb_bishop_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = southwest(to);
            }
            to = northwest((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_bishop_occ_var[sq][i] & bb_squares[to]) {
                    bb_bishop_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = northwest(to);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
cmpl $0x40, -0x4(%rbp)
jae 0x98c1
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x6fe07b(%rip), %rax    # 0x7075c0
movq (%rax,%rcx,8), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x47b0
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x18(%rbp)
cmpl $0x400, -0x18(%rbp)     # imm = 0x400
ja 0x9571
jmp 0x9590
leaq 0x2d169(%rip), %rdi     # 0x366e1
leaq 0x2d059(%rip), %rsi     # 0x365d8
movl $0x76, %edx
leaq 0x2d16d(%rip), %rcx     # 0x366f8
callq 0x20c0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x98b1
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x7fe211(%rip), %rax    # 0x8077c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77e1f3(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x9636
movslq -0x20(%rbp), %rdi
callq 0x4870
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x4b00
movq %rax, %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77e1ae(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
xorl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x95e5
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x96a9
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77e16f(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x4(%rbp), %ecx
movl %ecx, %edx
leaq 0x77e155(%rip), %rcx    # 0x7877c0
shlq $0xd, %rdx
addq %rdx, %rcx
movl -0x28(%rbp), %edx
cmpq (%rcx,%rdx,8), %rax
je 0x967d
jmp 0x969c
leaq 0x2d09c(%rip), %rdi     # 0x36720
leaq 0x2cf4d(%rip), %rsi     # 0x365d8
movl $0x85, %edx
leaq 0x2d061(%rip), %rcx     # 0x366f8
callq 0x20c0
jmp 0x969e
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x963d
movl -0x4(%rbp), %edi
callq 0x12610
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9727
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77e0fa(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbf90(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x971a
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbf7a(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x7fe0ba(%rip), %rax    # 0x8077c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9727
movl -0x2c(%rbp), %edi
callq 0x12610
movl %eax, -0x2c(%rbp)
jmp 0x96b4
movl -0x4(%rbp), %edi
callq 0x126a0
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x97a5
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77e07c(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbf12(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9798
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbefc(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x7fe03c(%rip), %rax    # 0x8077c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x97a5
movl -0x2c(%rbp), %edi
callq 0x126a0
movl %eax, -0x2c(%rbp)
jmp 0x9732
movl -0x4(%rbp), %edi
callq 0x12730
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9823
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77dffe(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbe94(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9816
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbe7e(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x7fdfbe(%rip), %rax    # 0x8077c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9823
movl -0x2c(%rbp), %edi
callq 0x12730
movl %eax, -0x2c(%rbp)
jmp 0x97b0
movl -0x4(%rbp), %edi
callq 0x127c0
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x98a1
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x77df80(%rip), %rax    # 0x7877c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbe16(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9894
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbe00(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x7fdf40(%rip), %rax    # 0x8077c0
shlq $0xd, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x98a1
movl -0x2c(%rbp), %edi
callq 0x127c0
movl %eax, -0x2c(%rbp)
jmp 0x982e
jmp 0x98a3
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9597
jmp 0x98b3
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x952f
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)