// File: /workspace/github_cpp_makefile_star2_O1/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, %ebp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x8c1f(%rip), %rsi      # 0x32584
leaq 0x8c2e(%rip), %rdx      # 0x3259a
leaq 0x8(%rsp), %rdi
callq 0xa7de
leaq 0x3a36fb(%rip), %rdi    # 0x3cd078
leaq 0x8(%rsp), %rsi
callq 0x15516
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x48(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x299ab
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x8be1(%rip), %rsi      # 0x3259b
leaq 0x8bed(%rip), %rdx      # 0x325ae
callq 0xa7de
leaq 0x3a36ab(%rip), %rdi    # 0x3cd078
leaq 0x8(%rsp), %rsi
callq 0x15516
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x44(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x299fb
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x8ba5(%rip), %rsi      # 0x325af
leaq 0x8bb1(%rip), %rdx      # 0x325c2
callq 0xa7de
leaq 0x3a365b(%rip), %rdi    # 0x3cd078
leaq 0x8(%rsp), %rsi
callq 0x15516
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x40(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x29a4b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x8b69(%rip), %rsi      # 0x325c3
leaq 0x8b77(%rip), %rdx      # 0x325d8
callq 0xa7de
leaq 0x3a360b(%rip), %rdi    # 0x3cd078
leaq 0x8(%rsp), %rsi
callq 0x15516
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x3c(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x29a9b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movl $0x0, 0x8(%rbx)
movl (%r15), %eax
movl %eax, 0x4(%rbx)
movl %eax, 0x38(%rsp)
movl %eax, (%rbx)
movl 0x8(%r15), %eax
movl $0x32, %ecx
cmpl %ecx, %eax
movl $0x32, %edx
cmovll %eax, %edx
testl %eax, %eax
cmovel %ecx, %edx
movl %edx, 0x34(%rsp)
testl %edx, %edx
jle 0x29ca4
movl (%rbx), %ecx
movq %rbx, 0x58(%rsp)
movl 0x4(%rbx), %edx
movl 0x4(%r15), %eax
movl %eax, 0x30(%rsp)
movl $0x1ff, %ebx            # imm = 0x1FF
cmpl %ebx, %ebp
movl $0x1ff, %eax            # imm = 0x1FF
cmovll %ebp, %eax
cltq
leaq 0x8ae4(%rip), %r14      # 0x325e0
movl (%r14,%rax,4), %eax
movl %eax, 0x2c(%rsp)
cvtsi2ss %eax, %xmm0
movss 0x8a68(%rip), %xmm1     # 0x32578
movaps %xmm0, 0x60(%rsp)
mulss %xmm0, %xmm1
movaps %xmm1, 0x70(%rsp)
addl $0x2, %ebp
movl %ebp, 0x28(%rsp)
movl $0x1, %r13d
xorl %ebp, %ebp
movl 0x3c(%rsp), %r12d
movl %edx, 0x50(%rsp)
movl %ecx, 0x54(%rsp)
leal -0x1(%r13), %ecx
imull 0x30(%rsp), %ecx
movl 0x48(%rsp), %edx
cmpl %edx, %r13d
cmovll %r13d, %edx
imull 0x40(%rsp), %edx
addl 0x44(%rsp), %edx
movl 0x38(%rsp), %eax
subl %edx, %eax
addl %ecx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpl $0x2, %r13d
jb 0x29b8f
xorl %ecx, %ecx
movl %ebp, %edx
movl 0x28(%rsp), %esi
cmpl %ebx, %esi
movl $0x1ff, %edi            # imm = 0x1FF
cmovll %esi, %edi
movslq %edi, %rdi
addl (%r14,%rdi,4), %ecx
addl $0x2, %esi
decl %edx
jne 0x29b77
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movaps 0x60(%rsp), %xmm3
movaps %xmm3, %xmm1
addss %xmm0, %xmm1
mulss 0x89d6(%rip), %xmm0     # 0x32580
addl 0x2c(%rsp), %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
addss %xmm3, %xmm0
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movlhps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
shufps $0xe2, %xmm3, %xmm0     # xmm0 = xmm0[2,0],xmm3[2,3]
divps %xmm1, %xmm0
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
minss %xmm1, %xmm0
movss %xmm2, 0x4c(%rsp)
mulss %xmm2, %xmm0
callq 0x5170
xorl %eax, %eax
cmpl $0x2, %r13d
jb 0x29c0f
xorl %eax, %eax
movl %ebp, %ecx
movl 0x28(%rsp), %edx
cmpl %ebx, %edx
movl $0x1ff, %esi            # imm = 0x1FF
cmovll %edx, %esi
movslq %esi, %rsi
addl (%r14,%rsi,4), %eax
addl $0x2, %edx
decl %ecx
jne 0x29bf7
cvttss2si %xmm0, %r15d
addl %r12d, %r15d
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps 0x70(%rsp), %xmm3
movaps %xmm3, %xmm1
addss %xmm0, %xmm1
mulss 0x894a(%rip), %xmm0     # 0x3257c
addss 0x60(%rsp), %xmm0
addl 0x2c(%rsp), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movlhps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
shufps $0xe2, %xmm3, %xmm0     # xmm0 = xmm0[2,0],xmm3[2,3]
divps %xmm1, %xmm0
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
minss %xmm1, %xmm0
mulss 0x4c(%rsp), %xmm0
callq 0x5170
cvttss2si %xmm0, %eax
addl %r12d, %eax
movl 0x54(%rsp), %ecx
cmpl %r15d, %ecx
cmovgel %r15d, %ecx
movl 0x50(%rsp), %edx
cmpl %eax, %edx
cmovgel %eax, %edx
leal 0x1(%r13), %eax
incl %ebp
cmpl 0x34(%rsp), %r13d
movl %eax, %r13d
jne 0x29b32
movq 0x58(%rsp), %rbx
movl %ecx, (%rbx)
movl %edx, 0x4(%rbx)
leaq 0x18(%rsp), %r14
leaq 0x8(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x8926(%rip), %rsi      # 0x325d9
leaq 0x8925(%rip), %rdx      # 0x325df
callq 0xa7de
leaq 0x3a33b2(%rip), %rdi    # 0x3cd078
leaq 0x8(%rsp), %rsi
callq 0x15516
addq $0x20, %rax
leaq 0x74bc(%rip), %rsi      # 0x31197
movq %rax, %rdi
callq 0x5120
movl %eax, %ebp
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x29cfc
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
testl %ebp, %ebp
jne 0x29d02
shll (%rbx)
movl (%rbx), %eax
movl 0x4(%rbx), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, (%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29d30
jmp 0x29d4c
jmp 0x29d30
jmp 0x29d4c
jmp 0x29d30
jmp 0x29d4c
jmp 0x29d30
jmp 0x29d4c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x29d4f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x29d4f
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5790
nop