// File: /workspace/github_cpp_makefile_star2_O2/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 0x624dd5(%rip), %r15d   # 0x62a360
shll $0x2, %r15d
leaq (,%r15,4), %rdi
callq 0x3030
movq %rax, %r14
movq %r15, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x624547(%rip), %r12    # 0x629af8
leaq 0x9d28(%rip), %rbp      # 0xf2e0
leaq 0x624561(%rip), %r13    # 0x629b20
movl 0x62457b(%rip), %eax    # 0x629b40
cmpl 0x9cfd(%rip), %eax      # 0xf2c8
jl 0x55bf
lock
incl 0x624574(%rip)          # 0x629b48
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x450b
movb 0x9cf1(%rip), %al       # 0xf2d8
testb $0x1, %al
jne 0x56c0
movl 0x624553(%rip), %eax    # 0x629b48
cmpl 0x9ccd(%rip), %eax      # 0xf2c8
jl 0x55ef
movq %r12, %rdi
callq 0x3360
lock
decl 0x624534(%rip)          # 0x629b40
movl 0x62452e(%rip), %eax    # 0x629b40
testl %eax, %eax
jne 0x56b3
movb 0x624524(%rip), %al     # 0x629b44
testb $0x1, %al
je 0x561a
xorl %eax, %eax
xchgl %eax, 0x62451c(%rip)    # 0x629b48
cmpq %rbp, 0x6244ad(%rip)    # 0x629ae0
movq %rbp, %rax
leaq 0x1906a3(%rip), %rcx    # 0x195ce0
cmoveq %rcx, %rax
leaq 0x317098(%rip), %rcx    # 0x31c6e0
leaq 0x49da91(%rip), %rdx    # 0x4a30e0
cmoveq %rdx, %rcx
movq %rax, 0x624486(%rip)    # 0x629ae0
movq %rcx, 0x624487(%rip)    # 0x629ae8
xorl %eax, %eax
xchgb %al, 0x6244db(%rip)     # 0x629b44
movb 0x9c6a(%rip), %al       # 0xf2d9
testb $0x1, %al
je 0x567b
movb $0x1, %al
xchgb %al, 0x9c5d(%rip)       # 0xf2d8
movq %r13, %rdi
callq 0x3410
testl %eax, %eax
je 0x569a
movq 0x9962(%rip), %rdi      # 0xeff0
leaq 0x55de(%rip), %rsi      # 0xac73
callq 0x3240
movl 0x9c28(%rip), %eax      # 0xf2c8
xchgl %eax, 0x62449a(%rip)    # 0x629b40
movq %r12, %rdi
callq 0x31d0
jmp 0x55cd
movq %r12, %rdi
callq 0x31d0
jmp 0x55bf
cmpb $0x1, 0x9c08(%rip)      # 0xf2cf
jne 0x56d9
movq %r14, %rdi
callq 0x3300
movq %r15, %rdi
callq 0x3300
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq