// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl 0x62ff25(%rip), %eax    # 0x637390
shll $0x2, %eax
movl %eax, %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x18(%rbp)
movl 0x62fef5(%rip), %eax    # 0x637390
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x20(%rbp)
jmp 0x74ad
leaq 0x62f6bc(%rip), %rdi    # 0x636b70
callq 0x90d0
cmpl 0x14e39(%rip), %eax     # 0x1c2f8
jge 0x74c3
jmp 0x74ad
jmp 0x74c5
leaq 0x62f6ac(%rip), %rdi    # 0x636b78
callq 0x9150
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x51d0
leaq 0x14e20(%rip), %rdi     # 0x1c308
callq 0x9180
testb $0x1, %al
jne 0x74f3
jmp 0x7534
testb $0x1, 0x14e05(%rip)     # 0x1c2ff
je 0x752c
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x7513
movq -0x28(%rbp), %rdi
callq 0x3350
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x752a
movq -0x30(%rbp), %rdi
callq 0x3350
jmp 0x752c
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq
jmp 0x7536
leaq 0x62f63b(%rip), %rdi    # 0x636b78
callq 0x90d0
cmpl 0x14db0(%rip), %eax     # 0x1c2f8
jge 0x754c
jmp 0x7536
leaq 0x62f5d5(%rip), %rdi    # 0x636b28
callq 0x33b0
leaq 0x62f611(%rip), %rdi    # 0x636b70
callq 0x9210
leaq 0x62f605(%rip), %rdi    # 0x636b70
callq 0x90d0
cmpl $0x0, %eax
jne 0x7661
jmp 0x757b
leaq 0x62f5f2(%rip), %rdi    # 0x636b74
callq 0x9180
xorb $-0x1, %al
testb $0x1, %al
jne 0x758f
jmp 0x7591
jmp 0x757b
leaq 0x62f5e0(%rip), %rdi    # 0x636b78
xorl %esi, %esi
callq 0x9240
leaq 0x14d6a(%rip), %rax     # 0x1c310
cmpq %rax, 0x62f563(%rip)    # 0x636b10
jne 0x75cd
leaq 0x19b75a(%rip), %rax    # 0x1a2d10
movq %rax, 0x62f553(%rip)    # 0x636b10
leaq 0x4a8b4c(%rip), %rax    # 0x4b0110
movq %rax, 0x62f54d(%rip)    # 0x636b18
jmp 0x75e9
leaq 0x14d3c(%rip), %rax     # 0x1c310
movq %rax, 0x62f535(%rip)    # 0x636b10
leaq 0x32212e(%rip), %rax    # 0x329710
movq %rax, 0x62f52f(%rip)    # 0x636b18
leaq 0x62f584(%rip), %rdi    # 0x636b74
xorl %esi, %esi
callq 0x92d0
leaq 0x14d0b(%rip), %rdi     # 0x1c309
callq 0x9180
testb $0x1, %al
jne 0x7609
jmp 0x761a
leaq 0x14cf8(%rip), %rdi     # 0x1c308
movl $0x1, %esi
callq 0x92d0
leaq 0x62f52f(%rip), %rdi    # 0x636b50
callq 0x3470
cmpl $0x0, %eax
je 0x763e
movq 0x149be(%rip), %rdi     # 0x1bff0
leaq 0xd5d5(%rip), %rsi      # 0x14c0e
callq 0x3290
movl 0x14cb4(%rip), %esi     # 0x1c2f8
leaq 0x62f525(%rip), %rdi    # 0x636b70
callq 0x9240
leaq 0x62f4d1(%rip), %rdi    # 0x636b28
callq 0x3200
jmp 0x74c5
leaq 0x62f4c0(%rip), %rdi    # 0x636b28
callq 0x3200
jmp 0x74ab
nopw %cs:(%rax,%rax)