// File: /workspace/github_cpp_makefile_star2_O2/Ahhgust[P]STRaitRazor/str8.cpp
// Function: main
int
main(int argc, char **argv) {

  if (argc < 3) {
    fprintf(stderr, "Not enough arguments!\n");
    usage(argv[0]);
  }

  unsigned i;

#ifdef OSX // for the named semaphores required by OSX
  char buff[100];
  sprintf(buff, "%ld", (long)getpid() );
#endif


  int start = parseArgs(argc, argv, opt);

  if ( opt.useQuality) {
    initQvalLUT( opt.useQuality );
    USE_QVALS=opt.useQuality; // working with pthreads is much easier if we just use globals... 
  }
  
  if (opt.numThreads == 0)
    opt.numThreads=1;
  
  int *ids;
  
  ids = new int [ opt.numThreads];
  matches = new Matches[ opt.numThreads ]; // each thread records the records it found

  
  
  for (i=0; i < (unsigned) opt.numThreads; ++i)
    ids[i] = i;
  
  std::ios::sync_with_stdio(false);
  
  // parse the config file
  c = parseConfig(opt.config, &numStrs, opt.type);

  biasCounts = new unsigned* [ opt.numThreads]; // and counts for partial allelic dropout  
  totalCounts = new unsigned* [ opt.numThreads]; // and counts for partial allelic dropout  

  leftFlankPosSum = new unsigned long* [ opt.numThreads]; // and counts for partial allelic dropout  
  rightFlankPosSum = new unsigned long* [ opt.numThreads]; // and counts for partial allelic dropout  

  for (i=0; i < (unsigned) opt.numThreads; ++i) {
    biasCounts[i] = new unsigned[ numStrs ](); // () initializes the bias counts to 0
    totalCounts[i] = new unsigned[ numStrs ](); // () initializes the total counts to 0

    leftFlankPosSum[i] = new unsigned long [ numStrs ](); 
    rightFlankPosSum[i] = new unsigned long [ numStrs ](); 
  }
  

  if (numStrs==0) {
    fprintf(stderr, "No markers found in file: %s\n", opt.config);
    return 1;
  }


  // compute minima and maxima for the STRs we're looking for
  minLen = min((*c)[0].forwardLength,  (*c)[0].reverseLength);
  maxLen = max(  (*c)[0].forwardLength,
                 max( (*c)[0].reverseLength, (*c)[0].motifLength));
  // assumes that the motif nomenclature cannot be negative
  minFrag =   (*c)[0].forwardLength +   (*c)[0].reverseLength + (*c)[0].motifOffset;
  for (i=1; i < numStrs; ++i) {

    if ((int)  (*c)[i].forwardLength < minLen)
      minLen =   (*c)[i].forwardLength;

    if ((int)  (*c)[i].reverseLength < minLen)
      minLen =   (*c)[i].reverseLength;

    if ((int)  (*c)[i].forwardLength > maxLen)
      maxLen =   (*c)[i].forwardLength;

    if ((int)  (*c)[i].reverseLength > maxLen)
      maxLen =   (*c)[i].reverseLength;

    if ((int)  (*c)[i].motifLength > maxLen)
      maxLen =   (*c)[i].motifLength;


    int fragLen =   (*c)[i].forwardLength +   (*c)[i].reverseLength + (*c)[i].motifOffset;
    if (fragLen < minFrag)
      minFrag = fragLen;
  }


  trie = new Trie;
  trie->makeTrieFromConfig(c, numStrs, opt.distance, opt.motifDistance);
    

#ifndef NOTHREADS
  if (opt.numThreads > 1) {
    if (pthread_mutex_init(&ioLock, 0)) {
      cerr << "Failed to init mutex..." << endl;
    }
    /* unnamed semaphore. not supported on OSX! */
#ifdef OSX
    writersLock = sem_open( buff , O_CREAT, 0600, 0);
#else
    if (sem_init(&writersLock, 0, 0)) {
      cerr << "Failed to init semaphore..." << endl;
      return 1;
    }
#endif

    

  }
#endif

  
  for (i=start ; i < (unsigned)argc; ++i) {
    ifstream in;
    in.open(argv[i], ios::in);
    if (! in.is_open() ) {
      cerr << "Failed to open " << argv[i] << " for reading\n";
      continue;
    }
    currentInputStream = &in;


    if (opt.numThreads < 2) 
      findMatchesOneThread(  );
    else {
#ifndef NOTHREADS
      int err;
      pthread_t t;
      list <pthread_t> threads;
            
      err = pthread_create(&t, NULL, writerThread, NULL ); 
      if (err) {
        cerr << "Error creating writer thread" << endl;
        exit(EXIT_FAILURE);
      }
      threads.push_back(t);

      for (int j=0; j < opt.numThreads; ++j) {
        err = pthread_create(&t, NULL, workerThread, (void*) &(ids[j]) ); 
        if (err) {
          cerr << "Error creating thread number: " << j << endl;
          exit(EXIT_FAILURE);
        }
        threads.push_back(t);
      }
      for (list<pthread_t>::iterator itr = threads.begin(); itr != threads.end(); ++itr) { // collect the threads when we're done
        t = *itr;
        pthread_join(t, NULL);
      }

      threads.clear();
      std::ios::sync_with_stdio(true);
      printReportsMT(opt.out);
      std::ios::sync_with_stdio(false);
#endif

    }
    in.close();
  }

  // if no fastq files are given then check stdin
  if (argc == start)  {

    currentInputStream = &cin;
    if (opt.numThreads < 2) 
      findMatchesOneThread(  );

    else {
#ifndef NOTHREADS
      int err;
      pthread_t t;
      list <pthread_t> threads;
            
      err = pthread_create(&t, NULL, writerThread, NULL ); 
      if (err) {
        cerr << "Error creating writer thread" << endl;
        exit(EXIT_FAILURE);
      }
      threads.push_back(t);
      
      for (int j=0; j < opt.numThreads; ++j) {
        err = pthread_create(&t, NULL, workerThread, (void*) &ids[j] ); 
        if (err) {
          cerr << "Error creating thread number: " << j << endl;
          exit(EXIT_FAILURE);
        }
        threads.push_back(t);
      }
      for (list<pthread_t>::iterator itr = threads.begin(); itr != threads.end(); ++itr) { // collect the threads when we're done
        t = *itr;
        pthread_join(t, NULL);
      }

      threads.clear();
      std::ios::sync_with_stdio(true);
      printReportsMT(opt.out);
      std::ios::sync_with_stdio(false);
#endif

    }

  }


#ifndef NOTHREADS
  if (opt.numThreads > 1) {
    pthread_mutex_destroy(&ioLock);

#ifdef OSX
    sem_unlink(buff);
#else
    sem_destroy(&writersLock);
#endif

  }
#endif

  // let's close our file handles, shall we?
  fclose(opt.out);

  // optionally can free memory here...
  // delete ids;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rsi, %rbx
cmpl $0x2, %edi
jle 0x5f05
leaq 0x9add(%rip), %rdx      # 0xf2a8
movl %edi, 0xc(%rsp)
movq %rbx, %rsi
callq 0x5116
movl %eax, 0x8(%rsp)
movb 0x9ad1(%rip), %al       # 0xf2b2
testb %al, %al
je 0x57f9
movsbl %al, %edi
callq 0x380c
movb 0x9abf(%rip), %al       # 0xf2b2
movb %al, 0x624b57(%rip)     # 0x62a350
movl 0x9ac9(%rip), %eax      # 0xf2c8
pushq $-0x1
popq %r15
testl %eax, %eax
movq %rbx, 0x18(%rsp)
je 0x5822
movslq %eax, %rbx
leaq (,%rbx,4), %rdi
testl %eax, %eax
cmovsq %r15, %rdi
movl %eax, %r12d
jmp 0x5835
movl $0x1, 0x9a9c(%rip)      # 0xf2c8
pushq $0x4
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %r12
pushq $0x30
popq %rcx
movq %rbx, %rax
mulq %rcx
movq %rax, %r14
seto %al
orq $0x8, %r14
testb %al, %al
cmovneq %r15, %r14
callq 0x3030
movq %rax, %r13
movq %r14, %rdi
callq 0x3030
movq %rbx, (%rax)
leaq 0x8(%rax), %rcx
addq $0x10, %rax
imulq $0x30, %rbx, %rdx
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
addq $0x30, %rax
addq $-0x30, %rdx
jne 0x586d
movq %rcx, 0x624b0d(%rip)    # 0x62a3a0
xorl %eax, %eax
cmpq %rax, %r12
je 0x58a4
movl %eax, (%r13,%rax,4)
incq %rax
jmp 0x5895
movq %r13, 0x10(%rsp)
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x3100
movq 0x9a06(%rip), %rdi      # 0xf2c0
movq 0x9a0f(%rip), %rdx      # 0xf2d0
leaq 0x624a98(%rip), %rsi    # 0x62a360
callq 0x7a84
movq %rax, 0x624a84(%rip)    # 0x62a358
movslq 0x99ed(%rip), %rbp      # 0xf2c8
leaq (,%rbp,8), %rbx
testq %rbp, %rbp
cmovsq %r15, %rbx
movq %rbx, %rdi
callq 0x3030
movq %rax, 0x624a87(%rip)    # 0x62a380
movq %rbx, %rdi
callq 0x3030
movq %rax, 0x624a80(%rip)    # 0x62a388
movq %rbx, %rdi
callq 0x3030
movq %rax, 0x624a79(%rip)    # 0x62a390
movq %rbx, %rdi
callq 0x3030
movq %rax, 0x624a72(%rip)    # 0x62a398
movl 0x624a33(%rip), %r13d   # 0x62a360
leaq (,%r13,4), %r15
leaq (,%r13,8), %r12
movl %ebp, %ebp
cmpq %r14, %rbp
je 0x59dc
movq %r15, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3170
movq 0x624a19(%rip), %rax    # 0x62a380
movq %rbx, (%rax,%r14,8)
movq %r15, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3170
movq 0x6249fe(%rip), %rax    # 0x62a388
movq %rbx, (%rax,%r14,8)
movq %r12, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq 0x6249e3(%rip), %rax    # 0x62a390
movq %rbx, (%rax,%r14,8)
movq %r12, %rdi
callq 0x3030
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3170
movq 0x6249c8(%rip), %rax    # 0x62a398
movq %rbx, (%rax,%r14,8)
incq %r14
jmp 0x593f
pushq $0x1
popq %rbp
testq %r13, %r13
je 0x5e72
movq 0x624969(%rip), %rax    # 0x62a358
movq (%rax), %rax
movl 0xa0(%rax), %edi
movl 0xa4(%rax), %esi
cmpl %edi, %esi
movl %edi, %ecx
cmovbl %esi, %ecx
movl %ecx, 0x62495d(%rip)    # 0x62a368
movl 0xa8(%rax), %edx
cmpl %edx, %esi
cmoval %esi, %edx
cmpl %edx, %edi
cmoval %edi, %edx
movl %edx, 0x62494b(%rip)    # 0x62a36c
addl %esi, %edi
movzwl 0xae(%rax), %esi
addl %edi, %esi
movl %esi, 0x624932(%rip)    # 0x62a364
imulq $0xb8, %r13, %rdi
addq $-0xb8, %rdi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x5af6
movl 0x158(%rax,%r8), %r9d
cmpl %ecx, %r9d
jge 0x5a63
movl %r9d, 0x624908(%rip)    # 0x62a368
movl %r9d, %ecx
movl 0x15c(%rax,%r8), %r9d
cmpl %ecx, %r9d
jge 0x5a7a
movl %r9d, 0x6248f1(%rip)    # 0x62a368
movl %r9d, %ecx
movl 0x158(%rax,%r8), %r9d
cmpl %edx, %r9d
jle 0x5a91
movl %r9d, 0x6248de(%rip)    # 0x62a36c
movl %r9d, %edx
movl 0x15c(%rax,%r8), %r9d
cmpl %edx, %r9d
jle 0x5aa8
movl %r9d, 0x6248c7(%rip)    # 0x62a36c
movl %r9d, %edx
movl 0x160(%rax,%r8), %r9d
cmpl %edx, %r9d
jle 0x5abf
movl %r9d, 0x6248b0(%rip)    # 0x62a36c
movl %r9d, %edx
movl 0x15c(%rax,%r8), %r10d
addl 0x158(%rax,%r8), %r10d
movzwl 0x166(%rax,%r8), %r9d
addl %r10d, %r9d
cmpl %esi, %r9d
jge 0x5aea
movl %r9d, 0x62487d(%rip)    # 0x62a364
movl %r9d, %esi
addq $0xb8, %r8
jmp 0x5a43
pushq $0x18
popq %rdi
callq 0x3260
movq %rax, %r14
movq %rax, %rdi
callq 0x862c
movq %r14, 0x624860(%rip)    # 0x62a370
movq 0x624841(%rip), %rsi    # 0x62a358
movl 0x624843(%rip), %edx    # 0x62a360
movzbl 0x97a9(%rip), %ecx      # 0xf2cd
movzbl 0x97a2(%rip), %r8d      # 0xf2ce
movq %r14, %rdi
callq 0x8e9c
cmpl $0x2, 0x978d(%rip)      # 0xf2c8
jl 0x5b9e
leaq 0x623fb4(%rip), %rdi    # 0x629af8
xorl %esi, %esi
callq 0x3380
testl %eax, %eax
je 0x5b6a
movq 0x949a(%rip), %rdi      # 0xeff0
leaq 0x5163(%rip), %rsi      # 0xacc0
callq 0x3240
movq %rax, %rdi
callq 0x3130
leaq 0x623faf(%rip), %rdi    # 0x629b20
xorl %esi, %esi
xorl %edx, %edx
callq 0x3200
testl %eax, %eax
je 0x5b9e
movq 0x946b(%rip), %rdi      # 0xeff0
leaq 0x514c(%rip), %rsi      # 0xacd8
callq 0x3240
movq %rax, %rdi
callq 0x3130
jmp 0x5e5e
leaq 0x20(%rsp), %rbp
leaq 0x38(%rsp), %r13
leaq -0x638(%rip), %r15      # 0x5577
movl 0x8(%rsp), %r12d
movl 0xc(%rsp), %eax
cmpl %eax, %r12d
jae 0x5d26
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x32a0
movl %r12d, %ebx
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
pushq $0x8
popq %rdx
callq 0x3190
leaq 0xb8(%rsp), %rdi
callq 0x30a0
testb %al, %al
je 0x5c15
leaq 0x40(%rsp), %rax
movq %rax, 0x623eee(%rip)    # 0x629af0
cmpl $0x1, 0x96bf(%rip)      # 0xf2c8
jg 0x5c4d
callq 0x4e5c
jmp 0x5d0a
movq 0x93d4(%rip), %rdi      # 0xeff0
leaq 0x50d1(%rip), %rsi      # 0xacf4
callq 0x3240
movq 0x18(%rsp), %rcx
movq (%rcx,%rbx,8), %rsi
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
leaq 0x50c1(%rip), %rsi      # 0xad04
callq 0x3240
jmp 0x5d14
movq %rbp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %r13, %rdi
xorl %esi, %esi
leaq -0x57f(%rip), %rdx      # 0x56ea
xorl %ecx, %ecx
callq 0x3460
testl %eax, %eax
jne 0x5ec0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x61d4
movq 0x10(%rsp), %r14
xorl %ebx, %ebx
movslq 0x9637(%rip), %rax      # 0xf2c8
cmpq %rax, %rbx
jge 0x5cc2
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
callq 0x3460
testl %eax, %eax
jne 0x5e93
movq %rbp, %rdi
movq %r13, %rsi
callq 0x61d4
incq %rbx
addq $0x4, %r14
jmp 0x5c8a
movq %rbp, %rbx
movq (%rbx), %rbx
cmpq %rbp, %rbx
je 0x5cdf
movq 0x10(%rbx), %rdi
movq %rdi, 0x38(%rsp)
xorl %esi, %esi
callq 0x3230
jmp 0x5cc5
movq %rbp, %rdi
callq 0x61e0
pushq $0x1
popq %rdi
callq 0x3100
movq 0x95c2(%rip), %rdi      # 0xf2b8
callq 0x3f0e
xorl %edi, %edi
callq 0x3100
movq %rbp, %rdi
callq 0x6202
leaq 0x40(%rsp), %rdi
callq 0x3090
leaq 0x40(%rsp), %rdi
callq 0x30b0
incl %r12d
jmp 0x5bb4
cmpl %eax, 0x8(%rsp)
jne 0x5e2f
movq 0x92a1(%rip), %rax      # 0xefd8
movq %rax, 0x623db2(%rip)    # 0x629af0
cmpl $0x1, 0x9583(%rip)      # 0xf2c8
jg 0x5d51
callq 0x4e5c
jmp 0x5e2f
leaq 0x40(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq -0x683(%rip), %rdx      # 0x56ea
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x3460
testl %eax, %eax
jne 0x5f29
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x61d4
movq 0x10(%rsp), %r14
xorl %r15d, %r15d
leaq -0x82a(%rip), %r12      # 0x5577
leaq 0x20(%rsp), %r13
leaq 0x40(%rsp), %rbx
movslq 0x9516(%rip), %rax      # 0xf2c8
cmpq %rax, %r15
jge 0x5de3
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x3460
testl %eax, %eax
jne 0x5edd
movq %rbx, %rdi
movq %r13, %rsi
callq 0x61d4
incq %r15
addq $0x4, %r14
jmp 0x5dab
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x5e00
movq 0x10(%r14), %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
callq 0x3230
jmp 0x5de6
leaq 0x40(%rsp), %rdi
callq 0x61e0
pushq $0x1
popq %rdi
callq 0x3100
movq 0x949f(%rip), %rdi      # 0xf2b8
callq 0x3f0e
xorl %edi, %edi
callq 0x3100
leaq 0x40(%rsp), %rdi
callq 0x6202
cmpl $0x2, 0x9492(%rip)      # 0xf2c8
jl 0x5e50
leaq 0x623cb9(%rip), %rdi    # 0x629af8
callq 0x32c0
leaq 0x623cd5(%rip), %rdi    # 0x629b20
callq 0x34a0
movq 0x9461(%rip), %rdi      # 0xf2b8
callq 0x3250
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x914f(%rip), %rax      # 0xefc8
movq (%rax), %rdi
movq 0x943d(%rip), %rdx      # 0xf2c0
leaq 0x4e18(%rip), %rsi      # 0xaca2
xorl %eax, %eax
callq 0x3340
jmp 0x5e5e
movq 0x9156(%rip), %rdi      # 0xeff0
leaq 0x4e8e(%rip), %rsi      # 0xad2f
callq 0x3240
movq %rax, %rdi
movl %ebx, %esi
callq 0x3420
movq %rax, %rdi
callq 0x3130
pushq $0x1
popq %rdi
callq 0x32e0
movq 0x9129(%rip), %rdi      # 0xeff0
leaq 0x4e44(%rip), %rsi      # 0xad12
callq 0x3240
movq %rax, %rdi
callq 0x3130
jmp 0x5eb8
movq 0x910c(%rip), %rdi      # 0xeff0
leaq 0x4e44(%rip), %rsi      # 0xad2f
callq 0x3240
movq %rax, %rdi
movl %r15d, %esi
callq 0x3420
movq %rax, %rdi
callq 0x3130
jmp 0x5eb8
movq 0x90bc(%rip), %rax      # 0xefc8
movq (%rax), %rcx
leaq 0x4d75(%rip), %rdi      # 0xac8b
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x34c0
movq (%rbx), %rdi
callq 0x4efd
movq 0x90c0(%rip), %rdi      # 0xeff0
leaq 0x4ddb(%rip), %rsi      # 0xad12
callq 0x3240
movq %rax, %rdi
callq 0x3130
jmp 0x5eb8
jmp 0x5f61
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r14, %rdi
callq 0x3270
jmp 0x5f92
jmp 0x5f7b
jmp 0x5f61
jmp 0x5f61
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x6202
jmp 0x5f92
jmp 0x5f7b
movq %rax, %rbx
jmp 0x5f88
jmp 0x5f7b
jmp 0x5f7b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x6202
leaq 0x40(%rsp), %rdi
callq 0x30b0
movq %rbx, %rdi
callq 0x3440