// File: jswaff[P]prophet3/src/magic.cpp
// Function: init_rook_occupancy_variations()
void init_rook_occupancy_variations() {
    for (uint32 sq=0;sq<64;sq++) {
        uint64 mask = bb_rook_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 <= 4096);

        for (uint32 i=0;i<num_variations;i++) {
            bb_rook_occ_var[sq][i] = bb_rook_attack_set[sq][i] = 0;

            // map the index to an occupancy variation
            int32 index = i;
            while (index) {
                uint32 index_bit = get_lsb(index);
                bb_rook_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_rook_occ_var[sq][i] != bb_rook_occ_var[sq][j]);
            }

            // create the attack set.  that's the first "blocker" in every direction
            square_t to = north((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_rook_occ_var[sq][i] & bb_squares[to]) {
                    bb_rook_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = north(to);
            }
            to = south((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_rook_occ_var[sq][i] & bb_squares[to]) {
                    bb_rook_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = south(to);
            }
            to = east((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_rook_occ_var[sq][i] & bb_squares[to]) {
                    bb_rook_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = east(to);
            }
            to = west((square_t)sq);
            while (to != NO_SQUARE) {
                if (bb_rook_occ_var[sq][i] & bb_squares[to]) {
                    bb_rook_attack_set[sq][i] |= bb_squares[to];
                    break;
                }
                to = west(to);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
cmpl $0x40, -0x4(%rbp)
jae 0x9c71
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0xfd7cb(%rip), %rax     # 0x1070c0
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 $0x1000, -0x18(%rbp)    # imm = 0x1000
ja 0x9921
jmp 0x9940
leaq 0x2ce2d(%rip), %rdi     # 0x36755
leaq 0x2cca9(%rip), %rsi     # 0x365d8
movl $0xb2, %edx
leaq 0x2ce31(%rip), %rcx     # 0x3676c
callq 0x20c0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x9c61
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x4fd961(%rip), %rax    # 0x5072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd943(%rip), %rax    # 0x3072c0
shlq $0xf, %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 0x99e6
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 0x2fd8fe(%rip), %rax    # 0x3072c0
shlq $0xf, %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 0x9995
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x9a59
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd8bf(%rip), %rax    # 0x3072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x4(%rbp), %ecx
movl %ecx, %edx
leaq 0x2fd8a5(%rip), %rcx    # 0x3072c0
shlq $0xf, %rdx
addq %rdx, %rcx
movl -0x28(%rbp), %edx
cmpq (%rcx,%rdx,8), %rax
je 0x9a2d
jmp 0x9a4c
leaq 0x2cd5e(%rip), %rdi     # 0x36792
leaq 0x2cb9d(%rip), %rsi     # 0x365d8
movl $0xc1, %edx
leaq 0x2cd25(%rip), %rcx     # 0x3676c
callq 0x20c0
jmp 0x9a4e
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x99ed
movl -0x4(%rbp), %edi
callq 0x12410
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9ad7
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd84a(%rip), %rax    # 0x3072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbbe0(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9aca
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbbca(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x4fd80a(%rip), %rax    # 0x5072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9ad7
movl -0x2c(%rbp), %edi
callq 0x12410
movl %eax, -0x2c(%rbp)
jmp 0x9a64
movl -0x4(%rbp), %edi
callq 0x12490
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9b55
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd7cc(%rip), %rax    # 0x3072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbb62(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9b48
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbb4c(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x4fd78c(%rip), %rax    # 0x5072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9b55
movl -0x2c(%rbp), %edi
callq 0x12490
movl %eax, -0x2c(%rbp)
jmp 0x9ae2
movl -0x4(%rbp), %edi
callq 0x12510
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9bd3
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd74e(%rip), %rax    # 0x3072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfbae4(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9bc6
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfbace(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x4fd70e(%rip), %rax    # 0x5072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9bd3
movl -0x2c(%rbp), %edi
callq 0x12510
movl %eax, -0x2c(%rbp)
jmp 0x9b60
movl -0x4(%rbp), %edi
callq 0x12590
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
je 0x9c51
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x2fd6d0(%rip), %rax    # 0x3072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0xfba66(%rip), %rcx     # 0x105670
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x9c44
movl -0x2c(%rbp), %eax
movl %eax, %ecx
leaq 0xfba50(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rdx
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x4fd690(%rip), %rax    # 0x5072c0
shlq $0xf, %rcx
addq %rcx, %rax
movl -0x1c(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x9c51
movl -0x2c(%rbp), %edi
callq 0x12590
movl %eax, -0x2c(%rbp)
jmp 0x9bde
jmp 0x9c53
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9947
jmp 0x9c63
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x98df
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)