// File: /workspace/github_cpp_makefile_star2_O1/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 0x6f533a(%rip), %rax    # 0x6fc100
leaq 0x875833(%rip), %rcx    # 0x87c600
cmovneq %rax, %rcx
movq %rcx, 0x30(%rsp)
leaq 0x6f5523(%rip), %rax    # 0x6fc300
leaq 0x875a1c(%rip), %rcx    # 0x87c800
cmovneq %rax, %rcx
movq %rcx, 0x28(%rsp)
leaq 0x7f580c(%rip), %rax    # 0x7fc600
movq %rax, 0x20(%rsp)
leaq 0x4f5300(%rip), %rax    # 0x4fc100
movq %rax, 0x18(%rsp)
leaq 0x7757f4(%rip), %rax    # 0x77c600
movq %rax, 0x10(%rsp)
leaq 0x2f52e8(%rip), %rax    # 0x2fc100
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
testb %bl, %bl
leaq 0x6f55d8(%rip), %rax    # 0x6fc400
leaq 0xf50d1(%rip), %rdx     # 0xfbf00
cmovneq %rdx, %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rdi
callq 0x3b08
movl $0x1, %edx
movl %eax, %ecx
shll %cl, %edx
movl $0x40, %r12d
subl %eax, %r12d
movl 0x4(%rsp), %r15d
cmpl %r15d, %edx
movq %rdx, 0x40(%rsp)
cmovll %edx, %r15d
callq 0xe965
movq %rax, 0x48(%rsp)
callq 0xe965
movq %rax, %r13
callq 0xe965
movq %rax, %r14
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
callq 0x20c0
andq 0x48(%rsp), %r13
andq %r14, %r13
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdi
movl %ebp, %r8d
testb %bl, %bl
movq %rdx, %rcx
cmovneq %rax, %rcx
movq (%rcx), %r10
imulq %r13, %r10
movl %r12d, %ecx
shrq %cl, %r10
cmpl %r10d, %r15d
jl 0x6f7d
testb %bl, %bl
movq %rdi, %rcx
cmovneq %rsi, %rcx
movq (%rcx), %r9
movslq %r10d, %r10
cmpb $0x1, 0x50(%rsp,%r10)
jne 0x6ef4
cmpq %r9, 0x1050(%rsp,%r10,8)
setne %cl
jmp 0x6ef6
xorl %ecx, %ecx
testb %cl, %cl
jne 0x6e64
movb $0x1, 0x50(%rsp,%r10)
movq %r9, 0x1050(%rsp,%r10,8)
addq $0x8, %rdi
addq $0x8, %rsi
addq $0x8, %rdx
addq $0x8, %rax
decl %r8d
jne 0x6eb1
testb %cl, %cl
jne 0x6e64
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %r13, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
movl %r12d, (%rax,%rcx,4)
incq %rcx
movl $0x2000, %eax           # imm = 0x2000
addq %rax, 0x20(%rsp)
movl $0x8000, %edx           # imm = 0x8000
addq %rdx, 0x18(%rsp)
addq %rax, 0x10(%rsp)
addq %rdx, 0x8(%rsp)
cmpq $0x40, %rcx
jne 0x6e1f
addq $0x9058, %rsp           # imm = 0x9058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2381a(%rip), %rdi     # 0x2a79e
leaq 0x23628(%rip), %rsi     # 0x2a5b3
leaq 0x2384b(%rip), %rcx     # 0x2a7dd
movl $0x101, %edx            # imm = 0x101
callq 0x20d0