// File: /workspace/github_cpp_makefile_star2_O2/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 $0x9048, %rsp           # imm = 0x9048
movl %edi, %ebx
testl %edi, %edi
movl $0x1000, %eax           # imm = 0x1000
movl $0x400, %ecx            # imm = 0x400
cmovnel %eax, %ecx
movl %ecx, 0x4(%rsp)
leaq 0x6f86f5(%rip), %rax    # 0x6ff100
leaq 0x878bee(%rip), %rcx    # 0x87f600
cmovneq %rax, %rcx
movq %rcx, 0x28(%rsp)
leaq 0x6f88de(%rip), %rax    # 0x6ff300
leaq 0x878dd7(%rip), %rcx    # 0x87f800
cmovneq %rax, %rcx
movq %rcx, 0x20(%rsp)
leaq 0x2f86c7(%rip), %r12    # 0x2ff100
leaq 0x778bc0(%rip), %rax    # 0x77f600
movq %rax, 0x18(%rsp)
leaq 0x4f86b4(%rip), %rax    # 0x4ff100
movq %rax, 0x10(%rsp)
leaq 0x7f8ba8(%rip), %rax    # 0x7ff600
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
cmpq $0x40, %rcx
je 0x6b99
testb %bl, %bl
leaq 0x6f898e(%rip), %rax    # 0x6ff400
leaq 0xf8487(%rip), %rdx     # 0xfef00
cmovneq %rdx, %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %rdi
callq 0x3928
pushq $0x1
popq %rcx
movl %ecx, %ebp
movl %eax, %ecx
shll %cl, %ebp
pushq $0x40
popq %rcx
movl %ecx, %r14d
subl %eax, %r14d
movl 0x4(%rsp), %r13d
cmpl %r13d, %ebp
cmovll %ebp, %r13d
movq %r12, 0x38(%rsp)
callq 0xdcd1
movq %rax, %r12
callq 0xdcd1
movq %rax, %r15
callq 0xdcd1
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x6ad4
movb $0x0, 0x40(%rsp,%rcx)
incq %rcx
jmp 0x6ac5
andq %r12, %r15
andq %rax, %r15
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %r12
movq %r12, %rdi
movq %rbp, %r8
subq $0x1, %r8
jb 0x6b5e
testb %bl, %bl
movq %rsi, %rcx
cmovneq %rdi, %rcx
movq (%rcx), %r9
imulq %r15, %r9
movl %r14d, %ecx
shrq %cl, %r9
cmpl %r9d, %r13d
jl 0x6bab
testb %bl, %bl
movq %rax, %rcx
cmovneq %rdx, %rcx
movq (%rcx), %rcx
movslq %r9d, %r9
cmpb $0x1, 0x40(%rsp,%r9)
jne 0x6b3e
cmpq %rcx, 0x1040(%rsp,%r9,8)
jne 0x6aae
movb $0x1, 0x40(%rsp,%r9)
movq %rcx, 0x1040(%rsp,%r9,8)
addq $0x8, %rdi
addq $0x8, %rsi
addq $0x8, %rdx
addq $0x8, %rax
jmp 0x6af4
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %r15, (%rax,%rcx,8)
movq 0x20(%rsp), %rax
movl %r14d, (%rax,%rcx,4)
incq %rcx
movl $0x8000, %eax           # imm = 0x8000
addq %rax, %r12
movl $0x2000, %edx           # imm = 0x2000
addq %rdx, 0x18(%rsp)
addq %rax, 0x10(%rsp)
addq %rdx, 0x8(%rsp)
jmp 0x6a5f
addq $0x9048, %rsp           # imm = 0x9048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21c1c(%rip), %rdi     # 0x287ce
leaq 0x21a2a(%rip), %rsi     # 0x285e3
leaq 0x21c4d(%rip), %rcx     # 0x2880d
movl $0x101, %edx            # imm = 0x101
callq 0x20c0