// File: /workspace/github_cpp_makefile_star2_O1/ATOMIC-LICHESS[P]ATOMKRAFT/bitbase.cpp
// Function: init_kpk_bitbase()
void init_kpk_bitbase() {

  Result bb[IndexMax];
  KPKPosition pos;
  bool repeat;

  // Initialize table
  for (int i = 0; i < IndexMax; i++)
  {
      pos.from_index(i);
      bb[i] = !pos.is_legal()          ? RESULT_INVALID
             : pos.is_immediate_draw() ? RESULT_DRAW
             : pos.is_immediate_win()  ? RESULT_WIN : RESULT_UNKNOWN;
  }

  // Iterate until all positions are classified (30 cycles needed)
  do {
      repeat = false;

      for (int i = 0; i < IndexMax; i++)
          if (bb[i] == RESULT_UNKNOWN)
          {
              pos.from_index(i);

              bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
                                                : classify_btm(pos, bb);
              if (bb[i] != RESULT_UNKNOWN)
                  repeat = true;
          }

  } while (repeat);

  // Map 32 position results into one KPKBitbase[] entry
  for (int i = 0; i < IndexMax / 32; i++)
      for (int j = 0; j < 32; j++)
          if (bb[32 * i + j] == RESULT_WIN || bb[32 * i + j] == RESULT_LOSS)
              KPKBitbase[i] |= (1 << j);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc0028, %rsp          # imm = 0xC0028
xorl %eax, %eax
leaq 0x110f8e(%rip), %rbp    # 0x118750
leaq 0x1109d7(%rip), %rcx    # 0x1181a0
movl %eax, %edi
shrl $0xd, %edi
imulq $0xaaaaaab, %rdi, %rdx  # imm = 0xAAAAAAB
shrq $0x20, %rdx
shll $0x3, %edx
leal (%rdx,%rdx,2), %edx
subl %edx, %edi
movl %eax, %r10d
shrl $0x7, %r10d
andl $0x3f, %r10d
movl %edi, %r8d
andl $0x3, %r8d
andl $0x1c, %edi
leal (%r8,%rdi,2), %edx
addl $0x8, %edx
movl $0x1, %r11d
cmpl %edx, %r10d
jne 0x781d
movl %r11d, 0x20(%rsp,%rax,4)
incq %rax
cmpq $0x30000, %rax          # imm = 0x30000
jne 0x77c9
jmp 0x794a
movl %eax, %ebx
shrl %ebx
movl %ebx, %r9d
andl $0x3f, %r9d
cmpl %r9d, %r10d
je 0x7808
cmpl %edx, %r9d
je 0x7808
movl %r10d, %esi
movl %r9d, %r14d
testb $0x1, %al
jne 0x7859
movq (%rcx,%r14,8), %r15
testq %r15, 0xc00(%rbp,%rsi,8)
jne 0x7808
movl %edx, %r12d
testq %r15, 0x200(%rbp,%r12,8)
jne 0x7808
jmp 0x7867
movq (%rcx,%rsi,8), %r15
testq %r15, 0x1c00(%rbp,%r14,8)
jne 0x7808
testb $0x1, %al
jne 0x788e
cmpl $0x38, %r10d
jne 0x78d0
cmpl $0x30, %edx
jne 0x78d0
leal -0x32(%r9), %r11d
testl $0xfffffff7, %r11d      # imm = 0xFFFFFFF7
jne 0x78d0
movl $0x4, %r11d
jmp 0x7808
movq 0xc00(%rbp,%rsi,8), %r15
movq 0x1c00(%rbp,%r14,8), %r14
movl %edx, %r12d
movq 0x200(%rbp,%r12,8), %r13
orq %r15, %r13
notq %r13
movl $0x4, %r11d
testq %r13, %r14
je 0x7808
movq (%rcx,%r12,8), %r12
testq %r14, %r12
je 0x78d0
andq %r15, %r12
je 0x7808
xorl %r11d, %r11d
testb $0x1, %al
jne 0x7808
leal 0x8(,%rdi,2), %edi
cmpl $0x30, %edi
jne 0x7808
orl $0x8, %edx
cmpl %edx, %r10d
je 0x7808
andl $0x7, %ebx
subl %r8d, %ebx
movl %ebx, %edi
negl %edi
cmovsl %ebx, %edi
shrl $0x3, %r9d
addl $-0x7, %r9d
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
cmpl %r8d, %edi
cmoval %edi, %r8d
movl $0x2, %r11d
cmpl $0x1, %r8d
ja 0x7808
movl %edx, %edx
movq (%rcx,%rdx,8), %rdx
xorl %r11d, %r11d
testq %rdx, 0xc00(%rbp,%rsi,8)
setne %r11b
addl %r11d, %r11d
jmp 0x7808
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpl $0x0, 0x20(%rsp,%r15,4)
jne 0x7b62
movq %r14, 0x8(%rsp)
movl %r15d, %r13d
shrl $0xd, %r13d
imulq $0xaaaaaab, %r13, %rax  # imm = 0xAAAAAAB
shrq $0x20, %rax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
subl %eax, %r13d
movl %r15d, %edx
shrl %edx
andl $0x3f, %edx
movl %r15d, %r14d
shrl $0x7, %r14d
andl $0x3f, %r14d
movl %r13d, %r12d
andl $0x1c, %r12d
leal 0x8(,%r12,2), %eax
testb $0x1, %r15b
jne 0x7a08
movl %r14d, %ecx
movq 0xc00(%rbp,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
movq %rdx, 0x18(%rsp)
je 0x7a7b
shrl %eax
movl %r13d, %ecx
orl $0x7fffc, %ecx          # imm = 0x7FFFC
addl %eax, %ecx
shll $0xd, %ecx
leal (%rcx,%rdx,2), %ebp
incl %ebp
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x7d37
shll $0x7, %eax
addl %ebp, %eax
cltq
movl 0x20(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x79fc
cmpl $0x3, %eax
jne 0x79fe
jmp 0x7b3d
movb $0x1, %bl
cmpq $0x0, 0x10(%rsp)
jne 0x79d9
jmp 0x7a7d
movl %edx, %ecx
movq 0x1c00(%rbp,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x7b36
shrl %eax
orl $0x7fffc, %r13d         # imm = 0x7FFFC
addl %eax, %r13d
shll $0x7, %r14d
shll $0xd, %r13d
orl %r14d, %r13d
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
leaq 0x10(%rsp), %rdi
callq 0x7d37
leal (%r13,%rax,2), %eax
cltq
movl 0x20(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x7a61
cmpl $0x4, %eax
jne 0x7a63
jmp 0x7b4e
movb $0x1, %bl
cmpq $0x0, 0x10(%rsp)
jne 0x7a3e
notb %bl
movzbl %bl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %eax
jmp 0x7b4e
xorl %ebx, %ebx
cmpl $0x13, %r13d
ja 0x7b1d
movl %r13d, %eax
andl $0x3, %eax
leal (%rax,%r12,2), %edx
addl $0x8, %edx
addl $0x8, %edx
movl %edx, %eax
shrl $0x3, %eax
movl %r13d, %ecx
orl $-0x4, %ecx
leal (%rcx,%rax,4), %eax
movl %r14d, %esi
shll $0x7, %esi
shll $0xd, %eax
movq 0x18(%rsp), %r8
leal (%rsi,%r8,2), %edi
addl %edi, %eax
incl %eax
cltq
movl 0x20(%rsp,%rax,4), %edi
movl $0x2, %eax
cmpl $0x3, %edi
leaq 0x110c80(%rip), %rbp    # 0x118750
je 0x7b49
testl %edi, %edi
jne 0x7ad8
movb $0x1, %bl
movl %edx, %edi
andl $-0x8, %edi
cmpl $0x10, %edi
jne 0x7b24
cmpl %r14d, %edx
je 0x7b24
cmpl %r8d, %edx
movq 0x8(%rsp), %r14
je 0x7b29
leal (%rsi,%r8,2), %edx
incl %edx
addl $0xc, %r13d
andl $0x3c, %r13d
addl %ecx, %r13d
shll $0xd, %r13d
orl %edx, %r13d
movslq %r13d, %rcx
movl 0x20(%rsp,%rcx,4), %ecx
cmpl $0x3, %ecx
je 0x7b4e
testl %ecx, %ecx
jne 0x7b29
movb $0x1, %bl
jmp 0x7b29
leaq 0x110c2c(%rip), %rbp    # 0x118750
movq 0x8(%rsp), %r14
notb %bl
movzbl %bl, %eax
andl $0x1, %eax
shll $0x2, %eax
jmp 0x7b4e
movl $0x3, %eax
jmp 0x7b49
movl $0x2, %eax
leaq 0x110c07(%rip), %rbp    # 0x118750
movq 0x8(%rsp), %r14
movl %eax, 0x20(%rsp,%r15,4)
testl %eax, %eax
movzbl %r14b, %r14d
movl $0x1, %eax
cmovnel %eax, %r14d
incq %r15
cmpq $0x30000, %r15          # imm = 0x30000
jne 0x7950
xorl %r15d, %r15d
testb $0x1, %r14b
movl $0x0, %r14d
jne 0x7950
leaq 0x20(%rsp), %rax
xorl %edx, %edx
leaq 0x37bfd(%rip), %rsi     # 0x3f790
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edi
andl $-0x2, %edi
cmpl $0x2, %edi
jne 0x7baa
movl $0x1, %edi
shll %cl, %edi
orl %edi, (%rsi,%rdx,4)
incq %rcx
cmpq $0x20, %rcx
jne 0x7b95
incq %rdx
subq $-0x80, %rax
cmpq $0x1800, %rdx           # imm = 0x1800
jne 0x7b93
addq $0xc0028, %rsp          # imm = 0xC0028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)