// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rdi, %r14
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x98d3(%rip), %rsi      # 0x335e4
leaq 0x98e2(%rip), %rdx      # 0x335fa
leaq 0x8(%rsp), %rdi
callq 0xad16
leaq 0x3a434f(%rip), %rdi    # 0x3ce078
leaq 0x8(%rsp), %rsi
callq 0x15a9c
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x48(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x29d57
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x9895(%rip), %rsi      # 0x335fb
leaq 0x98a1(%rip), %rdx      # 0x3360e
callq 0xad16
leaq 0x3a42ff(%rip), %rdi    # 0x3ce078
leaq 0x8(%rsp), %rsi
callq 0x15a9c
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x44(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x29da7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x9859(%rip), %rsi      # 0x3360f
leaq 0x9865(%rip), %rdx      # 0x33622
callq 0xad16
leaq 0x3a42af(%rip), %rdi    # 0x3ce078
leaq 0x8(%rsp), %rsi
callq 0x15a9c
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x40(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x29df7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x981d(%rip), %rsi      # 0x33623
leaq 0x982b(%rip), %rdx      # 0x33638
callq 0xad16
leaq 0x3a425f(%rip), %rdi    # 0x3ce078
leaq 0x8(%rsp), %rsi
callq 0x15a9c
movq 0x20(%rax), %rdi
callq 0x5660
movl %eax, 0x3c(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x29e47
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movl $0x0, 0x8(%r14)
movl (%r13), %esi
movl %esi, 0x4(%r14)
movl %esi, (%r14)
movl 0x8(%r13), %eax
movl $0x32, %ecx
cmpl %ecx, %eax
movl $0x32, %edx
cmovll %eax, %edx
testl %eax, %eax
cmovel %ecx, %edx
movl %edx, 0x38(%rsp)
testl %edx, %edx
jle 0x2a058
movq %r14, 0x58(%rsp)
movl $0x1ff, %r15d           # imm = 0x1FF
cmpl %r15d, %ebx
movl $0x1ff, %eax            # imm = 0x1FF
cmovll %ebx, %eax
cltq
leaq 0x97a3(%rip), %r12      # 0x33640
movl (%r12,%rax,4), %eax
movl %eax, 0x2c(%rsp)
cvtsi2ss %eax, %xmm0
movl 0x4(%r13), %eax
movl %eax, 0x30(%rsp)
movss 0x971f(%rip), %xmm1     # 0x335d8
movaps %xmm0, 0x60(%rsp)
mulss %xmm0, %xmm1
movaps %xmm1, 0x70(%rsp)
addl $0x2, %ebx
movl %ebx, 0x28(%rsp)
movl $0x1, %r14d
xorl %ebx, %ebx
movl %esi, %r13d
movl %esi, %ecx
movl %esi, 0x34(%rsp)
movl %ecx, 0x50(%rsp)
leal -0x1(%r14), %ecx
imull 0x30(%rsp), %ecx
movl 0x48(%rsp), %edx
cmpl %edx, %r14d
cmovll %r14d, %edx
imull 0x40(%rsp), %edx
addl 0x44(%rsp), %edx
movl %esi, %eax
subl %edx, %eax
addl %ecx, %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpl $0x2, %r14d
jb 0x29f37
xorl %ecx, %ecx
movl %ebx, %edx
movl 0x28(%rsp), %esi
cmpl %r15d, %esi
movl $0x1ff, %edi            # imm = 0x1FF
cmovll %esi, %edi
movslq %edi, %rdi
addl (%r12,%rdi,4), %ecx
addl $0x2, %esi
decl %edx
jne 0x29f1e
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movaps 0x60(%rsp), %xmm3
movaps %xmm3, %xmm1
addss %xmm0, %xmm1
mulss 0x968e(%rip), %xmm0     # 0x335e0
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, %r14d
jb 0x29fb8
xorl %eax, %eax
movl %ebx, %ecx
movl 0x28(%rsp), %edx
cmpl %r15d, %edx
movl $0x1ff, %esi            # imm = 0x1FF
cmovll %edx, %esi
movslq %esi, %rsi
addl (%r12,%rsi,4), %eax
addl $0x2, %edx
decl %ecx
jne 0x29f9f
cvttss2si %xmm0, %ebp
movl %ebx, 0x54(%rsp)
movl 0x3c(%rsp), %ebx
addl %ebx, %ebp
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps 0x70(%rsp), %xmm3
movaps %xmm3, %xmm1
addss %xmm0, %xmm1
mulss 0x95fb(%rip), %xmm0     # 0x335dc
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 %ebx, %eax
movl 0x54(%rsp), %ebx
cmpl %ebp, %r13d
cmovgel %ebp, %r13d
movl 0x50(%rsp), %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
leal 0x1(%r14), %eax
incl %ebx
cmpl 0x38(%rsp), %r14d
movl %eax, %r14d
movl 0x34(%rsp), %esi
jne 0x29edf
movq 0x58(%rsp), %r14
movl %r13d, (%r14)
movl %ecx, 0x4(%r14)
leaq 0x18(%rsp), %r15
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x95d2(%rip), %rsi      # 0x33639
leaq 0x95d1(%rip), %rdx      # 0x3363f
callq 0xad16
leaq 0x3a3ffe(%rip), %rdi    # 0x3ce078
leaq 0x8(%rsp), %rsi
callq 0x15a9c
addq $0x20, %rax
leaq 0x8188(%rip), %rsi      # 0x32217
movq %rax, %rdi
callq 0x5120
movl %eax, %ebp
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x2a0b0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
movl (%r14), %eax
testl %ebp, %ebp
jne 0x2a0bc
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 0x2a0ea
jmp 0x2a106
jmp 0x2a0ea
jmp 0x2a106
jmp 0x2a0ea
jmp 0x2a106
jmp 0x2a0ea
jmp 0x2a106
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x2a109
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x2a109
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5790
nopl (%rax)