// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/parallel_build.cpp
// Function: indexParts(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, unsigned int, CSA::Parameters&, double&)
double
indexParts(std::vector<std::string>& filenames, usint threads, Parameters& parameters, double& build_time)
{
  double start = readTimer();
  std::cout << "Phase 1: Building indexes for input files" << std::endl;
  usint block_size = parameters.get(RLCSA_BLOCK_SIZE);
  usint sample_rate = parameters.get(SAMPLE_RATE);
  usint total_size = 0;

  for(usint i = 0; i < filenames.size(); i++)
  {
    double init = readTimer();
    uchar* data = 0; usint size = 0;
    std::cout << "Input: " << filenames[i]; std::cout.flush();
    std::ifstream input(filenames[i].c_str(), std::ios_base::binary);
    if(!input)
    {
      std::cerr << "Error opening input file " << filenames[i] << "!" << std::endl;
    }
    else
    {
      size = fileSize(input);
      data = new uchar[size];
      input.read((char*)data, size);
      input.close();
    }

    if(size > 0)
    {
      double mark = readTimer();
      RLCSA* index = new RLCSA(data, size, block_size, sample_rate, threads, true);
      double done = readTimer();
      std::cout << " (" << (done - init) << " seconds)" << std::endl;
      build_time += done - mark;
      if(index != 0 && index->isOk()) { index->writeTo(filenames[i]); total_size += size; }
      delete index;
    }
    else
    {
      std::cerr << "Warning: Empty input file " << filenames[i] << "!" << std::endl;
    }
  }

  double total_time = readTimer() - start;
  double megabytes = total_size / (double)MEGABYTE;
  std::cout << "Indexed " << megabytes << " megabytes in " << total_time << " seconds (" << (megabytes / total_time) << " MB/s)." << std::endl;
  std::cout << "Memory: " << memoryUsage() << " kB" << std::endl;
  std::cout << std::endl;

  return megabytes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rcx, 0x40(%rsp)
movq %rdx, %rbx
movl %esi, 0x34(%rsp)
movq %rdi, %r14
callq 0x121f8
movsd %xmm0, 0x38(%rsp)
movq 0x1a624(%rip), %rdi     # 0x1efd0
leaq 0xf8ea(%rip), %rsi      # 0x1429d
callq 0x3200
movq %rax, %rdi
callq 0x30f0
leaq 0x1a991(%rip), %rsi     # 0x1f358
movq %rbx, %rdi
callq 0x11546
movl %eax, 0x30(%rsp)
leaq 0x1a9a6(%rip), %rsi     # 0x1f380
movq %rbx, %rdi
callq 0x11546
movl %eax, 0x2c(%rsp)
xorl %r12d, %r12d
leaq 0x50(%rsp), %r15
movl $0x0, 0x1c(%rsp)
movl %r12d, %r12d
movq 0x8(%r14), %rbx
subq (%r14), %rbx
sarq $0x5, %rbx
callq 0x121f8
cmpq %r12, %rbx
jbe 0x4bf6
movsd %xmm0, 0x20(%rsp)
movq 0x1a5b1(%rip), %rbx     # 0x1efd0
movq %rbx, %rdi
leaq 0xf89e(%rip), %rsi      # 0x142c7
callq 0x3200
movq %r12, %r13
shlq $0x5, %r13
movq (%r14), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x31f0
movq %rbx, %rdi
callq 0x3160
movq (%r14), %rax
movq (%rax,%r13), %rsi
movq %r15, %rdi
pushq $0x4
popq %rdx
callq 0x3420
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x70(%rsp,%rax)
je 0x4ae2
movq 0x1a57c(%rip), %rdi     # 0x1eff0
leaq 0xf854(%rip), %rsi      # 0x142cf
callq 0x3200
movq (%r14), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
leaq 0xfdc8(%rip), %rsi      # 0x14860
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq 0x1a544(%rip), %rdi     # 0x1eff0
leaq 0xf836(%rip), %rsi      # 0x142e9
callq 0x3200
addq (%r14), %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0xfd90(%rip), %rsi      # 0x14860
callq 0x3200
movq %rax, %rdi
callq 0x30f0
jmp 0x4be1
movq %r15, %rdi
callq 0x11ef4
movq %rax, %rbx
movl %ebx, %ebp
movq %rbp, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x3280
leaq 0x50(%rsp), %rdi
callq 0x3080
testl %ebx, %ebx
je 0x4aa5
callq 0x121f8
movsd %xmm0, 0x48(%rsp)
movl $0x830, %edi            # imm = 0x830
callq 0x3220
movq %rax, %rbp
movl $0x1, (%rsp)
movq %rax, %rdi
movq %r15, %rsi
movl %ebx, %edx
movl 0x30(%rsp), %ecx
movl 0x2c(%rsp), %r8d
movl 0x34(%rsp), %r9d
callq 0x539a
callq 0x121f8
movsd %xmm0, 0x10(%rsp)
movq 0x1a46c(%rip), %rdi     # 0x1efd0
leaq 0xf6ca(%rip), %rsi      # 0x14235
callq 0x3200
movsd 0x10(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movq %rax, %rdi
callq 0x3380
movq %rax, %rdi
leaq 0xf661(%rip), %rsi      # 0x141ef
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movsd 0x10(%rsp), %xmm0
subsd 0x48(%rsp), %xmm0
movq 0x40(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
cmpb $0x1, (%rbp)
jne 0x4bcc
addq (%r14), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x62d4
addl %ebx, 0x1c(%rsp)
movq %rbp, %rdi
callq 0x621e
movl $0x830, %esi            # imm = 0x830
movq %rbp, %rdi
callq 0x3230
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0x3090
incl %r12d
jmp 0x49f6
subsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x1c(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd 0xf3f5(%rip), %xmm1     # 0x14008
movsd %xmm1, 0x10(%rsp)
movq 0x1a3b0(%rip), %rbx     # 0x1efd0
leaq 0xf6dd(%rip), %rsi      # 0x14304
movq %rbx, %rdi
callq 0x3200
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3380
leaq 0xf6c9(%rip), %rsi      # 0x1430d
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x3380
leaq 0xf5cc(%rip), %rsi      # 0x1422d
movq %rax, %rdi
callq 0x3200
movsd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq %rax, %rdi
callq 0x3380
leaq 0xf5b4(%rip), %rsi      # 0x14238
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
leaq 0xf681(%rip), %rsi      # 0x1431c
movq %rbx, %rdi
callq 0x3200
movq %rax, %r14
callq 0x1220d
movl %eax, %esi
movq %r14, %rdi
callq 0x3150
leaq 0xf5dd(%rip), %rsi      # 0x14299
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x30f0
movq %rbx, %rdi
callq 0x30f0
movsd 0x10(%rsp), %xmm0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x830, %esi            # imm = 0x830
movq %rbp, %rdi
callq 0x3230
jmp 0x4d07
jmp 0x4d04
jmp 0x4d04
jmp 0x4d04
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x3400
nop