// File: /workspace/github_cpp_makefile_star2_O3/rachit31[P]CubeSatADCS/threadManager.cpp
// Function: ThreadManager::wait()
const char* ThreadManager::wait()
{
  std::stringstream message;

  // Join with terminated threads
  while (!m_threadMap.empty())
  {
    std::string error;

    // Lock the thread Mutex (TLPI pg 636)
    error = m_mutex.lock();
    if (!error.empty())
    {
      return error.c_str();
    }

    // Set the Conditional Variable to wait for a thread to die
    if (m_numUnjoined == 0)
    {
      error = m_mutex.wait();
      if (!error.empty())
      {
        return error.c_str();
      }
    }

    // Loop through the threads to find the dead ones
    std::vector<unsigned int> eraseVec;
    std::map<unsigned int, Thread*>::iterator it;
    for (it = m_threadMap.begin(); it != m_threadMap.end(); ++it)
    {
      if (!it->second->isAlive())
      {
        void* pthreadRetVal;

        // Join with terminated thread (TLPI pg 625)
        int retVal = pthread_join(it->second->getThread(), &pthreadRetVal);
        if (retVal != 0)
        {
          message << "pthread_join(): " << std::strerror(retVal);
          return message.str().c_str();
        }

        if (pthreadRetVal == PTHREAD_CANCELED)
        {
          message << "thread " << it->first << " was canceled" << std::endl;
        }
        else
        {
          message << "thread " << it->first << " joined successfully" << std::endl;
        }

        // Release the dynamically allocated memory
        delete it->second;

        // Add the map key to the delete vector
        eraseVec.push_back(it->first);

        // Decrement the number of unjoined threads
        m_numUnjoined--;
      }
    }

    std::vector<unsigned int>::iterator iter;
    for (iter = eraseVec.begin(); iter != eraseVec.end(); ++iter)
    {
      if (!m_threadMap.erase(*iter))
      {
        error = "could not remove thread from threadManager threadMap";
        return error.c_str();
      }
    }

    // Lock the thread Mutex (TLPI pg 636)
    error = m_mutex.unlock();
    if (!error.empty())
    {
      return error.c_str();
    }
  }

  return message.str().c_str();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
leaq -0x230(%rbp), %rdi
callq 0x2180
leaq 0x8(%rbx), %r15
leaq 0x38(%rbx), %rax
movq %rax, -0x78(%rbp)
leaq 0x10(%rbx), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, 0x30(%rbx)
je 0x4fb7
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movb $0x0, -0x38(%rbp)
movq -0x78(%rbp), %rdi
callq 0x3712
movq %rax, %r14
movq -0x40(%rbp), %r12
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2310
cmpq $0x0, -0x40(%rbp)
je 0x4c75
movq -0x48(%rbp), %rdi
xorl %r14d, %r14d
movq %rdi, -0x50(%rbp)
jmp 0x4f97
cmpl $0x0, (%rbx)
je 0x4e74
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x20(%rbx), %r14
cmpq -0x80(%rbp), %r14
je 0x4e2c
movq 0x28(%r14), %rdi
callq 0x599a
testb %al, %al
jne 0x4de0
movq 0x28(%r14), %rdi
callq 0x59a4
movq %rax, %rdi
leaq -0x88(%rbp), %rsi
callq 0x2210
movl %eax, %r12d
testl %eax, %eax
jne 0x4eb0
leaq 0x20(%r14), %r12
cmpq $-0x1, -0x88(%rbp)
je 0x4d33
movl $0x7, %edx
leaq -0x220(%rbp), %rdi
leaq 0x35f0(%rip), %rsi      # 0x82e0
callq 0x2280
movl (%r12), %esi
leaq -0x220(%rbp), %rdi
callq 0x2150
movq %rax, %r13
movl $0x14, %edx
movq %rax, %rdi
leaq 0x360b(%rip), %rsi      # 0x8322
callq 0x2280
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2230
jmp 0x4d87
movl $0x7, %edx
leaq -0x220(%rbp), %rdi
leaq 0x359a(%rip), %rsi      # 0x82e0
callq 0x2280
movl (%r12), %esi
leaq -0x220(%rbp), %rdi
callq 0x2150
movq %rax, %r13
movl $0xd, %edx
movq %rax, %rdi
leaq 0x35a7(%rip), %rsi      # 0x8314
callq 0x2280
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2230
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2160
movq 0x28(%r14), %r13
testq %r13, %r13
je 0x4dab
movq %r13, %rdi
callq 0x5994
movl $0x20, %esi
movq %r13, %rdi
callq 0x2260
movq -0x68(%rbp), %rsi
cmpq -0x60(%rbp), %rsi
je 0x4dd2
movl (%r12), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x68(%rbp)
jmp 0x4dde
leaq -0x70(%rbp), %rdi
movq %r12, %rdx
callq 0x5610
decl (%rbx)
movq %r14, %rdi
callq 0x2170
movq %rax, %r14
cmpq -0x80(%rbp), %rax
jne 0x4c9b
movq -0x70(%rbp), %r14
cmpq -0x68(%rbp), %r14
je 0x4e2c
movq %r15, %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x5750
movq 0x30(%rbx), %r12
movq %r13, %rdi
movq %rax, %rsi
callq 0x57d2
cmpq 0x30(%rbx), %r12
je 0x4ef4
addq $0x4, %r14
jmp 0x4df9
movq -0x78(%rbp), %rdi
callq 0x3840
movq %rax, %r14
movq -0x40(%rbp), %r12
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2310
cmpq $0x0, -0x40(%rbp)
sete %r14b
je 0x4f7e
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x4f7e
movq -0x78(%rbp), %rdi
callq 0x396e
movq %rax, %r14
movq -0x40(%rbp), %r12
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2310
cmpq $0x0, -0x40(%rbp)
jne 0x4c65
jmp 0x4c7e
movl $0x10, %edx
leaq -0x220(%rbp), %rdi
leaq 0x3440(%rip), %rsi      # 0x8303
callq 0x2280
movl %r12d, %edi
callq 0x20d0
testq %rax, %rax
je 0x4f1a
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
leaq -0x220(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2280
jmp 0x4f3f
movq -0x40(%rbp), %rdx
movl $0x34, %r8d
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
leaq 0x342c(%rip), %rcx      # 0x8337
callq 0x2310
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x4f7b
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp), %rcx
leaq (%rcx,%rax), %rdi
movl -0x200(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x2360
leaq -0xa8(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x2320
movq -0xa8(%rbp), %r14
leaq -0x98(%rbp), %rax
cmpq %rax, %r14
je 0x4f77
movq -0x98(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2260
movq %r14, -0x50(%rbp)
xorl %r14d, %r14d
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x4f93
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x2260
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x4fac
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2260
testb %r14b, %r14b
jne 0x4c13
jmp 0x4fe7
leaq -0x48(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0x2320
movq -0x48(%rbp), %rbx
leaq -0x38(%rbp), %rax
cmpq %rax, %rbx
je 0x4fe3
movq -0x38(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2260
movq %rbx, -0x50(%rbp)
movq 0x5fca(%rip), %rsi      # 0xafb8
leaq -0x230(%rbp), %rdi
callq 0x21c0
leaq -0x1b0(%rbp), %rdi
callq 0x20c0
movq -0x50(%rbp), %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5061
jmp 0x5030
jmp 0x5030
jmp 0x5030
movq %rax, %rbx
jmp 0x5048
jmp 0x5030
jmp 0x5030
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x5048
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x2260
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5061
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2260
movq 0x5f50(%rip), %rsi      # 0xafb8
leaq -0x230(%rbp), %rdi
callq 0x21c0
leaq -0x1b0(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x23a0