// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/parallel_build.cpp
// Function: main
int
main(int argc, char** argv)
{
  std::cout << "Parallel RLCSA builder" << std::endl;
  if(argc < 3)
  {
    std::cout << "Usage: parallel_build [-f|-n] listname output [threads]" << std::endl;
    std::cout << "  -f   use fast algorithm with larger memory usage" << std::endl;
    std::cout << "  -n   do not merge the indexes" << std::endl;
    return 1;
  }

  int list_parameter = 1, output_parameter = 2, threads_parameter = 3;
  bool do_merge = true;
  bool fast_algorithm = false;
  if(argv[1][0] == '-')
  {
    if(argv[1][1] == 'f')
    {
      list_parameter++; output_parameter++; threads_parameter++;
      fast_algorithm = true;
      std::cout << "Using fast algorithm." << std::endl;
    }
    else if(argv[1][1] == 'n')
    {
      list_parameter++; output_parameter++; threads_parameter++;
      do_merge = false;
      std::cout << "Option '-n' specified. Partial indexes will not be merged." << std::endl;
    }
  }

  std::ifstream filelist(argv[list_parameter], std::ios_base::binary);
  if(!filelist)
  {
    std::cerr << "Error opening file list!" << std::endl;
    return 2;
  }
  std::vector<std::string> files;
  readRows(filelist, files, true);
  filelist.close();
  std::cout << "Input files: " << files.size() << std::endl;

  std::string base_name = argv[output_parameter];
  std::cout << "Output: " << base_name << std::endl;

  usint threads = 1;
  if(argc > threads_parameter)
  {
    threads = std::min(MAX_THREADS, std::max(atoi(argv[threads_parameter]), 1));
  }
  std::cout << "Threads: " << threads << std::endl; 
  std::cout << std::endl;

  std::string parameters_name = base_name + PARAMETERS_EXTENSION;
  Parameters parameters;
  parameters.set(RLCSA_BLOCK_SIZE);
  parameters.set(SAMPLE_RATE);
  parameters.set(SUPPORT_LOCATE);
  parameters.set(SUPPORT_DISPLAY);
  parameters.set(WEIGHTED_SAMPLES);
  parameters.read(parameters_name);
  parameters.print();

  double start = readTimer();
  double megabytes = 0.0, build_time = 0.0;
  RLCSABuilder builder(parameters.get(RLCSA_BLOCK_SIZE), parameters.get(SAMPLE_RATE), 0, threads);

  if(fast_algorithm)
  {
    for(std::vector<std::string>::iterator iter = files.begin(); iter != files.end(); ++iter)
    {
      double mark = readTimer();
      std::cout << "Increment: " << *iter; std::cout.flush();
      builder.insertCollection(*iter);
      std::cout << " (" << (readTimer() - mark) << " seconds)" << std::endl;
    }
    std::cout << std::endl;
    megabytes = getRLCSA(builder, base_name);
  }
  else
  {
    megabytes = indexParts(files, threads, parameters, build_time);
    if(do_merge)
    {
      std::cout << "Phase 2: Merging the indexes" << std::endl;
      for(std::vector<std::string>::iterator iter = files.begin(); iter != files.end(); ++iter)
      {
        double mark = readTimer();
        std::cout << "Increment: " << *iter; std::cout.flush();
        builder.insertFromFile(*iter);
        std::cout << " (" << (readTimer() - mark) << " seconds)" << std::endl;
      }
      std::cout << std::endl;
      megabytes = getRLCSA(builder, base_name);
    }
  }

  double stop = readTimer();
  std::cout << megabytes << " megabytes indexed in " << (stop - start) << " seconds (" << (megabytes / (stop - start)) << " MB/s)." << std::endl;
  if(do_merge)
  {
    std::cout << "Build time:    " << build_time + builder.getBuildTime() << " seconds" << std::endl;
    std::cout << "Search time:   " << builder.getSearchTime() << " seconds" << std::endl;
    std::cout << "Sort time:     " << builder.getSortTime() << " seconds" << std::endl;
    std::cout << "Merge time:    " << builder.getMergeTime() << " seconds" << std::endl;
  }
  std::cout << "Memory usage:  " << memoryUsage() << " kB" << std::endl;
  std::cout << std::endl;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rsi, %r14
movl %edi, %ebp
movq 0x1ae47(%rip), %rdi     # 0x1efd0
leaq 0xff26(%rip), %rsi      # 0x140b6
callq 0x3200
movq %rax, %rdi
callq 0x30f0
cmpl $0x2, %ebp
jg 0x41f6
pushq $0x1
popq %rbx
movq 0x1ae24(%rip), %r14     # 0x1efd0
leaq 0xff1a(%rip), %rsi      # 0x140cd
movq %r14, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
leaq 0xff3b(%rip), %rsi      # 0x14105
movq %r14, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
leaq 0xff57(%rip), %rsi      # 0x14138
movq %r14, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
jmp 0x4879
movq 0x8(%r14), %rcx
pushq $0x1
popq %rax
pushq $0x2
popq %r13
pushq $0x3
popq %r12
cmpb $0x2d, (%rcx)
jne 0x422a
movzbl 0x1(%rcx), %ecx
movb $0x1, %dl
movl %edx, 0x14(%rsp)
cmpl $0x66, %ecx
je 0x4235
cmpl $0x6e, %ecx
jne 0x4230
leaq 0xff49(%rip), %rsi      # 0x1416e
xorl %r15d, %r15d
jmp 0x423f
movb $0x1, %cl
movl %ecx, 0x14(%rsp)
xorl %r15d, %r15d
jmp 0x4263
leaq 0xff1c(%rip), %rsi      # 0x14158
movb $0x1, %r15b
pushq $0x4
popq %r12
movq 0x1ad86(%rip), %rdi     # 0x1efd0
callq 0x3200
movq %rax, %rdi
callq 0x30f0
pushq $0x2
popq %rax
pushq $0x3
popq %r13
movl %r15d, 0x14(%rsp)
movq (%r14,%rax,8), %rsi
leaq 0x100(%rsp), %rbx
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x3420
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x120(%rsp,%rax)
je 0x42ae
movq 0x1ad5e(%rip), %rdi     # 0x1eff0
leaq 0xff10(%rip), %rsi      # 0x141a9
callq 0x3200
movq %rax, %rdi
callq 0x30f0
pushq $0x2
popq %rbx
jmp 0x486c
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x100(%rsp), %rdi
pushq $0x1
popq %rbx
movl %ebx, %edx
callq 0x11ffa
leaq 0x100(%rsp), %rdi
callq 0x3080
movq 0x1acea(%rip), %rdi     # 0x1efd0
leaq 0xfed5(%rip), %rsi      # 0x141c2
callq 0x3200
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x3150
movq %rax, %rdi
callq 0x30f0
movq (%r14,%r13,8), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x4d1a
movq 0x1aca3(%rip), %rdi     # 0x1efd0
leaq 0xfe9c(%rip), %rsi      # 0x141d0
callq 0x3200
leaq 0x78(%rsp), %rsi
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
cmpl %ebp, %r12d
jae 0x4371
movl %r12d, %eax
movq (%r14,%rax,8), %rdi
callq 0x3350
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
cmpl $0x40, %ecx
pushq $0x40
popq %rbx
cmovll %ecx, %ebx
movq 0x1ac58(%rip), %rdi     # 0x1efd0
leaq 0xfe5a(%rip), %rsi      # 0x141d9
callq 0x3200
movl %ebx, %esi
movq %rax, %rdi
callq 0x3150
movq %rax, %rdi
callq 0x30f0
movq 0x1ac33(%rip), %rdi     # 0x1efd0
callq 0x30f0
leaq 0x1af2f(%rip), %rdx     # 0x1f2d8
leaq 0xe0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x4de4
leaq 0x48(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x1af7c(%rip), %rsi     # 0x1f358
leaq 0x40(%rsp), %rdi
callq 0x11558
leaq 0x1af93(%rip), %rsi     # 0x1f380
leaq 0x40(%rsp), %rdi
callq 0x11558
leaq 0x1afaa(%rip), %rsi     # 0x1f3a8
leaq 0x40(%rsp), %rdi
callq 0x11558
leaq 0x1afc1(%rip), %rsi     # 0x1f3d0
leaq 0x40(%rsp), %rdi
callq 0x11558
leaq 0x1afd8(%rip), %rsi     # 0x1f3f8
leaq 0x40(%rsp), %rdi
callq 0x11558
leaq 0x40(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x117a0
leaq 0x40(%rsp), %rdi
callq 0x1184e
callq 0x121f8
movsd %xmm0, 0x70(%rsp)
andq $0x0, 0x38(%rsp)
leaq 0x1aefa(%rip), %rsi     # 0x1f358
leaq 0x40(%rsp), %rdi
callq 0x11546
movl %eax, %ebp
leaq 0x1af0f(%rip), %rsi     # 0x1f380
leaq 0x40(%rsp), %rdi
callq 0x11546
leaq 0x98(%rsp), %rdi
movl %ebp, %esi
movl %eax, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x9ef4
testb %r15b, %r15b
je 0x453c
movq 0x20(%rsp), %rbx
movq 0x1ab27(%rip), %r14     # 0x1efd0
leaq 0x98(%rsp), %r12
leaq 0xfd7d(%rip), %r13      # 0x14235
leaq 0xfd30(%rip), %rbp      # 0x141ef
cmpq 0x28(%rsp), %rbx
je 0x4619
callq 0x121f8
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
leaq 0xfd04(%rip), %rsi      # 0x141e3
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31f0
movq %r14, %rdi
callq 0x3160
movq %r12, %rdi
movq %rbx, %rsi
callq 0xa3d0
movq %r14, %rdi
movq %r13, %rsi
callq 0x3200
movq %rax, %r15
callq 0x121f8
subsd 0x8(%rsp), %xmm0
movq %r15, %rdi
callq 0x3380
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
addq $0x20, %rbx
jmp 0x44bf
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movl %ebx, %esi
callq 0x497a
movsd %xmm0, 0x18(%rsp)
cmpb $0x0, 0x14(%rsp)
je 0x463d
movq 0x1aa66(%rip), %rdi     # 0x1efd0
leaq 0xfc88(%rip), %rsi      # 0x141f9
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x20(%rsp), %rbx
movq 0x1aa46(%rip), %r14     # 0x1efd0
leaq 0x98(%rsp), %r12
leaq 0xfc9c(%rip), %r13      # 0x14235
leaq 0xfc4f(%rip), %rbp      # 0x141ef
cmpq 0x28(%rsp), %rbx
je 0x4619
callq 0x121f8
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
leaq 0xfc27(%rip), %rsi      # 0x141e3
callq 0x3200
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31f0
movq %r14, %rdi
callq 0x3160
movq %r12, %rdi
movq %rbx, %rsi
callq 0xa1ec
movq %r14, %rdi
movq %r13, %rsi
callq 0x3200
movq %rax, %r15
callq 0x121f8
subsd 0x8(%rsp), %xmm0
movq %r15, %rdi
callq 0x3380
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
addq $0x20, %rbx
jmp 0x45a0
movq 0x1a9b0(%rip), %rdi     # 0x1efd0
callq 0x30f0
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x4901
movsd %xmm0, 0x18(%rsp)
callq 0x121f8
movsd %xmm0, 0x8(%rsp)
movq 0x1a981(%rip), %rdi     # 0x1efd0
movsd 0x18(%rsp), %xmm0
callq 0x3380
leaq 0xfbb5(%rip), %rsi      # 0x14216
movq %rax, %rdi
callq 0x3200
movsd 0x8(%rsp), %xmm0
subsd 0x70(%rsp), %xmm0
movq %rax, %rdi
movsd %xmm0, 0x8(%rsp)
callq 0x3380
leaq 0xfba3(%rip), %rsi      # 0x1422d
movq %rax, %rdi
callq 0x3200
movsd 0x18(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
movq %rax, %rdi
callq 0x3380
leaq 0xfb8b(%rip), %rsi      # 0x14238
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
cmpb $0x0, 0x14(%rsp)
je 0x47ea
movq 0x1a901(%rip), %rdi     # 0x1efd0
leaq 0xfb6a(%rip), %rsi      # 0x14240
callq 0x3200
movq %rax, %rbx
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x98(%rsp), %rdi
callq 0xa8b0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x3380
leaq 0xfb3c(%rip), %rsi      # 0x14250
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a8a5(%rip), %rdi     # 0x1efd0
leaq 0xfb27(%rip), %rsi      # 0x14259
callq 0x3200
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0xa8b6
movq %rbx, %rdi
callq 0x3380
leaq 0xfafa(%rip), %rsi      # 0x14250
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a863(%rip), %rdi     # 0x1efd0
leaq 0xfaf5(%rip), %rsi      # 0x14269
callq 0x3200
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0xa8bc
movq %rbx, %rdi
callq 0x3380
leaq 0xfab8(%rip), %rsi      # 0x14250
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a821(%rip), %rdi     # 0x1efd0
leaq 0xfac3(%rip), %rsi      # 0x14279
callq 0x3200
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0xa8c2
movq %rbx, %rdi
callq 0x3380
leaq 0xfa76(%rip), %rsi      # 0x14250
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a7df(%rip), %rdi     # 0x1efd0
leaq 0xfa91(%rip), %rsi      # 0x14289
callq 0x3200
movq %rax, %rbx
callq 0x1220d
movl %eax, %esi
movq %rbx, %rdi
callq 0x3150
leaq 0xfa83(%rip), %rsi      # 0x14299
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a7a3(%rip), %rdi     # 0x1efd0
callq 0x30f0
leaq 0x98(%rsp), %rdi
callq 0x9f78
leaq 0x40(%rsp), %rdi
callq 0x4e48
leaq 0xe0(%rsp), %rdi
callq 0x3468
leaq 0x78(%rsp), %rdi
callq 0x3468
leaq 0x20(%rsp), %rdi
callq 0x4e1c
xorl %ebx, %ebx
leaq 0x100(%rsp), %rdi
callq 0x3090
movl %ebx, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x48a8
jmp 0x489e
jmp 0x48a3
jmp 0x48b1
jmp 0x48a8
jmp 0x489e
movq %rax, %rbx
jmp 0x48ec
movq %rax, %rbx
jmp 0x48d8
movq %rax, %rbx
jmp 0x48e2
movq %rax, %rbx
jmp 0x48c1
jmp 0x48b1
jmp 0x48b1
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x9f78
leaq 0x40(%rsp), %rdi
callq 0x4e48
leaq 0xe0(%rsp), %rdi
callq 0x3468
leaq 0x78(%rsp), %rdi
callq 0x3468
leaq 0x20(%rsp), %rdi
callq 0x4e1c
leaq 0x100(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3400