// File: /workspace/github_cpp_makefile_star2_O3/Ahhgust[P]STRaitRazor/str8.cpp
// Function: workerThread(void*)
void *
workerThread(void *arg) {
  
  int id =  *((int*)arg ); // thread id

  unsigned *matchIds = NULL;
  unsigned char *matchTypes = NULL;
  matchIds = new unsigned[ numStrs * 4];
  matchTypes = new unsigned char[ numStrs * 4];


 top:
  // spin loop... wait for the producer to generate some data
  while (workersWorking < opt.numThreads) 
    ;

 middle:

  ++startedWorking;
  processDNA_Trie(id, matchIds, matchTypes);

  if (done) {

    if (opt.useTrie) {
      delete [] matchIds;
      delete [] matchTypes;
    }

    return NULL;
  }

  while (startedWorking < opt.numThreads)
    ;

  pthread_mutex_lock(&ioLock);

  --workersWorking;

  // last worker to check in
  if (workersWorking == 0) {    

    while (! buffered) //ensure that the other buffer is filled... 
      ; // ie, wait for buffered=1 assignment

    startedWorking=0;
    // swap the pointers...
    if (records==MEM) {
      records = OTHERMEM;
      qrecords = OTHERQMEM;
    } else {
      records = MEM;
      qrecords = QMEM;
    }
    
    buffered=0; // we set buffered=0; ie, the other buffer needs to get filled

    if (nextdone)
      done = true; // update done with the status of the current buffer
#ifdef OSX
    if (sem_post(writersLock)) { // wake up the writer thread which fills the buffer (Named semaphore)
      cerr << "Error waking up writer\n";
    }
#else
    if (sem_post(&writersLock)) { // wake up the writer thread which fills the buffer (unnamed semaphore)
      cerr << "Error waking up writer\n";
    }
#endif
    workersWorking= opt.numThreads; // wake up the othe readers
    pthread_mutex_unlock(&ioLock);  // release mutex
    goto middle;
  } else {
    pthread_mutex_unlock(&ioLock);
    goto top;
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl (%rdi), %ebx
movl 0x6242ee(%rip), %r15d   # 0x62a380
shll $0x2, %r15d
leaq (,%r15,4), %rdi
callq 0x3030
movq %rax, %r14
movq %r15, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x623a60(%rip), %r12    # 0x629b18
leaq 0x9241(%rip), %rbp      # 0xf300
leaq 0x623a7a(%rip), %r13    # 0x629b40
movl 0x623a94(%rip), %eax    # 0x629b60
cmpl 0x9216(%rip), %eax      # 0xf2e8
jl 0x60c6
lock
incl 0x623a8d(%rip)          # 0x629b68
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x490d
movb 0x920a(%rip), %al       # 0xf2f8
testb $0x1, %al
jne 0x61cc
movl 0x623a6c(%rip), %eax    # 0x629b68
cmpl 0x91e6(%rip), %eax      # 0xf2e8
jl 0x60f6
movq %r12, %rdi
callq 0x33a0
lock
decl 0x623a4d(%rip)          # 0x629b60
movl 0x623a47(%rip), %eax    # 0x629b60
testl %eax, %eax
jne 0x61bf
movb 0x623a3d(%rip), %al     # 0x629b64
testb $0x1, %al
je 0x6121
xorl %eax, %eax
xchgl %eax, 0x623a35(%rip)    # 0x629b68
cmpq %rbp, 0x6239c6(%rip)    # 0x629b00
movq %rbp, %rax
leaq 0x18fbbc(%rip), %rcx    # 0x195d00
cmoveq %rcx, %rax
leaq 0x3165b1(%rip), %rcx    # 0x31c700
leaq 0x49cfaa(%rip), %rdx    # 0x4a3100
cmoveq %rdx, %rcx
movq %rax, 0x62399f(%rip)    # 0x629b00
movq %rcx, 0x6239a0(%rip)    # 0x629b08
xorl %eax, %eax
xchgb %al, 0x6239f4(%rip)     # 0x629b64
movb 0x9183(%rip), %al       # 0xf2f9
testb $0x1, %al
je 0x6182
movb $0x1, %al
xchgb %al, 0x9176(%rip)       # 0xf2f8
movq %r13, %rdi
callq 0x3440
testl %eax, %eax
je 0x61a6
movl $0x17, %edx
movq 0x8e56(%rip), %rdi      # 0xeff0
leaq 0x5b12(%rip), %rsi      # 0xbcb3
callq 0x3310
movl 0x913c(%rip), %eax      # 0xf2e8
xchgl %eax, 0x6239ae(%rip)    # 0x629b60
movq %r12, %rdi
callq 0x3210
jmp 0x60d4
movq %r12, %rdi
callq 0x3210
jmp 0x60c6
cmpb $0x1, 0x911c(%rip)      # 0xf2ef
jne 0x61e5
movq %r14, %rdi
callq 0x3360
movq %r15, %rdi
callq 0x3360
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq