// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc0038, %rsp          # imm = 0xC0038
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
pushq $0x1
popq %r12
cmpq $0x30000, %rbx          # imm = 0x30000
je 0x77e6
movq %r14, %rdi
movl %ebx, %esi
callq 0x7a1e
movslq 0x8(%rsp), %r13
movl 0x10(%rsp), %r15d
movl %r12d, %ebp
cmpl %r15d, %r13d
je 0x77da
movslq 0xc(%rsp), %rdi
cmpl %edi, %r13d
sete %al
cmpl %r15d, %edi
sete %cl
orb %al, %cl
movl %r12d, %ebp
jne 0x77da
movslq %r15d, %rax
movl 0x14(%rsp), %ecx
leaq 0x109acf(%rip), %rdx    # 0x1111a0
testl %ecx, %ecx
je 0x774f
movq (%rdx,%r13,8), %rdx
movl %r12d, %ebp
leaq 0x10a06d(%rip), %rsi    # 0x111750
testq %rdx, 0x1c00(%rsi,%rdi,8)
jne 0x77da
cmpl $0x1, %ecx
jne 0x777b
leaq 0x10a04f(%rip), %rsi    # 0x111750
movq 0xc00(%rsi,%r13,8), %rcx
movq 0x1c00(%rsi,%rdi,8), %rdx
movq 0x200(%rsi,%rax,8), %rsi
orq %rcx, %rsi
notq %rsi
pushq $0x4
popq %rbp
testq %rsi, %rdx
je 0x77da
leaq 0x109a6e(%rip), %rsi    # 0x1111a0
movq (%rsi,%rax,8), %rax
testq %rdx, %rax
setne %dl
testq %rcx, %rax
sete %al
andb %dl, %al
movzbl %al, %ebp
shll $0x2, %ebp
jmp 0x77da
movq (%rdx,%rdi,8), %rdx
movl %r12d, %ebp
leaq 0x109ff3(%rip), %rsi    # 0x111750
testq %rdx, 0xc00(%rsi,%r13,8)
jne 0x77da
movl %r12d, %ebp
leaq 0x109fdf(%rip), %rsi    # 0x111750
testq %rdx, 0x200(%rsi,%rax,8)
jne 0x77da
movl %r13d, %edx
xorl $0x38, %edx
movl %r15d, %esi
xorl $0x30, %esi
orl %edx, %esi
jne 0x7799
leal -0x32(%rdi), %edx
pushq $0x4
popq %rbp
testl $0xfffffff7, %edx       # imm = 0xFFFFFFF7
je 0x77da
andl $-0x8, %eax
xorl $0x30, %eax
xorl %ebp, %ebp
orl %ecx, %eax
jne 0x77da
orq $0x8, %r15
cmpl %r15d, %r13d
je 0x77da
movl %r15d, %esi
callq 0x75ec
cmpl $0x1, %eax
jg 0x77d7
leaq 0x1099de(%rip), %rax    # 0x1111a0
movq (%rax,%r15,8), %rax
leaq 0x109f83(%rip), %rcx    # 0x111750
testq %rax, 0xc00(%rcx,%r13,8)
je 0x77da
pushq $0x2
popq %rbp
movl %ebp, 0x30(%rsp,%rbx,4)
incq %rbx
jmp 0x767a
xorl %r14d, %r14d
pushq $0x1
popq %rbx
leaq 0x18(%rsp), %r12
xorl %eax, %eax
cmpq $0x30000, %r14          # imm = 0x30000
je 0x789b
cmpl $0x0, 0x30(%rsp,%r14,4)
jne 0x79c4
movq %rax, 0x28(%rsp)
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x7a1e
cmpl $0x0, 0x14(%rsp)
je 0x78c2
movslq 0xc(%rsp), %rax
leaq 0x109f1b(%rip), %rcx    # 0x111750
movq 0x1c00(%rcx,%rax,8), %rax
movq %rax, 0x18(%rsp)
movl 0x8(%rsp), %r15d
movl 0x10(%rsp), %ebp
movl %ebp, %ecx
andl $0x7, %ecx
shrl %ebp
andl $0x7fffc, %ebp          # imm = 0x7FFFC
addl %ecx, %ebp
shll $0xd, %ebp
shll $0x7, %r15d
addl $0xffff8000, %ebp       # imm = 0xFFFF8000
xorl %r13d, %r13d
testq %rax, %rax
je 0x78b0
movq %r12, %rdi
callq 0x7b62
leal (%r15,%rax,2), %eax
addl %ebp, %eax
cltq
movl 0x30(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x7891
cmpl $0x4, %eax
jne 0x7894
jmp 0x79b3
movb $0x1, %r13b
movq 0x18(%rsp), %rax
jmp 0x786a
xorl %r14d, %r14d
testb $0x1, %al
movl $0x0, %eax
jne 0x77f3
jmp 0x79cc
notb %r13b
movzbl %r13b, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %eax
jmp 0x79b3
movslq 0x8(%rsp), %rdi
leaq 0x109e82(%rip), %rax    # 0x111750
movq 0xc00(%rax,%rdi,8), %rax
movq %rax, 0x18(%rsp)
movl 0xc(%rsp), %ebp
movl 0x10(%rsp), %r15d
movl $0x0, 0x4(%rsp)
movq %rdi, %r13
testq %rax, %rax
je 0x7931
movq %r12, %rdi
callq 0x7b62
movl %eax, %edi
movl %ebp, %esi
movl %r15d, %edx
movl %ebx, %ecx
callq 0x763c
cltq
movl 0x30(%rsp,%rax,4), %eax
testl %eax, %eax
je 0x7921
cmpl $0x3, %eax
movq %r13, %rdi
jne 0x792a
jmp 0x79b0
movb $0x1, %al
movl %eax, 0x4(%rsp)
movq %r13, %rdi
movq 0x18(%rsp), %rax
jmp 0x78ef
cmpl $0x2f, %r15d
jg 0x79a2
leal 0x8(%r15), %edx
movl %ebp, %esi
movl %edx, 0x24(%rsp)
movl %ebx, %ecx
callq 0x763c
cltq
movl 0x30(%rsp,%rax,4), %ecx
pushq $0x2
popq %rax
cmpl $0x3, %ecx
je 0x79b3
testl %ecx, %ecx
jne 0x7960
movb $0x1, %al
movl %eax, 0x4(%rsp)
movl 0x24(%rsp), %ecx
movl %ecx, %eax
andl $-0x8, %eax
cmpl $0x10, %eax
jne 0x79a2
cmpl %r13d, %ecx
sete %al
cmpl %ebp, %ecx
sete %cl
orb %al, %cl
jne 0x79a2
addl $0x10, %r15d
movl %r13d, %edi
movl %ebp, %esi
movl %r15d, %edx
movl %ebx, %ecx
callq 0x763c
cltq
movl 0x30(%rsp,%rax,4), %ecx
testl %ecx, %ecx
je 0x79ac
pushq $0x2
popq %rax
cmpl $0x3, %ecx
je 0x79b3
pushq $0x4
popq %rax
testb $0x1, 0x4(%rsp)
je 0x79b3
xorl %eax, %eax
jmp 0x79b3
pushq $0x2
popq %rax
movq 0x28(%rsp), %rcx
movl %eax, 0x30(%rsp,%r14,4)
testl %eax, %eax
setne %al
orb %cl, %al
incq %r14
jmp 0x77f3
leaq 0x30(%rsp), %rax
xorl %edx, %edx
leaq 0x30db6(%rip), %rsi     # 0x38790
cmpq $0x1800, %rdx           # imm = 0x1800
je 0x7a0b
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x7a02
movl (%rax,%rcx,4), %edi
andl $-0x2, %edi
cmpl $0x2, %edi
jne 0x79fd
movl %ebx, %edi
shll %cl, %edi
orl %edi, (%rsi,%rdx,4)
incq %rcx
jmp 0x79e5
incq %rdx
subq $-0x80, %rax
jmp 0x79da
addq $0xc0038, %rsp          # imm = 0xC0038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop