// File: /workspace/github_cpp_makefile_star2_O2/Altius[P]hotspot2/src/hotspot2_part2.cpp
// Function: main
int main(int argc, char* argv[])
{
  // Option defaults
  long double fdr_threshold = 1.00;
  int write_pvals = 0;
  int print_help = 0;
  int print_version = 0;
  string infilename = "";
  string infilePvals = "";
  string infileChromNames = "";
  string outfilename = "";
  int numEntries = 0;

  // Long-opt definitions
  static struct option long_options[] = {
    { "fdr_threshold", required_argument, 0, 'f' },
    { "num_entries", required_argument, 0, 'n' },
    { "write_pvals", no_argument, &write_pvals, 1 },
    { "input", required_argument, 0, 'i' },
    { "infileChromNames", required_argument, 0, 'c' },
    { "infilePvalData", required_argument, 0, 'p' },
    { "output", required_argument, 0, 'o' },
    { "help", no_argument, &print_help, 1 },
    { "version", no_argument, &print_version, 1 },
    { 0, 0, 0, 0 }
  };

  // Parse options
  char c;
  stringstream ss; // Used for parsing doubles (allows scientific notation)
  while ((c = getopt_long(argc, argv, "f:n:c:p:i:o:hvV", long_options, NULL)) != -1)
    {
      switch (c)
        {
        case 'f':
          ss << optarg;
          ss >> fdr_threshold;
          break;
        case 'n':
          numEntries = atoi(optarg);
          break;
        case 'i':
          infilename = optarg;
          break;
        case 'c':
          infileChromNames = optarg;
          break;
        case 'p':
          infilePvals = optarg;
          break;
	case 'o':
          outfilename = optarg;
          break;
        case 'h':
          print_help = 1;
          break;
        case 'v':
        case 'V':
          print_version = 1;
          break;
        // no short option needed for --write_pvals
        case 0:
          // long option received, do nothing
          break;
        default:
          print_help = 1;
        }
    }

  if (!print_help && !print_version && infileChromNames.empty())
    {
      cerr << "Error:  Required file containing mapping from integers to chromosome names was not supplied."
	   << endl << endl;
      print_help = 1;
    }
  
  if (!print_help && !print_version && infilePvals.empty())
    {
      cerr << "Error:  Required file containing scaled -log10(P) values and their occurrence counts was not supplied."
	   << endl << endl;
      print_help = 1;
    }
 
  // Print usage and exit if necessary
  if (print_help)
    {
      cerr << "Usage:  " << argv[0] << " [options] < in.PvalueData.txt > out.FDR.bed\n"
           << "\n"
           << "Options: \n"
           << "  -n, --num_entries=INT          The number of lines of input (required for optimization)\n"
           << "  -p, --inputPvalData=FILE       A file of scaled -log10(P) values and their occurrence counts\n"
	   << "  -c, --inputChromNames=FILE     A file containing the mapping from integers to chromosome names\n"
           << "  --write_pvals                  Output P-values in column 6 (P-values are not output by default)\n"
           << "  -f, --fdr_threshold=THRESHOLD  Do not output sites with FDR > THRESHOLD (1.00)\n"
           << "  -i, --input=FILE               A file to read input from (STDIN)\n"
           << "  -o, --output=FILE              A file to write output to (STDOUT)\n"
           << "  -v, --version                  Print the version information and exit\n"
           << "  -h, --help                     Display this helpful help\n"
           << "\n"
           << " output (sent to stdout) will be a .bed5 file with FDR in field 5\n"
           << "\tor, if --write_pvals is specified, a .bed6 file with P-values appended in field 6\n"
           << " input (via \"-i FILE\" or piped in from stdin) consists of many rows of values in 4 columns.\n"
           << endl
           << endl;
      return -1;
    }

  if (print_version)
    {
      cout << argv[0] << " version " << hotspot2_VERSION_MAJOR
           << '.' << hotspot2_VERSION_MINOR << endl;
      return 0;
    }

  ios_base::sync_with_stdio(false); // calling this static method in this way turns off checks, speeds up I/O

  if (!infilename.empty() && infilename != "-")
    {
      if (freopen(infilename.c_str(), "r", stdin) == NULL)
        {
          cerr << "Error: Couldn't open input file " << infilename << endl;
          return 1;
        }
    }
  if (!outfilename.empty() && outfilename != "-")
    {
      if (freopen(outfilename.c_str(), "w", stdout) == NULL)
        {
          cerr << "Error: Couldn't open output file " << outfilename << " for writing" << endl;
          return 1;
        }
    }

  ifstream ifsChromNames(infileChromNames.c_str()), ifsPvals(infilePvals.c_str());
  if (!ifsChromNames)
    {
      cerr << "Error:  Unable to open file \"" << infileChromNames << "\" for read." << endl;
      return 1;
    }
  if (!ifsPvals)
    {
      cerr << "Error:  Unable to open file \"" << infilePvals << "\" for read." << endl;
      return 1;
    }

  map<int, string*> IntToChromNameMap;
  if (!buildIntToChromNameMap(ifsChromNames, IntToChromNameMap))
    return -1;

  vector<pair<int, long double> > PvalToFDRmapping;
  if (!buildFDRmapping(ifsPvals, PvalToFDRmapping))
    return -1;

  if (!parseAndProcessInput(IntToChromNameMap, PvalToFDRmapping, numEntries, fdr_threshold, write_pvals ? true : false))
    return -1;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x698, %rsp            # imm = 0x698
movq %rsi, %r14
movl %edi, %ebp
andl $0x0, 0x1c(%rsp)
fld1
andl $0x0, 0x8(%rsp)
fstpt 0xa0(%rsp)
andl $0x0, 0x10(%rsp)
leaq 0x25c0(%rip), %rbx      # 0x5384
leaq 0x40(%rsp), %rdi
leaq 0x100(%rsp), %rdx
movq %rbx, %rsi
callq 0x3664
leaq 0x80(%rsp), %rdi
leaq 0x100(%rsp), %rdx
movq %rbx, %rsi
callq 0x3664
leaq 0x258c(%rip), %rsi      # 0x5384
leaq 0x60(%rsp), %rdi
leaq 0x100(%rsp), %rdx
callq 0x3664
leaq 0x2573(%rip), %rsi      # 0x5384
leaq 0x20(%rsp), %rdi
leaq 0x100(%rsp), %rdx
callq 0x3664
andl $0x0, 0xc(%rsp)
movb 0x54c2(%rip), %al       # 0x82f0
testb %al, %al
je 0x33f8
leaq 0x308(%rsp), %rdi
callq 0x2120
movl 0xc(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x2593(%rip), %r12      # 0x53e5
leaq 0x5357(%rip), %rbx      # 0x81b0
leaq 0x21b8(%rip), %r15      # 0x5018
movq 0x5149(%rip), %r13      # 0x7fb0
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x2270
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x2f1c
sarl $0x18, %eax
leal -0x63(%rax), %ecx
cmpl $0x13, %ecx
ja 0x2ec9
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
leaq 0x60(%rsp), %rdi
jmpq *%rax
movq (%r13), %rsi
leaq 0x318(%rsp), %rdi
callq 0x2190
leaq 0x308(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21f0
jmp 0x2e67
testl %eax, %eax
je 0x2e67
cmpl $0x56, %eax
jne 0x2edc
movl $0x1, 0x10(%rsp)
jmp 0x2e67
movl $0x1, 0x8(%rsp)
jmp 0x2e67
leaq 0x40(%rsp), %rdi
jmp 0x2efc
leaq 0x80(%rsp), %rdi
jmp 0x2efc
leaq 0x20(%rsp), %rdi
movq (%r13), %rsi
callq 0x22e0
jmp 0x2e67
movq (%r13), %rdi
callq 0x2230
movl %eax, 0x18(%rsp)
jmp 0x2e67
movl 0x18(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %ecx
movl 0x10(%rsp), %eax
testl %ecx, %ecx
jne 0x2f69
testl %eax, %eax
jne 0x2f69
cmpq $0x0, 0x68(%rsp)
jne 0x2f69
movq 0x50ad(%rip), %rdi      # 0x7ff0
leaq 0x24ab(%rip), %rsi      # 0x53f5
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
movl $0x1, 0x8(%rsp)
jmp 0x2f7b
movl %ecx, %edx
orl %eax, %edx
je 0x30e8
testl %ecx, %ecx
je 0x31e7
movq 0x506e(%rip), %rdi      # 0x7ff0
leaq 0x2530(%rip), %rsi      # 0x54b9
callq 0x2190
movq (%r14), %rsi
movq %rax, %rdi
callq 0x2190
leaq 0x2522(%rip), %rsi      # 0x54c2
movq %rax, %rdi
callq 0x2190
leaq 0x254a(%rip), %rsi      # 0x54f9
movq %rax, %rdi
callq 0x2190
leaq 0x2532(%rip), %rsi      # 0x54f0
movq %rax, %rdi
callq 0x2190
leaq 0x252e(%rip), %rsi      # 0x54fb
movq %rax, %rdi
callq 0x2190
leaq 0x257a(%rip), %rsi      # 0x5556
movq %rax, %rdi
callq 0x2190
leaq 0x25cb(%rip), %rsi      # 0x55b6
movq %rax, %rdi
callq 0x2190
leaq 0x261e(%rip), %rsi      # 0x5618
movq %rax, %rdi
callq 0x2190
leaq 0x2672(%rip), %rsi      # 0x567b
movq %rax, %rdi
callq 0x2190
leaq 0x26b5(%rip), %rsi      # 0x56cd
movq %rax, %rdi
callq 0x2190
leaq 0x26ea(%rip), %rsi      # 0x5711
movq %rax, %rdi
callq 0x2190
leaq 0x2720(%rip), %rsi      # 0x5756
movq %rax, %rdi
callq 0x2190
leaq 0x275a(%rip), %rsi      # 0x579f
movq %rax, %rdi
callq 0x2190
leaq 0x24a5(%rip), %rsi      # 0x54f9
movq %rax, %rdi
callq 0x2190
leaq 0x2778(%rip), %rsi      # 0x57db
movq %rax, %rdi
callq 0x2190
leaq 0x27ac(%rip), %rsi      # 0x581e
movq %rax, %rdi
callq 0x2190
leaq 0x27f1(%rip), %rsi      # 0x5872
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
pushq $-0x1
popq %rbx
leaq 0x308(%rsp), %rdi
callq 0x2140
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x60(%rsp), %rdi
callq 0x20c0
leaq 0x80(%rsp), %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x20c0
movl %ebx, %eax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x88(%rsp)
je 0x323d
xorl %edi, %edi
callq 0x20a0
pushq $0x1
popq %rbx
cmpq $0x0, 0x48(%rsp)
je 0x3142
leaq 0x27c9(%rip), %rsi      # 0x58d9
leaq 0x40(%rsp), %rdi
callq 0x3759
testb %al, %al
je 0x3142
movq 0x40(%rsp), %rdi
movq 0x4e96(%rip), %rax      # 0x7fc0
movq (%rax), %rdx
leaq 0x27a7(%rip), %rsi      # 0x58db
callq 0x21d0
testq %rax, %rax
je 0x32db
cmpq $0x0, 0x28(%rsp)
je 0x3183
leaq 0x2788(%rip), %rsi      # 0x58d9
leaq 0x20(%rsp), %rdi
callq 0x3759
testb %al, %al
je 0x3183
movq 0x20(%rsp), %rdi
movq 0x4e35(%rip), %rax      # 0x7fa0
movq (%rax), %rdx
leaq 0x2789(%rip), %rsi      # 0x58fe
callq 0x21d0
testq %rax, %rax
je 0x3395
movq 0x60(%rsp), %rsi
leaq 0x100(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x22d0
movq 0x80(%rsp), %rsi
leaq 0x490(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x22d0
movq 0x100(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x120(%rsp,%rax)
je 0x3265
movq 0x4e1f(%rip), %rdi      # 0x7ff0
leaq 0x2757(%rip), %rsi      # 0x592f
callq 0x2190
leaq 0x60(%rsp), %rsi
jmp 0x329a
testl %eax, %eax
je 0x30f7
movq (%r14), %rsi
movq 0x4dcf(%rip), %rdi      # 0x7fc8
callq 0x2190
leaq 0x26ca(%rip), %rsi      # 0x58cf
movq %rax, %rdi
callq 0x2190
pushq $0x2
popq %rsi
movq %rax, %rdi
callq 0x2290
pushq $0x2e
popq %rsi
movq %rax, %rdi
callq 0x21e0
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x2290
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x20d0
jmp 0x309c
movq 0x4dac(%rip), %rdi      # 0x7ff0
leaq 0x2207(%rip), %rsi      # 0x5452
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
jmp 0x2f5f
movq 0x490(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x4b0(%rsp,%rax)
je 0x3300
movq 0x4d6a(%rip), %rdi      # 0x7ff0
leaq 0x26a2(%rip), %rsi      # 0x592f
callq 0x2190
leaq 0x80(%rsp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x26a4(%rip), %rsi      # 0x594d
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
leaq 0x490(%rsp), %rdi
callq 0x2040
leaq 0x100(%rsp), %rdi
callq 0x2040
jmp 0x309c
movq 0x4d0e(%rip), %rdi      # 0x7ff0
leaq 0x25f4(%rip), %rsi      # 0x58dd
callq 0x2190
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x2180
jmp 0x33c4
leaq 0xd8(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x100(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x2790
testb %al, %al
je 0x33d1
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x490(%rsp), %rdi
callq 0x243e
testb %al, %al
je 0x33d6
cmpl $0x0, 0x1c(%rsp)
leaq 0x17(%rsp), %r8
setne (%r8)
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
callq 0x2931
movzbl %al, %ebx
decl %ebx
jmp 0x33d9
movq 0x4c54(%rip), %rdi      # 0x7ff0
leaq 0x255d(%rip), %rsi      # 0x5900
callq 0x2190
leaq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x2566(%rip), %rsi      # 0x5922
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
jmp 0x309c
pushq $-0x1
popq %rbx
jmp 0x33e6
pushq $-0x1
popq %rbx
leaq 0xb0(%rsp), %rdi
callq 0x4950
leaq 0xd0(%rsp), %rdi
callq 0x37c6
jmp 0x32bc
leaq 0x4ef1(%rip), %rdi      # 0x82f0
callq 0x22c0
testl %eax, %eax
je 0x2e36
leaq 0x1f72(%rip), %rax      # 0x5385
movq %rax, 0x4d96(%rip)      # 0x81b0
pushq $0x1
popq %rax
movl %eax, 0x4d95(%rip)      # 0x81b8
andq $0x0, 0x4d95(%rip)      # 0x81c0
movl $0x66, 0x4d93(%rip)     # 0x81c8
leaq 0x1f57(%rip), %rcx      # 0x5393
movq %rcx, 0x4d8d(%rip)      # 0x81d0
movl %eax, 0x4d8f(%rip)      # 0x81d8
andq $0x0, 0x4d8f(%rip)      # 0x81e0
movl $0x6e, 0x4d8d(%rip)     # 0x81e8
leaq 0x1f3d(%rip), %rcx      # 0x539f
movq %rcx, 0x4d87(%rip)      # 0x81f0
andl $0x0, 0x4d88(%rip)      # 0x81f8
leaq 0x1c(%rsp), %rcx
movq %rcx, 0x4d84(%rip)      # 0x8200
movl %eax, 0x4d86(%rip)      # 0x8208
leaq 0x1f22(%rip), %rcx      # 0x53ab
movq %rcx, 0x4d80(%rip)      # 0x8210
movl %eax, 0x4d82(%rip)      # 0x8218
andq $0x0, 0x4d82(%rip)      # 0x8220
movl $0x69, 0x4d80(%rip)     # 0x8228
leaq 0x1f02(%rip), %rcx      # 0x53b1
movq %rcx, 0x4d7a(%rip)      # 0x8230
movl %eax, 0x4d7c(%rip)      # 0x8238
andq $0x0, 0x4d7c(%rip)      # 0x8240
movl $0x63, 0x4d7a(%rip)     # 0x8248
leaq 0x1eed(%rip), %rcx      # 0x53c2
movq %rcx, 0x4d74(%rip)      # 0x8250
movl %eax, 0x4d76(%rip)      # 0x8258
andq $0x0, 0x4d76(%rip)      # 0x8260
movl $0x70, 0x4d74(%rip)     # 0x8268
leaq 0x1ed6(%rip), %rcx      # 0x53d1
movq %rcx, 0x4d6e(%rip)      # 0x8270
movl %eax, 0x4d70(%rip)      # 0x8278
andq $0x0, 0x4d70(%rip)      # 0x8280
movl $0x6f, 0x4d6e(%rip)     # 0x8288
leaq 0x1eb7(%rip), %rcx      # 0x53d8
movq %rcx, 0x4d68(%rip)      # 0x8290
andl $0x0, 0x4d69(%rip)      # 0x8298
leaq 0x8(%rsp), %rcx
movq %rcx, 0x4d65(%rip)      # 0x82a0
movl %eax, 0x4d67(%rip)      # 0x82a8
leaq 0x1e95(%rip), %rcx      # 0x53dd
movq %rcx, 0x4d61(%rip)      # 0x82b0
andl $0x0, 0x4d62(%rip)      # 0x82b8
leaq 0x10(%rsp), %rcx
movq %rcx, 0x4d5e(%rip)      # 0x82c0
movl %eax, 0x4d60(%rip)      # 0x82c8
xorps %xmm0, %xmm0
movaps %xmm0, 0x4d6e(%rip)     # 0x82e0
movaps %xmm0, 0x4d57(%rip)     # 0x82d0
leaq 0x4d70(%rip), %rdi      # 0x82f0
callq 0x2100
jmp 0x2e36
jmp 0x358c
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x4950
jmp 0x35a1
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x37c6
jmp 0x35ba
movq %rax, %rbx
jmp 0x35c7
jmp 0x35ec
movq %rax, %rbx
leaq 0x490(%rsp), %rdi
callq 0x2040
leaq 0x100(%rsp), %rdi
callq 0x2040
jmp 0x35ef
movq %rax, %rbx
jmp 0x35fc
movq %rax, %rbx
jmp 0x3606
movq %rax, %rbx
jmp 0x3610
movq %rax, %rbx
jmp 0x361d
jmp 0x35ec
movq %rax, %rbx
leaq 0x308(%rsp), %rdi
callq 0x2140
leaq 0x20(%rsp), %rdi
callq 0x20c0
leaq 0x60(%rsp), %rdi
callq 0x20c0
leaq 0x80(%rsp), %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x22a0
nop