// File: 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
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
callq 0x26480
movsd %xmm0, -0x28(%rbp)
movq 0x34e95(%rip), %rdi     # 0x3afd0
leaq 0x2515b(%rip), %rsi     # 0x2b29d
callq 0x32a0
movq %rax, %rdi
movq 0x34e5f(%rip), %rsi     # 0x3afb0
callq 0x3300
movq -0x18(%rbp), %rdi
leaq 0x352a7(%rip), %rsi     # 0x3b408
callq 0x24150
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x352bc(%rip), %rsi     # 0x3b430
callq 0x24150
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq %rax, -0x298(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6a10
movq %rax, %rcx
movq -0x298(%rbp), %rax
cmpq %rcx, %rax
jae 0x65de
callq 0x26480
movsd %xmm0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq 0x34e00(%rip), %rdi     # 0x3afd0
movq %rdi, -0x2a8(%rbp)
leaq 0x250e9(%rip), %rsi     # 0x2b2c7
callq 0x32a0
movq %rax, -0x2b0(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x6cf0
movq -0x2b0(%rbp), %rdi
movq %rax, %rsi
callq 0x3280
movq -0x2a8(%rbp), %rdi
callq 0x31c0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x6cf0
movq %rax, %rdi
callq 0x3120
movq %rax, %rsi
leaq -0x258(%rbp), %rdi
movl $0x4, %edx
callq 0x3550
movq -0x258(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x258(%rbp,%rax), %rdi
callq 0x3150
movb %al, -0x299(%rbp)
jmp 0x625d
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x626c
jmp 0x62f8
movq 0x34d7d(%rip), %rdi     # 0x3aff0
leaq 0x25055(%rip), %rsi     # 0x2b2cf
callq 0x32a0
movq %rax, -0x2b8(%rbp)
jmp 0x6288
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x6cf0
movq -0x2b8(%rbp), %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, -0x2c0(%rbp)
jmp 0x62ae
movq -0x2c0(%rbp), %rdi
leaq 0x255c4(%rip), %rsi     # 0x2b880
callq 0x32a0
movq %rax, -0x2c8(%rbp)
jmp 0x62ca
movq -0x2c8(%rbp), %rdi
movq 0x34cd8(%rip), %rsi     # 0x3afb0
callq 0x3300
jmp 0x62df
jmp 0x635c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
jmp 0x65cd
leaq -0x258(%rbp), %rdi
callq 0x25ee0
movq %rax, -0x2d0(%rbp)
jmp 0x630d
movq -0x2d0(%rbp), %rax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0x2d8(%rbp)
jmp 0x632a
movq -0x2d8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %eax
movl %eax, %edx
leaq -0x258(%rbp), %rdi
callq 0x33b0
jmp 0x634c
leaq -0x258(%rbp), %rdi
callq 0x30b0
jmp 0x635a
jmp 0x635c
cmpl $0x0, -0x4c(%rbp)
jbe 0x653e
callq 0x26480
movsd %xmm0, -0x2e0(%rbp)
jmp 0x6375
movsd -0x2e0(%rbp), %xmm0
movsd %xmm0, -0x270(%rbp)
movl $0x830, %edi            # imm = 0x830
callq 0x32c0
movq %rax, -0x2e8(%rbp)
jmp 0x6398
movq -0x2e8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
movl -0xc(%rbp), %r9d
movq %rsp, %rax
movl $0x1, (%rax)
callq 0x7e40
jmp 0x63c1
movq -0x2e8(%rbp), %rax
movq %rax, -0x278(%rbp)
callq 0x26480
movsd %xmm0, -0x2f0(%rbp)
jmp 0x63de
movsd -0x2f0(%rbp), %xmm0
movsd %xmm0, -0x280(%rbp)
movq 0x34bdb(%rip), %rdi     # 0x3afd0
leaq 0x24e39(%rip), %rsi     # 0x2b235
callq 0x32a0
movq %rax, -0x2f8(%rbp)
jmp 0x640a
movq -0x2f8(%rbp), %rdi
movsd -0x280(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
subsd %xmm1, %xmm0
callq 0x3560
movq %rax, -0x300(%rbp)
jmp 0x6430
movq -0x300(%rbp), %rdi
leaq 0x24db1(%rip), %rsi     # 0x2b1ef
callq 0x32a0
movq %rax, -0x308(%rbp)
jmp 0x644c
movq -0x308(%rbp), %rdi
movq 0x34b56(%rip), %rsi     # 0x3afb0
callq 0x3300
jmp 0x6461
movsd -0x280(%rbp), %xmm0
subsd -0x270(%rbp), %xmm0
movq -0x20(%rbp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
cmpq $0x0, -0x278(%rbp)
je 0x650b
movq -0x278(%rbp), %rdi
callq 0x6cb0
movb %al, -0x309(%rbp)
jmp 0x649f
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x64ab
jmp 0x650b
movq -0x278(%rbp), %rax
movq %rax, -0x318(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x6cf0
movq -0x318(%rbp), %rdi
movq %rax, %rsi
callq 0x99e0
jmp 0x64d8
movl -0x4c(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x650b
movq -0x2e8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
movl $0x830, %esi            # imm = 0x830
callq 0x32d0
jmp 0x65cd
movq -0x278(%rbp), %rax
movq %rax, -0x320(%rbp)
cmpq $0x0, %rax
je 0x653c
movq -0x320(%rbp), %rdi
callq 0x98c0
movq -0x320(%rbp), %rdi
movl $0x830, %esi            # imm = 0x830
callq 0x32d0
jmp 0x65b3
movq 0x34aab(%rip), %rdi     # 0x3aff0
leaq 0x24d9d(%rip), %rsi     # 0x2b2e9
callq 0x32a0
movq %rax, -0x328(%rbp)
jmp 0x655a
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x6cf0
movq -0x328(%rbp), %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, -0x330(%rbp)
jmp 0x6580
movq -0x330(%rbp), %rdi
leaq 0x252f2(%rip), %rsi     # 0x2b880
callq 0x32a0
movq %rax, -0x338(%rbp)
jmp 0x659c
movq -0x338(%rbp), %rdi
movq 0x34a06(%rip), %rsi     # 0x3afb0
callq 0x3300
jmp 0x65b1
jmp 0x65b3
leaq -0x258(%rbp), %rdi
callq 0x30d0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x618a
leaq -0x258(%rbp), %rdi
callq 0x30d0
jmp 0x6706
callq 0x26480
movsd -0x28(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x288(%rbp)
movl -0x34(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd 0x24a04(%rip), %xmm1    # 0x2b008
divsd %xmm1, %xmm0
movsd %xmm0, -0x290(%rbp)
movq 0x349b9(%rip), %rdi     # 0x3afd0
leaq 0x24ce6(%rip), %rsi     # 0x2b304
callq 0x32a0
movq %rax, %rdi
movsd -0x290(%rbp), %xmm0
callq 0x3560
movq %rax, %rdi
leaq 0x24cd0(%rip), %rsi     # 0x2b30d
callq 0x32a0
movq %rax, %rdi
movsd -0x288(%rbp), %xmm0
callq 0x3560
movq %rax, %rdi
leaq 0x24bd1(%rip), %rsi     # 0x2b22d
callq 0x32a0
movq %rax, %rdi
movsd -0x290(%rbp), %xmm0
divsd -0x288(%rbp), %xmm0
callq 0x3560
movq %rax, %rdi
leaq 0x24bb5(%rip), %rsi     # 0x2b238
callq 0x32a0
movq %rax, %rdi
movq 0x3491e(%rip), %rsi     # 0x3afb0
callq 0x3300
movq 0x34932(%rip), %rdi     # 0x3afd0
leaq 0x24c77(%rip), %rsi     # 0x2b31c
callq 0x32a0
movq %rax, -0x340(%rbp)
callq 0x264a0
movq -0x340(%rbp), %rdi
movl %eax, %esi
callq 0x33f0
movq %rax, %rdi
leaq 0x24bcb(%rip), %rsi     # 0x2b299
callq 0x32a0
movq %rax, %rdi
movq 0x348d3(%rip), %rsi     # 0x3afb0
callq 0x3300
movq 0x348e7(%rip), %rdi     # 0x3afd0
movq 0x348c0(%rip), %rsi     # 0x3afb0
callq 0x3300
movsd -0x290(%rbp), %xmm0
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
movq -0x260(%rbp), %rdi
callq 0x3520
nopw %cs:(%rax,%rax)
nopl (%rax)