// File: /workspace/github_cpp_makefile_star2_O3/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 0x1119e6(%rip), %r13    # 0x119750
leaq 0x11142f(%rip), %rcx    # 0x1191a0
movl %eax, %edi
shrl $0xd, %edi
movl %eax, %r8d
shrl $0x7, %r8d
andl $0x3f, %r8d
andl $0x3, %edi
movl %eax, %ebx
shrl $0xc, %ebx
andl $0x38, %ebx
leal (%rbx,%rdi), %edx
addl $0x8, %edx
movl $0x1, %esi
cmpl %edx, %r8d
jne 0x7db0
movl %esi, 0x20(%rsp,%rax,4)
incq %rax
cmpq $0x30000, %rax          # imm = 0x30000
jne 0x7d71
jmp 0x7ea3
movl %eax, %r10d
shrl %r10d
movl %r10d, %r9d
andl $0x3f, %r9d
cmpl %r9d, %r8d
je 0x7d9c
cmpl %edx, %r9d
je 0x7d9c
movl %r8d, %r11d
movl %r9d, %r14d
testb $0x1, %al
jne 0x7e0d
movq 0xc00(%r13,%r11,8), %r11
movq (%rcx,%r14,8), %r14
testq %r11, %r14
jne 0x7d9c
movl %edx, %r15d
testq %r14, 0x200(%r13,%r15,8)
jne 0x7d9c
cmpl $0x38, %r8d
jne 0x7e55
cmpl $0x30, %edx
jne 0x7e55
leal -0x32(%r9), %esi
testl $0xfffffff7, %esi       # imm = 0xFFFFFFF7
jne 0x7e5d
movl $0x4, %esi
jmp 0x7d9c
movq 0x1c00(%r13,%r14,8), %rdi
testq %rdi, (%rcx,%r11,8)
jne 0x7d9c
movq 0xc00(%r13,%r11,8), %r8
movl %edx, %edx
movq 0x200(%r13,%rdx,8), %r9
orq %r8, %r9
notq %r9
movl $0x4, %esi
testq %r9, %rdi
je 0x7d9c
movq (%rcx,%rdx,8), %rdx
testq %rdi, %rdx
je 0x7e65
andq %r8, %rdx
je 0x7d9c
jmp 0x7e65
addl $0x8, %ebx
cmpl $0x30, %ebx
jne 0x7e65
orl $0x8, %edx
cmpl %edx, %r8d
jne 0x7e6c
xorl %esi, %esi
jmp 0x7d9c
andl $0x7, %r10d
subl %edi, %r10d
movl %r10d, %esi
negl %esi
cmovsl %r10d, %esi
shrl $0x3, %r9d
xorl $0x7, %r9d
cmpl %r9d, %esi
cmoval %esi, %r9d
cmpl $0x1, %r9d
ja 0x7e99
movl %edx, %edx
testq %r11, (%rcx,%rdx,8)
je 0x7e65
movl $0x2, %esi
jmp 0x7d9c
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %ecx, %ecx
cmpl $0x0, 0x20(%rsp,%r12,4)
je 0x7eba
movl %ecx, %eax
jmp 0x80a4
movq %rcx, 0x18(%rsp)
movl %r12d, %ebp
shrl $0xd, %ebp
movl %r12d, %ebx
shrl %ebx
andl $0x3f, %ebx
movl %r12d, %r14d
shrl $0x7, %r14d
andl $0x3f, %r14d
movl %r12d, %r15d
shrl $0xc, %r15d
andl $0x38, %r15d
leal 0x8(%r15), %eax
testb $0x1, %r12b
jne 0x7f58
movl %r14d, %ecx
movq 0xc00(%r13,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x7fc5
shrl %eax
movl %ebp, %ecx
orl $0x7fffc, %ecx          # imm = 0x7FFFC
addl %eax, %ecx
shll $0xd, %ecx
leal (%rcx,%rbx,2), %r13d
incl %r13d
movl $0x0, 0xc(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x827b
shll $0x7, %eax
addl %r13d, %eax
cltq
movl 0x20(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x7f48
cmpl $0x3, %eax
jne 0x7f4e
jmp 0x8084
movb $0x1, %al
movl %eax, 0xc(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x7f24
jmp 0x7fcd
movl %ebx, %ecx
movq 0x1c00(%r13,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
je 0x807d
shrl %eax
orl $0x7fffc, %ebp          # imm = 0x7FFFC
addl %eax, %ebp
shll $0x7, %r14d
shll $0xd, %ebp
orl %r14d, %ebp
xorl %ebx, %ebx
xorl %r14d, %r14d
leaq 0x10(%rsp), %rdi
callq 0x827b
leal (%rbp,%rax,2), %eax
cltq
movl 0x20(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x7fab
cmpl $0x4, %eax
jne 0x7fad
jmp 0x8093
movb $0x1, %bl
cmpq $0x0, 0x10(%rsp)
jne 0x7f89
notb %bl
movzbl %bl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %eax
jmp 0x8093
movl $0x0, 0xc(%rsp)
cmpl $0x27, %r15d
ja 0x8063
movl %ebp, %eax
andl $0x3, %eax
leal (%r15,%rax), %ecx
addl $0x8, %ecx
addl $0x8, %ecx
movl %ecx, %eax
shrl $0x3, %eax
orl $-0x4, %ebp
leal (%rbp,%rax,4), %eax
movl %r14d, %edx
shll $0x7, %edx
shll $0xd, %eax
leal (%rdx,%rbx,2), %esi
addl %esi, %eax
incl %eax
cltq
movl 0x20(%rsp,%rax,4), %esi
movl $0x2, %eax
cmpl $0x3, %esi
leaq 0x111739(%rip), %r13    # 0x119750
je 0x8090
testl %esi, %esi
jne 0x8024
movb $0x1, %sil
movl %esi, 0xc(%rsp)
movl %ecx, %esi
andl $-0x8, %esi
cmpl $0x10, %esi
jne 0x806a
cmpl %r14d, %ecx
je 0x806a
cmpl %ebx, %ecx
movl $0x0, %r14d
je 0x806d
leal (%rdx,%rbx,2), %ecx
incl %ecx
addl $0x18, %r15d
shrl %r15d
addl %ebp, %r15d
shll $0xd, %r15d
orl %ecx, %r15d
movl 0x20(%rsp,%r15,4), %ecx
testl %ecx, %ecx
je 0x8079
cmpl $0x3, %ecx
je 0x8093
jmp 0x806d
leaq 0x1116e6(%rip), %r13    # 0x119750
xorl %r14d, %r14d
movl $0x4, %eax
testb $0x1, 0xc(%rsp)
je 0x8093
xorl %eax, %eax
jmp 0x8093
movl $0x3, %eax
jmp 0x8090
movl $0x2, %eax
leaq 0x1116c0(%rip), %r13    # 0x119750
xorl %r14d, %r14d
movq 0x18(%rsp), %rcx
movl %eax, 0x20(%rsp,%r12,4)
testl %eax, %eax
setne %al
orb %cl, %al
incq %r12
cmpq $0x30000, %r12          # imm = 0x30000
setne %cl
andb %al, %cl
cmpq $0x30000, %r12          # imm = 0x30000
cmoveq %r14, %r12
jne 0x7eab
testb $0x1, %al
jne 0x7eab
leaq 0x20(%rsp), %rax
xorl %edx, %edx
leaq 0x386b6(%rip), %rsi     # 0x40790
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edi
andl $-0x2, %edi
cmpl $0x2, %edi
jne 0x80f1
movl $0x1, %edi
shll %cl, %edi
orl %edi, (%rsi,%rdx,4)
incq %rcx
cmpq $0x20, %rcx
jne 0x80dc
incq %rdx
subq $-0x80, %rax
cmpq $0x1800, %rdx           # imm = 0x1800
jne 0x80da
addq $0xc0028, %rsp          # imm = 0xC0028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq