// File: /workspace/github_cpp_makefile_star2_O1/adamnovak[P]rlcsa/merge_rlcsa.cpp
// Function: main
int
main(int argc, char** argv)
{
  std::cout << "RLCSA merger" << std::endl;
  if(argc < 3)
  {
    std::cout << "Usage: merge_rlcsa [-threads] original additional [additional2...]" << std::endl;
    return 1;
  }

  int original_parameter = 2, additional_parameter = 3, threads_parameter = 1;

  usint threads = 1;
  if(argc > threads_parameter && argv[threads_parameter][0] == '-')
  {
    threads = std::min(MAX_THREADS, std::max(atoi(argv[threads_parameter] + 1), 1));
  }
  else 
  {
    original_parameter--;
    additional_parameter--;
  }

  if(argc < additional_parameter)
  {
    std::cerr << "Error: specify the index to merge into and one index to add." << std::endl;
    return 1;
  }

  std::string base_name = argv[original_parameter];
  std::cout << "Index to update: " << base_name << std::endl;
  
  std::vector<std::string> additional_names;
  
  for(int i = additional_parameter; i < argc; i++)
  {
    std::string additional_name = argv[i];
    std::cout << "Index to add: " << additional_name << std::endl;
    additional_names.push_back(additional_name);
  }
  
  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;
  
  
  std::cout << "Merging the indexes" << std::endl;
  
  double mark = readTimer();
  std::cout << "Load: " << base_name; std::cout.flush();
  RLCSA* originalIndex = new RLCSA(base_name);
  RLCSABuilder builder(parameters.get(RLCSA_BLOCK_SIZE), parameters.get(SAMPLE_RATE), 0, threads, originalIndex);
  std::cout << " (" << (readTimer() - mark) << " seconds)" << std::endl;
  
  for(std::vector<std::string>::iterator i = additional_names.begin(); i != additional_names.end(); ++i)
  {
    mark = readTimer();
    std::cout << "Increment: " << *i; std::cout.flush();
    builder.insertFromFile(*i);
    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;
  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 $0xe8, %rsp
movq %rsi, %r14
movl %edi, %r15d
movq 0x1c566(%rip), %rbx     # 0x20fd0
leaq 0x13640(%rip), %rsi     # 0x180b1
movl $0xc, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
cmpl $0x2, %r15d
jg 0x4af0
leaq 0x1360c(%rip), %rsi     # 0x180be
movl $0x42, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movl $0x1, %eax
jmp 0x5314
movq 0x8(%r14), %rdi
cmpb $0x2d, (%rdi)
jne 0x4b26
incq %rdi
callq 0x3350
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
cmpl $0x40, %ecx
movl $0x40, %eax
cmovll %ecx, %eax
movl %eax, (%rsp)
movl $0x2, %eax
movl $0x3, %ebp
jmp 0x4b37
movl $0x1, %eax
movl $0x2, %ebp
movl $0x1, (%rsp)
movq (%r14,%rax,8), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x5460
movq 0x1c47c(%rip), %rdi     # 0x20fd0
leaq 0x135a6(%rip), %rsi     # 0x18101
movl $0x11, %edx
callq 0x3280
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x1c45a(%rip), %rdi     # 0x20fd0
callq 0x3280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpl %r15d, %ebp
jge 0x4c7a
movl %ebp, %ebp
movl %r15d, %ebx
leaq 0x80(%rsp), %r15
movq 0x1c3fb(%rip), %r13     # 0x20fd0
movq (%r14,%rbp,8), %rsi
movq %r15, %rdi
leaq 0x48(%rsp), %rdx
callq 0x5460
movl $0xe, %edx
movq %r13, %rdi
leaq 0x1351e(%rip), %rsi     # 0x18113
callq 0x3280
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq %r13, %rdi
callq 0x3280
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x54ba
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x4c6e
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3240
incq %rbp
cmpq %rbp, %rbx
jne 0x4bd5
movq 0x1c34f(%rip), %rdi     # 0x20fd0
leaq 0x1349a(%rip), %rsi     # 0x18122
movl $0x9, %edx
callq 0x3280
movl (%rsp), %ebp
movl %ebp, %esi
movq 0x1c332(%rip), %rbx     # 0x20fd0
movq %rbx, %rdi
callq 0x3160
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq 0x1c2e2(%rip), %rdi     # 0x20fd0
callq 0x3040
movq %rax, %rdi
callq 0x3170
leaq 0x1c5d6(%rip), %rdx     # 0x212d8
leaq 0xc8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x54f7
leaq 0x50(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x1c620(%rip), %rsi     # 0x21358
leaq 0x48(%rsp), %rdi
callq 0x151f2
leaq 0x1c637(%rip), %rsi     # 0x21380
leaq 0x48(%rsp), %rdi
callq 0x151f2
leaq 0x1c64e(%rip), %rsi     # 0x213a8
leaq 0x48(%rsp), %rdi
callq 0x151f2
leaq 0x1c665(%rip), %rsi     # 0x213d0
leaq 0x48(%rsp), %rdi
callq 0x151f2
leaq 0x1c67c(%rip), %rsi     # 0x213f8
leaq 0x48(%rsp), %rdi
callq 0x151f2
leaq 0x48(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x1550e
leaq 0x48(%rsp), %rdi
callq 0x155f2
callq 0x16167
movsd %xmm0, 0x78(%rsp)
movq 0x1c21c(%rip), %rbx     # 0x20fd0
leaq 0x13371(%rip), %rsi     # 0x1812c
movl $0x13, %edx
movq %rbx, %rdi
callq 0x3280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq 0x1c1ea(%rip), %rdi     # 0x20fd0
callq 0x3040
movq %rax, %rdi
callq 0x3170
callq 0x16167
movsd %xmm0, 0x8(%rsp)
movq 0x1c1cb(%rip), %rdi     # 0x20fd0
leaq 0x13334(%rip), %rsi     # 0x18140
movl $0x6, %edx
callq 0x3280
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x1c1a9(%rip), %rdi     # 0x20fd0
callq 0x3280
movq 0x1c19d(%rip), %rdi     # 0x20fd0
callq 0x3170
movl $0x830, %edi            # imm = 0x830
callq 0x3230
movq %rax, %r14
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x5994
leaq 0x1c4fd(%rip), %rsi     # 0x21358
leaq 0x48(%rsp), %rdi
callq 0x151ca
movl %eax, %ebx
leaq 0x1c512(%rip), %rsi     # 0x21380
leaq 0x48(%rsp), %rdi
callq 0x151ca
leaq 0x80(%rsp), %rdi
movl %ebx, %esi
movl %eax, %edx
xorl %ecx, %ecx
movl %ebp, %r8d
movq %r14, %r9
callq 0xb00c
movq 0x1c138(%rip), %rdi     # 0x20fd0
leaq 0x132dd(%rip), %rsi     # 0x1817c
movl $0x2, %edx
callq 0x3280
callq 0x16167
subsd 0x8(%rsp), %xmm0
movq 0x1c115(%rip), %rdi     # 0x20fd0
callq 0x3380
movq %rax, %rbx
leaq 0x1327d(%rip), %rsi     # 0x18147
movl $0x9, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq 0x30(%rsp), %r14
cmpq 0x38(%rsp), %r14
je 0x4fd7
movq 0x1c0bb(%rip), %r15     # 0x20fd0
leaq 0x80(%rsp), %r13
leaq 0x13258(%rip), %rbp     # 0x1817c
leaq 0x1321c(%rip), %rbx     # 0x18147
callq 0x16167
movsd %xmm0, (%rsp)
movl $0xb, %edx
movq %r15, %rdi
leaq 0x1320d(%rip), %rsi     # 0x18151
callq 0x3280
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x3280
movq %r15, %rdi
callq 0x3170
movq %r13, %rdi
movq %r14, %rsi
callq 0xb30c
movl $0x2, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3280
callq 0x16167
subsd (%rsp), %xmm0
movq %r15, %rdi
callq 0x3380
movq %rax, %r12
movl $0x9, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3280
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
addq $0x20, %r14
cmpq 0x38(%rsp), %r14
jne 0x4f2b
movq 0x1bff2(%rip), %rdi     # 0x20fd0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq 0x1bfd7(%rip), %rdi     # 0x20fd0
callq 0x3040
movq %rax, %rdi
callq 0x3170
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x53e3
movsd %xmm0, 0x8(%rsp)
callq 0x16167
movsd %xmm0, (%rsp)
movq 0x1bfa1(%rip), %rdi     # 0x20fd0
movsd 0x8(%rsp), %xmm0
callq 0x3380
movq %rax, %rbx
leaq 0x13119(%rip), %rsi     # 0x1815d
movl $0x16, %edx
movq %rax, %rdi
callq 0x3280
movsd (%rsp), %xmm0
subsd 0x78(%rsp), %xmm0
movq %rbx, %rdi
movsd %xmm0, (%rsp)
callq 0x3380
movq %rax, %rbx
leaq 0x13101(%rip), %rsi     # 0x18174
movl $0xa, %edx
movq %rax, %rdi
callq 0x3280
movsd 0x8(%rsp), %xmm0
divsd (%rsp), %xmm0
movq %rbx, %rdi
callq 0x3380
movq %rax, %rbx
leaq 0x130e2(%rip), %rsi     # 0x1817f
movl $0x7, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq 0x1bef8(%rip), %rdi     # 0x20fd0
leaq 0x130a8(%rip), %rsi     # 0x18187
movl $0xf, %edx
callq 0x3280
leaq 0x80(%rsp), %rdi
callq 0xba5c
movq 0x1bed3(%rip), %rdi     # 0x20fd0
callq 0x3380
movq %rax, %rbx
leaq 0x1308b(%rip), %rsi     # 0x18197
movl $0x8, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq 0x1be89(%rip), %rdi     # 0x20fd0
leaq 0x13052(%rip), %rsi     # 0x181a0
movl $0xf, %edx
callq 0x3280
leaq 0x80(%rsp), %rdi
callq 0xba62
movq 0x1be64(%rip), %rdi     # 0x20fd0
callq 0x3380
movq %rax, %rbx
leaq 0x1301c(%rip), %rsi     # 0x18197
movl $0x8, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq 0x1be1a(%rip), %rdi     # 0x20fd0
leaq 0x12ff3(%rip), %rsi     # 0x181b0
movl $0xf, %edx
callq 0x3280
leaq 0x80(%rsp), %rdi
callq 0xba68
movq 0x1bdf5(%rip), %rdi     # 0x20fd0
callq 0x3380
movq %rax, %rbx
leaq 0x12fad(%rip), %rsi     # 0x18197
movl $0x8, %edx
movq %rax, %rdi
callq 0x3280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq 0x1bdab(%rip), %rdi     # 0x20fd0
leaq 0x12f94(%rip), %rsi     # 0x181c0
movl $0xf, %edx
callq 0x3280
callq 0x1617c
movl %eax, %esi
movq 0x1bd8c(%rip), %rbx     # 0x20fd0
movq %rbx, %rdi
callq 0x3160
movq %rax, %r14
leaq 0x12f7a(%rip), %rsi     # 0x181d0
movl $0x3, %edx
movq %rax, %rdi
callq 0x3280
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3170
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3210
movsbl %al, %esi
movq 0x1bd28(%rip), %rdi     # 0x20fd0
callq 0x3040
movq %rax, %rdi
callq 0x3170
leaq 0x80(%rsp), %rdi
callq 0xb096
leaq 0x48(%rsp), %rdi
callq 0x55b8
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x52ed
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x30(%rsp), %rdi
callq 0x5552
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5312
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3240
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x830, %esi            # imm = 0x830
movq %r14, %rdi
callq 0x3240
jmp 0x538b
jmp 0x5349
jmp 0x5349
jmp 0x5352
jmp 0x5349
jmp 0x5349
jmp 0x5349
movq %rax, %rbx
jmp 0x53c0
movq %rax, %rbx
jmp 0x538b
jmp 0x5352
jmp 0x537b
movq %rax, %rbx
jmp 0x53b6
jmp 0x537b
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x53b6
movq 0x90(%rsp), %rsi
jmp 0x53ae
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xb096
leaq 0x48(%rsp), %rdi
callq 0x55b8
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53b6
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x3240
leaq 0x30(%rsp), %rdi
callq 0x5552
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x53db
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3240
movq %rbx, %rdi
callq 0x3410