// File: /workspace/github_cpp_makefile_star2_O1/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 0x624341(%rip), %r15d   # 0x62a380
shll $0x2, %r15d
leaq (,%r15,4), %rdi
callq 0x3030
movq %rax, %r14
movq %r15, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x623ab3(%rip), %r12    # 0x629b18
leaq 0x9294(%rip), %rbp      # 0xf300
leaq 0x623acd(%rip), %r13    # 0x629b40
movl 0x623ae7(%rip), %eax    # 0x629b60
cmpl 0x9269(%rip), %eax      # 0xf2e8
jl 0x6073
lock
incl 0x623ae0(%rip)          # 0x629b68
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4927
movb 0x925d(%rip), %al       # 0xf2f8
testb $0x1, %al
jne 0x6179
movl 0x623abf(%rip), %eax    # 0x629b68
cmpl 0x9239(%rip), %eax      # 0xf2e8
jl 0x60a3
movq %r12, %rdi
callq 0x33a0
lock
decl 0x623aa0(%rip)          # 0x629b60
movl 0x623a9a(%rip), %eax    # 0x629b60
testl %eax, %eax
jne 0x616c
movb 0x623a90(%rip), %al     # 0x629b64
testb $0x1, %al
je 0x60ce
xorl %eax, %eax
xchgl %eax, 0x623a88(%rip)    # 0x629b68
cmpq %rbp, 0x623a19(%rip)    # 0x629b00
movq %rbp, %rax
leaq 0x18fc0f(%rip), %rcx    # 0x195d00
cmoveq %rcx, %rax
leaq 0x316604(%rip), %rcx    # 0x31c700
leaq 0x49cffd(%rip), %rdx    # 0x4a3100
cmoveq %rdx, %rcx
movq %rax, 0x6239f2(%rip)    # 0x629b00
movq %rcx, 0x6239f3(%rip)    # 0x629b08
xorl %eax, %eax
xchgb %al, 0x623a47(%rip)     # 0x629b64
movb 0x91d6(%rip), %al       # 0xf2f9
testb $0x1, %al
je 0x612f
movb $0x1, %al
xchgb %al, 0x91c9(%rip)       # 0xf2f8
movq %r13, %rdi
callq 0x3440
testl %eax, %eax
je 0x6153
movl $0x17, %edx
movq 0x8ea9(%rip), %rdi      # 0xeff0
leaq 0x5b25(%rip), %rsi      # 0xbc73
callq 0x3310
movl 0x918f(%rip), %eax      # 0xf2e8
xchgl %eax, 0x623a01(%rip)    # 0x629b60
movq %r12, %rdi
callq 0x3210
jmp 0x6081
movq %r12, %rdi
callq 0x3210
jmp 0x6073
cmpb $0x1, 0x916f(%rip)      # 0xf2ef
jne 0x6192
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