// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/timeman.cpp
// Function: TimeManager::init(SearchLimits const&, int)
void TimeManager::init(const SearchLimits& limits, int currentPly)
{
  /* We support four different kind of time controls:

      increment == 0 && movesToGo == 0 means: x basetime  [sudden death!]
      increment == 0 && movesToGo != 0 means: x moves in y minutes
      increment >  0 && movesToGo == 0 means: x basetime + z increment
      increment >  0 && movesToGo != 0 means: x moves in y minutes + z increment

    Time management is adjusted by following UCI parameters:

      emergencyMoveHorizon: Be prepared to always play at least this many moves
      emergencyBaseTime   : Always attempt to keep at least this much time (in ms) at clock
      emergencyMoveTime   : Plus attempt to keep at least this much time for each remaining emergency move
      minThinkingTime     : No matter what, use at least this much thinking before doing the move
  */

  int hypMTG, hypMyTime, t1, t2;

  // Read uci parameters
  int emergencyMoveHorizon = Options["Emergency Move Horizon"].value<int>();
  int emergencyBaseTime    = Options["Emergency Base Time"].value<int>();
  int emergencyMoveTime    = Options["Emergency Move Time"].value<int>();
  int minThinkingTime      = Options["Minimum Thinking Time"].value<int>();

  // Initialize to maximum values but unstablePVExtraTime that is reset
  unstablePVExtraTime = 0;
  optimumSearchTime = maximumSearchTime = limits.time;

  // We calculate optimum time usage for different hypothetic "moves to go"-values and choose the
  // minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
  for (hypMTG = 1; hypMTG <= (limits.movesToGo ? Min(limits.movesToGo, MoveHorizon) : MoveHorizon); hypMTG++)
  {
      // Calculate thinking time for hypothetic "moves to go"-value
      hypMyTime =  limits.time
                 + limits.increment * (hypMTG - 1)
                 - emergencyBaseTime
                 - emergencyMoveTime * Min(hypMTG, emergencyMoveHorizon);

      hypMyTime = Max(hypMyTime, 0);

      t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly);
      t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly);

      optimumSearchTime = Min(optimumSearchTime, t1);
      maximumSearchTime = Min(maximumSearchTime, t2);
  }

  if (Options["Ponder"].value<bool>()) {
      OLD optimumSearchTime += optimumSearchTime / 4;
      NEW optimumSearchTime += optimumSearchTime;
  }

  // Make sure that maxSearchTime is not over absoluteMaxSearchTime
  optimumSearchTime = Min(optimumSearchTime, maximumSearchTime);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x7e36(%rip), %rsi      # 0x2a404
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9c42
leaq 0x3a3a94(%rip), %rdi    # 0x3c6078
leaq 0x18(%rsp), %rsi
callq 0x11d0c
movq %rax, %rdi
callq 0x11e28
movl %eax, 0x4c(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x5858
leaq 0x7e10(%rip), %rsi      # 0x2a41b
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9c42
leaq 0x3a3a57(%rip), %rdi    # 0x3c6078
leaq 0x18(%rsp), %rsi
callq 0x11d0c
movq %rax, %rdi
callq 0x11e28
movl %eax, 0x48(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x5858
leaq 0x7de7(%rip), %rsi      # 0x2a42f
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9c42
leaq 0x3a3a1a(%rip), %rdi    # 0x3c6078
leaq 0x18(%rsp), %rsi
callq 0x11d0c
movq %rax, %rdi
callq 0x11e28
movl %eax, 0x44(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x5858
leaq 0x7dbe(%rip), %rsi      # 0x2a443
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9c42
leaq 0x3a39dd(%rip), %rdi    # 0x3c6078
leaq 0x18(%rsp), %rsi
callq 0x11d0c
movq %rax, %rdi
callq 0x11e28
movl %eax, 0x40(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x5858
andl $0x0, 0x8(%r14)
movl (%r15), %esi
movl %esi, 0x4(%r14)
movl %esi, (%r14)
movl 0x8(%r15), %eax
cmpl $0x32, %eax
pushq $0x32
popq %rcx
cmovll %eax, %ecx
xorl %edi, %edi
testl %ecx, %ecx
cmovlel %edi, %ecx
movl 0x4(%r15), %edx
movl %edx, 0x38(%rsp)
movl $0x1ff, %r15d           # imm = 0x1FF
cmpl %r15d, %ebx
movl $0x1ff, %edx            # imm = 0x1FF
cmovll %ebx, %edx
incl %ecx
testl %eax, %eax
movslq %edx, %rax
movq %rax, 0x60(%rsp)
pushq $0x33
popq %rax
cmovnel %ecx, %eax
movl %eax, 0x50(%rsp)
addl $0x2, %ebx
movl %ebx, 0x10(%rsp)
pushq $0x1
popq %rbx
leaq 0x7d42(%rip), %rbp      # 0x2a460
movl %esi, %ecx
movl %esi, 0x3c(%rsp)
movq %r14, 0x68(%rsp)
cmpl 0x50(%rsp), %ebx
je 0x2289f
movl %esi, 0x58(%rsp)
movl %ecx, 0x5c(%rsp)
leal -0x1(%rbx), %ecx
imull 0x38(%rsp), %ecx
movl 0x4c(%rsp), %edx
cmpl %edx, %ebx
cmovll %ebx, %edx
imull 0x44(%rsp), %edx
addl 0x48(%rsp), %edx
movl 0x3c(%rsp), %eax
subl %edx, %eax
addl %ecx, %eax
movq 0x60(%rsp), %rcx
movl (%rbp,%rcx,4), %r12d
xorl %ecx, %ecx
movl 0x10(%rsp), %edx
movl %edi, 0x14(%rsp)
movl %edi, %esi
subl $0x1, %esi
jb 0x2278f
cmpl %r15d, %edx
movl $0x1ff, %edi            # imm = 0x1FF
cmovll %edx, %edi
movslq %edi, %rdi
addl (%rbp,%rdi,4), %ecx
addl $0x2, %edx
jmp 0x22773
xorl %r14d, %r14d
testl %eax, %eax
cmovlel %r14d, %eax
cvtsi2ss %r12d, %xmm3
cvtsi2ss %ecx, %xmm1
movaps %xmm3, %xmm2
addss %xmm1, %xmm2
mulss 0x7c50(%rip), %xmm1     # 0x2a400
addl %r12d, %ecx
cvtsi2ss %ecx, %xmm0
addss %xmm3, %xmm1
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movaps %xmm3, 0x70(%rsp)
movaps %xmm3, %xmm0
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
divps %xmm2, %xmm0
cvtsi2ss %eax, %xmm2
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
minss %xmm1, %xmm0
movss %xmm2, 0x54(%rsp)
mulss %xmm2, %xmm0
callq 0x5160
cvttss2si %xmm0, %r13d
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
jb 0x22814
cmpl %r15d, %eax
movl $0x1ff, %edx            # imm = 0x1FF
cmovll %eax, %edx
movslq %edx, %rdx
addl (%rbp,%rdx,4), %r14d
addl $0x2, %eax
jmp 0x227f7
movaps 0x70(%rsp), %xmm3
movaps %xmm3, %xmm0
mulss 0x7bd4(%rip), %xmm0     # 0x2a3f8
cvtsi2ss %r14d, %xmm1
movaps %xmm0, %xmm2
addss %xmm1, %xmm2
mulss 0x7bc4(%rip), %xmm1     # 0x2a3fc
addss %xmm3, %xmm1
addl %r14d, %r12d
cvtsi2ss %r12d, %xmm3
movl 0x40(%rsp), %r14d
addl %r14d, %r13d
unpcklps %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
divps %xmm2, %xmm0
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
minss %xmm1, %xmm0
mulss 0x54(%rsp), %xmm0
callq 0x5160
movl 0x14(%rsp), %edi
cvttss2si %xmm0, %eax
addl %r14d, %eax
movl 0x58(%rsp), %esi
cmpl %r13d, %esi
cmovgel %r13d, %esi
movl 0x5c(%rsp), %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
movq 0x68(%rsp), %r14
movl %esi, (%r14)
movl %ecx, 0x4(%r14)
incl %ebx
incl %edi
jmp 0x22729
leaq 0x7bb3(%rip), %rsi      # 0x2a459
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x9c42
leaq 0x3a37bc(%rip), %rdi    # 0x3c6078
leaq 0x18(%rsp), %rsi
callq 0x11d0c
movq %rax, %rdi
callq 0x11d78
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
callq 0x5858
movl (%r14), %eax
testb %bpl, %bpl
je 0x228e7
addl %eax, %eax
movl %eax, (%r14)
movl 0x4(%r14), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, (%r14)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x22915
jmp 0x2291a
jmp 0x22915
jmp 0x2291a
jmp 0x22915
jmp 0x2291a
jmp 0x22915
jmp 0x2291a
movq %rax, %rbx
jmp 0x22927
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x5858
movq %rbx, %rdi
callq 0x5780
nop