// File: ATOMIC-LICHESS[P]ATOMKRAFT/search.cpp
// Function: ThreadsManager::idle_loop(int, SplitPoint*)
void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {

  assert(threadID >= 0 && threadID < MAX_THREADS);

  int i;
  bool allFinished;

  while (true)
  {
      // Slave threads can exit as soon as AllThreadsShouldExit raises,
      // master should exit as last one.
      if (allThreadsShouldExit)
      {
          assert(!sp);
          threads[threadID].state = Thread::TERMINATED;
          return;
      }

      // If we are not thinking, wait for a condition to be signaled
      // instead of wasting CPU time polling for work.
      while (   threadID >= activeThreads
             || threads[threadID].state == Thread::INITIALIZING
             || (useSleepingThreads && threads[threadID].state == Thread::AVAILABLE))
      {
          assert(!sp || useSleepingThreads);
          assert(threadID != 0 || useSleepingThreads);

          if (threads[threadID].state == Thread::INITIALIZING)
              threads[threadID].state = Thread::AVAILABLE;

          // Grab the lock to avoid races with Thread::wake_up()
          lock_grab(&threads[threadID].sleepLock);

          // If we are master and all slaves have finished do not go to sleep
          for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
          allFinished = (i == activeThreads);

          if (allFinished || allThreadsShouldExit)
          {
              lock_release(&threads[threadID].sleepLock);
              break;
          }

          // Do sleep here after retesting sleep conditions
          if (threadID >= activeThreads || threads[threadID].state == Thread::AVAILABLE)
              cond_wait(&threads[threadID].sleepCond, &threads[threadID].sleepLock);

          lock_release(&threads[threadID].sleepLock);
      }

      // If this thread has been assigned work, launch a search
      if (threads[threadID].state == Thread::WORKISWAITING)
      {
          assert(!allThreadsShouldExit);

          threads[threadID].state = Thread::SEARCHING;

          // Copy split point position and search stack and call search()
          // with SplitPoint template parameter set to true.
          SearchStack ss[PLY_MAX_PLUS_2];
          SplitPoint* tsp = threads[threadID].splitPoint;
          Position pos(*tsp->pos, threadID);

          memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
          (ss+1)->sp = tsp;

          if (tsp->pvNode)
              search<PV, true, false>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth);
          else
              search<NonPV, true, false>(pos, ss+1, tsp->alpha, tsp->beta, tsp->depth);

          assert(threads[threadID].state == Thread::SEARCHING);

          threads[threadID].state = Thread::AVAILABLE;

          // Wake up master thread so to allow it to return from the idle loop in
          // case we are the last slave of the split point.
          if (   useSleepingThreads
              && threadID != tsp->master
              && threads[tsp->master].state == Thread::AVAILABLE)
              threads[tsp->master].wake_up();
      }

      // If this thread is the master of a split point and all slaves have
      // finished their work at this split point, return from the idle loop.
      for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
      allFinished = (i == activeThreads);

      if (allFinished)
      {
          // Because sp->slaves[] is reset under lock protection,
          // be sure sp->lock has been released before to return.
          lock_grab(&(sp->lock));
          lock_release(&(sp->lock));

          // In helpful master concept a master can help only a sub-tree, and
          // because here is all finished is not possible master is booked.
          assert(threads[threadID].state == Thread::AVAILABLE);

          threads[threadID].state = Thread::SEARCHING;
          return;
      }
  }
}

// Assembly:
subq $0x2fd8, %rsp           # imm = 0x2FD8
movq %rdi, 0x2fd0(%rsp)
movl %esi, 0x2fcc(%rsp)
movq %rdx, 0x2fc0(%rsp)
movq 0x2fd0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movb 0x38(%rax), %al
testb $0x1, %al
je 0x433d1
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl $0x5, 0x88(%rax)
jmp 0x438f4
jmp 0x433d3
movq 0x10(%rsp), %rdx
movl 0x2fcc(%rsp), %ecx
movb $0x1, %al
cmpl 0x34(%rdx), %ecx
movb %al, 0xf(%rsp)
jge 0x4345a
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, 0xf(%rsp)
je 0x4345a
movq 0x10(%rsp), %rcx
xorl %eax, %eax
testb $0x1, 0x30(%rcx)
movb %al, 0xe(%rsp)
je 0x43452
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %eax
cmpl $0x2, %eax
sete %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x43467
jmp 0x4363d
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %eax
cmpl $0x0, %eax
jne 0x434b2
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl $0x2, 0x88(%rax)
movq 0x10(%rsp), %rdi
addq $0x40, %rdi
movslq 0x2fcc(%rsp), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rdi
addq $0x30, %rdi
callq 0x6790
movl $0x0, 0x2fbc(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x2fc0(%rsp)
movb %al, 0xd(%rsp)
je 0x43523
movq 0x10(%rsp), %rdx
movl 0x2fbc(%rsp), %ecx
xorl %eax, %eax
cmpl 0x34(%rdx), %ecx
movb %al, 0xd(%rsp)
jge 0x43523
movq 0x2fc0(%rsp), %rax
movslq 0x2fbc(%rsp), %rcx
movb 0x75(%rax,%rcx), %al
xorb $-0x1, %al
movb %al, 0xd(%rsp)
movb 0xd(%rsp), %al
testb $0x1, %al
jne 0x4352d
jmp 0x43542
jmp 0x4352f
movl 0x2fbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2fbc(%rsp)
jmp 0x434e1
movq 0x10(%rsp), %rcx
movl 0x2fbc(%rsp), %eax
cmpl 0x34(%rcx), %eax
sete %al
andb $0x1, %al
movb %al, 0x2fbb(%rsp)
testb $0x1, 0x2fbb(%rsp)
jne 0x43573
movq 0x10(%rsp), %rax
movb 0x38(%rax), %al
testb $0x1, %al
je 0x4359c
movq 0x10(%rsp), %rdi
addq $0x40, %rdi
movslq 0x2fcc(%rsp), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rdi
addq $0x30, %rdi
callq 0x6390
jmp 0x4363d
movq 0x10(%rsp), %rcx
movl 0x2fcc(%rsp), %eax
cmpl 0x34(%rcx), %eax
jge 0x435d3
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %eax
cmpl $0x2, %eax
jne 0x43614
movq 0x10(%rsp), %rsi
movq %rsi, %rdi
addq $0x40, %rdi
movslq 0x2fcc(%rsp), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rdi
addq $0x58, %rdi
addq $0x40, %rsi
movslq 0x2fcc(%rsp), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rsi
addq $0x30, %rsi
callq 0x67c0
movq 0x10(%rsp), %rdi
addq $0x40, %rdi
movslq 0x2fcc(%rsp), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rdi
addq $0x30, %rdi
callq 0x6390
jmp 0x433d3
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %eax
cmpl $0x4, %eax
jne 0x43815
movq 0x10(%rsp), %rax
movq %rax, %rcx
addq $0x40, %rcx
movslq 0x2fcc(%rsp), %rdx
imulq $0x560, %rdx, %rdx      # imm = 0x560
addq %rdx, %rcx
movl $0x1, 0x88(%rcx)
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movq 0x90(%rax), %rax
movq %rax, 0x1958(%rsp)
movq 0x1958(%rsp), %rax
movq 0x8(%rax), %rsi
movl 0x2fcc(%rsp), %edx
leaq 0x18(%rsp), %rdi
callq 0x36460
leaq 0x1960(%rsp), %rdi
movq 0x1958(%rsp), %rax
movq 0x30(%rax), %rsi
addq $-0x38, %rsi
movl $0xe0, %edx
callq 0x63f0
movq 0x1958(%rsp), %rax
movq %rax, 0x19c8(%rsp)
movq 0x1958(%rsp), %rax
testb $0x1, 0x14(%rax)
je 0x4374b
leaq 0x1960(%rsp), %rsi
addq $0x38, %rsi
movq 0x1958(%rsp), %rax
movl 0x68(%rax), %edx
movq 0x1958(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x1958(%rsp), %rax
movl 0x10(%rax), %r8d
leaq 0x18(%rsp), %rdi
callq 0x43900
jmp 0x43783
leaq 0x1960(%rsp), %rsi
addq $0x38, %rsi
movq 0x1958(%rsp), %rax
movl 0x68(%rax), %edx
movq 0x1958(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x1958(%rsp), %rax
movl 0x10(%rax), %r8d
leaq 0x18(%rsp), %rdi
callq 0x44630
movq 0x10(%rsp), %rax
movq %rax, %rcx
addq $0x40, %rcx
movslq 0x2fcc(%rsp), %rdx
imulq $0x560, %rdx, %rdx      # imm = 0x560
addq %rdx, %rcx
movl $0x2, 0x88(%rcx)
testb $0x1, 0x30(%rax)
je 0x43813
movl 0x2fcc(%rsp), %eax
movq 0x1958(%rsp), %rcx
cmpl 0x20(%rcx), %eax
je 0x43813
movq 0x10(%rsp), %rax
addq $0x40, %rax
movq 0x1958(%rsp), %rcx
movslq 0x20(%rcx), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl 0x88(%rax), %eax
cmpl $0x2, %eax
jne 0x43813
movq 0x10(%rsp), %rdi
addq $0x40, %rdi
movq 0x1958(%rsp), %rax
movslq 0x20(%rax), %rax
imulq $0x560, %rax, %rax      # imm = 0x560
addq %rax, %rdi
callq 0x4f4c0
jmp 0x43815
movl $0x0, 0x2fbc(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x2fc0(%rsp)
movb %al, 0xc(%rsp)
je 0x43862
movq 0x10(%rsp), %rdx
movl 0x2fbc(%rsp), %ecx
xorl %eax, %eax
cmpl 0x34(%rdx), %ecx
movb %al, 0xc(%rsp)
jge 0x43862
movq 0x2fc0(%rsp), %rax
movslq 0x2fbc(%rsp), %rcx
movb 0x75(%rax,%rcx), %al
xorb $-0x1, %al
movb %al, 0xc(%rsp)
movb 0xc(%rsp), %al
testb $0x1, %al
jne 0x4386c
jmp 0x43881
jmp 0x4386e
movl 0x2fbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2fbc(%rsp)
jmp 0x43820
movq 0x10(%rsp), %rcx
movl 0x2fbc(%rsp), %eax
cmpl 0x34(%rcx), %eax
sete %al
andb $0x1, %al
movb %al, 0x2fbb(%rsp)
testb $0x1, 0x2fbb(%rsp)
je 0x438ef
movq 0x2fc0(%rsp), %rdi
addq $0x38, %rdi
callq 0x6790
movq 0x2fc0(%rsp), %rdi
addq $0x38, %rdi
callq 0x6390
movq 0x10(%rsp), %rax
addq $0x40, %rax
movslq 0x2fcc(%rsp), %rcx
imulq $0x560, %rcx, %rcx      # imm = 0x560
addq %rcx, %rax
movl $0x1, 0x88(%rax)
jmp 0x438f4
jmp 0x4339b
addq $0x2fd8, %rsp           # imm = 0x2FD8
retq
nopl (%rax)