// File: 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:
subq $0xc0038, %rsp          # imm = 0xC0038
movl $0x0, 0x18(%rsp)
cmpl $0x30000, 0x18(%rsp)    # imm = 0x30000
jge 0xcfc1
movl 0x18(%rsp), %esi
leaq 0x20(%rsp), %rdi
callq 0xd100
leaq 0x20(%rsp), %rdi
callq 0xd1b0
testb $0x1, %al
jne 0xcf64
movl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xcfa4
leaq 0x20(%rsp), %rdi
callq 0xd270
testb $0x1, %al
jne 0xcf74
jmp 0xcf7f
movl $0x4, %eax
movl %eax, 0x4(%rsp)
jmp 0xcf9c
leaq 0x20(%rsp), %rdi
callq 0xd370
movb %al, %dl
xorl %eax, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %ecx
movslq 0x18(%rsp), %rax
movl %ecx, 0x30(%rsp,%rax,4)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xcf2f
jmp 0xcfc3
movb $0x0, 0x1f(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x30000, 0x14(%rsp)    # imm = 0x30000
jge 0xd04f
movslq 0x14(%rsp), %rax
cmpl $0x0, 0x30(%rsp,%rax,4)
jne 0xd040
movl 0x14(%rsp), %esi
leaq 0x20(%rsp), %rdi
callq 0xd100
cmpl $0x0, 0x2c(%rsp)
jne 0xd00f
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0xd460
movl %eax, (%rsp)
jmp 0xd021
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0xd630
movl %eax, (%rsp)
movl (%rsp), %ecx
movslq 0x14(%rsp), %rax
movl %ecx, 0x30(%rsp,%rax,4)
movslq 0x14(%rsp), %rax
cmpl $0x0, 0x30(%rsp,%rax,4)
je 0xd03e
movb $0x1, 0x1f(%rsp)
jmp 0xd040
jmp 0xd042
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xcfd0
jmp 0xd051
testb $0x1, 0x1f(%rsp)
jne 0xcfc3
movl $0x0, 0x10(%rsp)
cmpl $0x1800, 0x10(%rsp)     # imm = 0x1800
jge 0xd0ea
movl $0x0, 0xc(%rsp)
cmpl $0x20, 0xc(%rsp)
jge 0xd0d8
movl 0x10(%rsp), %eax
shll $0x5, %eax
addl 0xc(%rsp), %eax
cltq
cmpl $0x2, 0x30(%rsp,%rax,4)
je 0xd0a5
movl 0x10(%rsp), %eax
shll $0x5, %eax
addl 0xc(%rsp), %eax
cltq
cmpl $0x3, 0x30(%rsp,%rax,4)
jne 0xd0c9
movl 0xc(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movslq 0x10(%rsp), %rcx
leaq 0x627e4(%rip), %rax     # 0x6f8a0
orl (%rax,%rcx,4), %edx
leaq 0x627da(%rip), %rax     # 0x6f8a0
movl %edx, (%rax,%rcx,4)
jmp 0xd0cb
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd076
jmp 0xd0da
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0xd064
addq $0xc0038, %rsp          # imm = 0xC0038
retq
nopw %cs:(%rax,%rax)