// File: /workspace/github_cpp_makefile_star2_O3/jswaff[P]prophet3/src/magic.cpp
// Function: init_magic_numbers(bool)
void init_magic_numbers(bool is_rook) {
    for (uint32 sq=0;sq<64;sq++) {
        uint64 mask = is_rook ? bb_rook_masks[sq] : bb_bishop_masks[sq];
        uint32 num_mask_bits = get_num_bits(mask);
        uint32 num_variations = 1 << num_mask_bits;

        uint64 magic;
        uint32 magic_shift = 64 - num_mask_bits;

        bool fail;
        bool is_used[4096];
        uint64 used_by[4096];

        do {
            magic = random64() & random64() & random64(); // not many bits set

            for (uint32 i=0;i<num_variations;i++) {
                is_used[i] = false;
            }

            fail = false;
            for (uint32 i=0;i<num_variations;i++) {
                uint64 occupied = is_rook ? bb_rook_occ_var[sq][i] : bb_bishop_occ_var[sq][i];
                int index = (int)((occupied * magic) >> magic_shift);
                assert(index <= (1 << num_mask_bits) && index <= (is_rook ?4096:1024));

                // fail if this index is used by an attack set that is incorrect
                // for this occupancy variation
                uint64 attack_set = is_rook ? bb_rook_attack_set[sq][i] : bb_bishop_attack_set[sq][i];
                fail = is_used[index] && (used_by[index] != attack_set);
                if (fail) {
                    break;
                }
                is_used[index] = true;
                used_by[index] = attack_set;
            }
        } while (fail);

        if (is_rook) {
            magic_numbers_rooks[sq] = magic;
            magic_numbers_shift_rooks[sq] = magic_shift;
        } else {
            magic_numbers_bishops[sq] = magic;
            magic_numbers_shift_bishops[sq] = magic_shift;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9058, %rsp           # imm = 0x9058
movl %edi, %ebx
testl %edi, %edi
movl $0x1000, %eax           # imm = 0x1000
movl $0x400, %ecx            # imm = 0x400
cmovnel %eax, %ecx
movl %ecx, 0x4(%rsp)
leaq 0x6f5284(%rip), %rax    # 0x6fc100
leaq 0x87577d(%rip), %rcx    # 0x87c600
cmovneq %rax, %rcx
movq %rcx, 0x20(%rsp)
leaq 0x6f546d(%rip), %rax    # 0x6fc300
leaq 0x875966(%rip), %rcx    # 0x87c800
cmovneq %rax, %rcx
movq %rcx, 0x18(%rsp)
leaq 0x2f5256(%rip), %rbp    # 0x2fc100
leaq 0x77574f(%rip), %r14    # 0x77c600
leaq 0x4f5248(%rip), %rax    # 0x4fc100
movq %rax, 0x10(%rsp)
leaq 0x7f573c(%rip), %rax    # 0x7fc600
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
testb %bl, %bl
leaq 0x6f552c(%rip), %rax    # 0x6fc400
leaq 0xf5025(%rip), %rdx     # 0xfbf00
cmovneq %rdx, %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rdi
callq 0x3b14
movl $0x1, %edx
movl %eax, %ecx
shll %cl, %edx
movl $0x40, %r12d
subl %eax, %r12d
movl 0x4(%rsp), %r15d
cmpl %r15d, %edx
movq %rdx, 0x30(%rsp)
cmovll %edx, %r15d
movq %r14, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
callq 0xe8bd
movq %rax, 0x48(%rsp)
callq 0xe8bd
movq %rax, %r13
callq 0xe8bd
movq %rax, %r14
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
callq 0x20c0
andq 0x48(%rsp), %r13
andq %r14, %r13
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r14
movq %rbp, %rax
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq %r14, %rdi
movq %r8, %rbp
testb %bl, %bl
movq %rdi, %rcx
cmovneq %r8, %rcx
movq (%rcx), %r9
imulq %r13, %r9
movl %r12d, %ecx
shrq %cl, %r9
cmpl %r9d, %r15d
jl 0x7024
testb %bl, %bl
movq %rdx, %rcx
cmovneq %rsi, %rcx
movq (%rcx), %rcx
movslq %r9d, %r9
cmpb $0x1, 0x50(%rsp,%r9)
jne 0x6fb1
cmpq %rcx, 0x1050(%rsp,%r9,8)
jne 0x6f1a
movb $0x1, 0x50(%rsp,%r9)
movq %rcx, 0x1050(%rsp,%r9,8)
addq $0x8, %r8
addq $0x8, %rdi
addq $0x8, %rsi
addq $0x8, %rdx
decq %rax
jne 0x6f6d
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %r13, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movl %r12d, (%rax,%rcx,4)
incq %rcx
movl $0x8000, %eax           # imm = 0x8000
addq %rax, %rbp
movl $0x2000, %edx           # imm = 0x2000
addq %rdx, %r14
addq %rax, 0x10(%rsp)
addq %rdx, 0x8(%rsp)
cmpq $0x40, %rcx
jne 0x6ecb
addq $0x9058, %rsp           # imm = 0x9058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x237f3(%rip), %rdi     # 0x2a81e
leaq 0x23601(%rip), %rsi     # 0x2a633
leaq 0x23824(%rip), %rcx     # 0x2a85d
movl $0x101, %edx            # imm = 0x101
callq 0x20d0