// File: 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:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
movl %edx, 0x194(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x15f(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x6980
movq 0xa0(%rsp), %rdx
leaq 0xab82(%rip), %rsi      # 0x5b524
leaq 0x160(%rsp), %rdi
callq 0x12070
jmp 0x509b1
leaq 0x3ac870(%rip), %rdi    # 0x3fd228
leaq 0x160(%rsp), %rsi
callq 0x27f60
movq %rax, 0x90(%rsp)
jmp 0x509cf
movq 0x90(%rsp), %rdi
callq 0x28300
movl %eax, 0x8c(%rsp)
jmp 0x509e5
leaq 0x160(%rsp), %rdi
callq 0x6a78
leaq 0x15f(%rsp), %rdi
callq 0x6550
movl 0x8c(%rsp), %eax
movl %eax, 0x180(%rsp)
leaq 0x127(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x6980
movq 0x80(%rsp), %rdx
leaq 0xab0a(%rip), %rsi      # 0x5b53b
leaq 0x128(%rsp), %rdi
callq 0x12070
jmp 0x50a40
leaq 0x3ac7e1(%rip), %rdi    # 0x3fd228
leaq 0x128(%rsp), %rsi
callq 0x27f60
movq %rax, 0x78(%rsp)
jmp 0x50a5b
movq 0x78(%rsp), %rdi
callq 0x28300
movl %eax, 0x74(%rsp)
jmp 0x50a6b
leaq 0x128(%rsp), %rdi
callq 0x6a78
leaq 0x127(%rsp), %rdi
callq 0x6550
movl 0x74(%rsp), %eax
movl %eax, 0x148(%rsp)
leaq 0xff(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x6980
movq 0x68(%rsp), %rdx
leaq 0xaaa1(%rip), %rsi      # 0x5b54f
leaq 0x100(%rsp), %rdi
callq 0x12070
jmp 0x50abd
leaq 0x3ac764(%rip), %rdi    # 0x3fd228
leaq 0x100(%rsp), %rsi
callq 0x27f60
movq %rax, 0x60(%rsp)
jmp 0x50ad8
movq 0x60(%rsp), %rdi
callq 0x28300
movl %eax, 0x5c(%rsp)
jmp 0x50ae8
leaq 0x100(%rsp), %rdi
callq 0x6a78
leaq 0xff(%rsp), %rdi
callq 0x6550
movl 0x5c(%rsp), %eax
movl %eax, 0x120(%rsp)
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x6980
movq 0x50(%rsp), %rdx
leaq 0xaa38(%rip), %rsi      # 0x5b563
leaq 0xd8(%rsp), %rdi
callq 0x12070
jmp 0x50b3a
leaq 0x3ac6e7(%rip), %rdi    # 0x3fd228
leaq 0xd8(%rsp), %rsi
callq 0x27f60
movq %rax, 0x48(%rsp)
jmp 0x50b55
movq 0x48(%rsp), %rdi
callq 0x28300
movl %eax, 0x44(%rsp)
jmp 0x50b65
leaq 0xd8(%rsp), %rdi
callq 0x6a78
leaq 0xd7(%rsp), %rdi
callq 0x6550
movl 0x44(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0xf8(%rsp)
movl $0x0, 0x8(%rax)
movq 0x198(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x4(%rax)
movl %ecx, (%rax)
movl $0x1, 0x190(%rsp)
movl 0x190(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x198(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0x50c00
movq 0x198(%rsp), %rax
cmpl $0x32, 0x8(%rax)
jge 0x50beb
movq 0x198(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x3c(%rsp)
jmp 0x50bf6
movl $0x32, %eax
movl %eax, 0x3c(%rsp)
jmp 0x50bf6
movl 0x3c(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x50c0b
movl $0x32, %eax
movl %eax, 0x38(%rsp)
jmp 0x50c0b
movl 0x40(%rsp), %eax
movl 0x38(%rsp), %ecx
cmpl %ecx, %eax
jg 0x50ed2
movq 0x198(%rsp), %rax
movl (%rax), %eax
movq 0x198(%rsp), %rcx
movl 0x4(%rcx), %ecx
movl 0x190(%rsp), %edx
subl $0x1, %edx
imull %edx, %ecx
addl %ecx, %eax
subl 0x148(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x190(%rsp), %eax
cmpl 0x180(%rsp), %eax
jge 0x50c72
movl 0x190(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x50c7d
movl 0x180(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
movl 0x2c(%rsp), %edx
imull %edx, %ecx
subl %ecx, %eax
movl %eax, 0x18c(%rsp)
cmpl $0x0, 0x18c(%rsp)
jge 0x50ca7
xorl %eax, %eax
movl %eax, 0x28(%rsp)
jmp 0x50cb2
movl 0x18c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x18c(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x18c(%rsp), %edi
movl 0x190(%rsp), %esi
movl 0x194(%rsp), %edx
callq 0x51000
movl %eax, %ecx
movl 0x20(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x188(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x18c(%rsp), %edi
movl 0x190(%rsp), %esi
movl 0x194(%rsp), %edx
callq 0x51130
movl 0x24(%rsp), %ecx
movl %eax, %edx
movq 0x98(%rsp), %rax
addl %edx, %ecx
movl %ecx, 0x184(%rsp)
movl (%rax), %eax
cmpl 0x188(%rsp), %eax
jge 0x50d48
movq 0x98(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
jmp 0x50d53
movl 0x188(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x98(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x4(%rax), %eax
cmpl 0x184(%rsp), %eax
jge 0x50d7e
movq 0x98(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x50d89
movl 0x184(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x98(%rsp), %rax
movl 0x18(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x50bb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x50de5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x6a78
leaq 0x15f(%rsp), %rdi
callq 0x6550
jmp 0x50fe5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x50e2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x6a78
leaq 0x127(%rsp), %rdi
callq 0x6550
jmp 0x50fe5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x50e77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x6a78
leaq 0xff(%rsp), %rdi
callq 0x6550
jmp 0x50fe5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x50ec0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x6a78
leaq 0xd7(%rsp), %rdi
callq 0x6550
jmp 0x50fe5
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x6980
movq 0x10(%rsp), %rdx
leaq 0xa689(%rip), %rsi      # 0x5b579
leaq 0xb0(%rsp), %rdi
callq 0x12070
jmp 0x50eff
leaq 0x3ac322(%rip), %rdi    # 0x3fd228
leaq 0xb0(%rsp), %rsi
callq 0x27f60
movq %rax, 0x8(%rsp)
jmp 0x50f1a
movq 0x8(%rsp), %rdi
callq 0x28050
movb %al, 0x7(%rsp)
jmp 0x50f2a
leaq 0xb0(%rsp), %rdi
callq 0x6a78
leaq 0xaf(%rsp), %rdi
callq 0x6550
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x50f4e
jmp 0x50fa4
movq 0x98(%rsp), %rax
movl (%rax), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x50fa4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x50f95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x6a78
leaq 0xaf(%rsp), %rdi
callq 0x6550
jmp 0x50fe5
movq 0x98(%rsp), %rcx
movl (%rcx), %eax
cmpl 0x4(%rcx), %eax
jge 0x50fc2
movq 0x98(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rsp)
jmp 0x50fd0
movq 0x98(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, (%rsp)
movq 0x98(%rsp), %rax
movl (%rsp), %ecx
movl %ecx, (%rax)
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0x150(%rsp), %rdi
callq 0x6970
nopw %cs:(%rax,%rax)