// File: ATOMIC-LICHESS[P]ATOMKRAFT/tt.cpp
// Function: TranspositionTable::store(unsigned long, Value, ValueType, Depth, Move, Value, Value)
void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {

  int c1, c2, c3;
  TTEntry *tte, *replace;
  uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key inside the cluster

  tte = replace = first_entry(posKey);

  for (int i = 0; i < ClusterSize; i++, tte++)
  {
      if (!tte->key() || tte->key() == posKey32) // Empty or overwrite old
      {
          // Preserve any existing ttMove
          if (m == MOVE_NONE)
              m = tte->move();

          tte->save(posKey32, v, t, d, m, generation, statV, kingD);
          return;
      }

      // Implement replace strategy
      c1 = (replace->generation() == generation ?  2 : 0);
      c2 = (tte->generation() == generation || tte->type() == VALUE_TYPE_EXACT ? -2 : 0);
      c3 = (tte->depth() < replace->depth() ?  1 : 0);

      if (c1 + c2 + c3 > 0)
          replace = tte;
  }
  replace->save(posKey32, v, t, d, m, generation, statV, kingD);
}

// Assembly:
subq $0x78, %rsp
movl 0x88(%rsp), %eax
movl 0x80(%rsp), %eax
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movl %edx, 0x64(%rsp)
movl %ecx, 0x60(%rsp)
movl %r8d, 0x5c(%rsp)
movl %r9d, 0x58(%rsp)
movq 0x70(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movq 0x68(%rsp), %rax
shrq $0x20, %rax
movl %eax, 0x34(%rsp)
movq 0x68(%rsp), %rsi
callq 0x3e740
movq %rax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl $0x0, 0x30(%rsp)
cmpl $0x4, 0x30(%rsp)
jge 0x51671
movq 0x40(%rsp), %rdi
callq 0x51830
cmpl $0x0, %eax
je 0x5153b
movq 0x40(%rsp), %rdi
callq 0x51830
cmpl 0x34(%rsp), %eax
jne 0x5159b
cmpl $0x0, 0x58(%rsp)
jne 0x51550
movq 0x40(%rsp), %rdi
callq 0x4ea20
movl %eax, 0x58(%rsp)
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
movl 0x5c(%rsp), %r8d
movl 0x58(%rsp), %r9d
movzbl 0x10(%rax), %r11d
movl 0x80(%rsp), %r10d
movl 0x88(%rsp), %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x51840
jmp 0x516b7
movq 0x38(%rsp), %rdi
callq 0x518b0
movl %eax, %edx
movq 0x28(%rsp), %rax
movzbl 0x10(%rax), %esi
xorl %eax, %eax
movl $0x2, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
movl %eax, 0x54(%rsp)
movq 0x40(%rsp), %rdi
callq 0x518b0
movl %eax, %ecx
movq 0x28(%rsp), %rax
movzbl 0x10(%rax), %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, 0x27(%rsp)
je 0x515f3
movq 0x40(%rsp), %rdi
callq 0x4eac0
cmpl $0x3, %eax
sete %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %dl
xorl %eax, %eax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x50(%rsp)
movq 0x40(%rsp), %rdi
callq 0x4ebb0
movl %eax, 0x20(%rsp)
movq 0x38(%rsp), %rdi
callq 0x4ebb0
movl 0x20(%rsp), %edx
movl %eax, %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpl %esi, %edx
cmovll %ecx, %eax
movl %eax, 0x4c(%rsp)
movl 0x54(%rsp), %eax
addl 0x50(%rsp), %eax
addl 0x4c(%rsp), %eax
cmpl $0x0, %eax
jle 0x51651
movq 0x40(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0x51653
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movq 0x40(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x40(%rsp)
jmp 0x51511
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x60(%rsp), %ecx
movl 0x5c(%rsp), %r8d
movl 0x58(%rsp), %r9d
movzbl 0x10(%rax), %r11d
movl 0x80(%rsp), %r10d
movl 0x88(%rsp), %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x51840
addq $0x78, %rsp
retq
nopl (%rax)