// File: /workspace/github_cpp_makefile_star2_O1/adamnovak[P]rlcsa/build_rlcsa.cpp
// Function: main
int
main(int argc, char** argv)
{
  try {
    std::cout << "RLCSA builder" << std::endl;
    if(argc < 2)
    {
      std::cout << "Usage: build_rlcsa base_name [threads]" << std::endl;
      return 1;
    }

    std::string base_name = argv[1];
    int threads = 1;
    if(argc > 2)
    {
      threads = std::max(threads, atoi(argv[2]));
      threads = std::min(threads, MAX_THREADS);
    }
    std::cout << "Base name: " << base_name << std::endl;
    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();
    std::ifstream input(base_name.c_str(), std::ios_base::binary);
    if(!input)
    {
      std::cerr << "Error opening input file " << base_name << "!" << std::endl;
      return 2;
    }
    usint size = fileSize(input);
    if(size <= 0)
    {
      std::cerr << "Error: Input file is empty!" << std::endl;
      input.close();
      return 3;
    }
    uchar* data = new uchar[size];
    input.read((char*)data, size);
    input.close();

    RLCSA rlcsa(data, size, parameters.get(RLCSA_BLOCK_SIZE), parameters.get(SAMPLE_RATE), threads, true);
    data = 0; // RLCSA constructor deleted the data!
    if(!(rlcsa.isOk())) { return 4; }
    rlcsa.printInfo();
    rlcsa.reportSize(true);
    rlcsa.writeTo(base_name);
    double total = readTimer() - start;

    double megabytes = size / (double)MEGABYTE;
    std::cout << "Indexed " << megabytes << " megabytes in " << total << " seconds (" << (megabytes / total) << " MB/s)." << std::endl;
    std::cout << "Memory: " << memoryUsage() << " kB" << std::endl;
    std::cout << std::endl;

    return 0;
  } catch(std::bad_alloc e) {
    std::cerr << "Uncaught exception in build_rlcsa!" << std::endl;
    throw e;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xad0, %rsp            # imm = 0xAD0
movq %rsi, %r14
movl %edi, %ebp
movq 0x1aa01(%rip), %rbx     # 0x1efc8
leaq 0x12ae3(%rip), %rsi     # 0x170b1
movl $0xd, %edx
movq %rbx, %rdi
callq 0x3290
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0x1a9cf(%rip), %rdi     # 0x1efc8
callq 0x3040
movq %rax, %rdi
callq 0x3180
cmpl $0x1, %ebp
jg 0x465b
movq 0x1a9b6(%rip), %rbx     # 0x1efc8
leaq 0x12aa6(%rip), %rsi     # 0x170bf
movl $0x26, %edx
movq %rbx, %rdi
callq 0x3290
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0x1a984(%rip), %rdi     # 0x1efc8
callq 0x3040
movl $0x1, %ebx
movq %rax, %rdi
callq 0x3180
jmp 0x4bc5
movq 0x8(%r14), %rsi
leaq 0x18(%rsp), %rdi
leaq 0x2a0(%rsp), %rdx
callq 0x4d04
movl $0x1, %ebx
cmpl $0x2, %ebp
je 0x469a
movq 0x10(%r14), %rdi
callq 0x3370
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
cmpl $0x40, %ecx
movl $0x40, %ebx
cmovll %ecx, %ebx
movq 0x1a927(%rip), %rdi     # 0x1efc8
leaq 0x12a3e(%rip), %rsi     # 0x170e6
movl $0xb, %edx
callq 0x3290
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x1a905(%rip), %rdi     # 0x1efc8
callq 0x3290
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq 0x1a8cf(%rip), %rdi     # 0x1efc8
leaq 0x129f2(%rip), %rsi     # 0x170f2
movl $0x9, %edx
callq 0x3290
movq 0x1a8b7(%rip), %r14     # 0x1efc8
movq %r14, %rdi
movl %ebx, %esi
callq 0x33f0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0x1a865(%rip), %rdi     # 0x1efc8
callq 0x3040
movq %rax, %rdi
callq 0x3180
leaq 0x1ab79(%rip), %rdx     # 0x1f2f0
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4d5d
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 0x1abc6(%rip), %rsi     # 0x1f370
leaq 0x48(%rsp), %rdi
callq 0x139da
leaq 0x1abdd(%rip), %rsi     # 0x1f398
leaq 0x48(%rsp), %rdi
callq 0x139da
leaq 0x1abf4(%rip), %rsi     # 0x1f3c0
leaq 0x48(%rsp), %rdi
callq 0x139da
leaq 0x1ac0b(%rip), %rsi     # 0x1f3e8
leaq 0x48(%rsp), %rdi
callq 0x139da
leaq 0x1ac22(%rip), %rsi     # 0x1f410
leaq 0x48(%rsp), %rdi
callq 0x139da
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x13cf6
leaq 0x48(%rsp), %rdi
callq 0x13dda
callq 0x1494f
movsd %xmm0, 0x40(%rsp)
movq 0x18(%rsp), %rsi
leaq 0x98(%rsp), %rdi
movl $0x4, %edx
callq 0x3450
movq 0x98(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xb8(%rsp,%rax)
je 0x48bf
movq 0x1a7a0(%rip), %rdi     # 0x1eff0
leaq 0x128a5(%rip), %rsi     # 0x170fc
movl $0x19, %edx
callq 0x3290
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x1a77e(%rip), %rdi     # 0x1eff0
callq 0x3290
movq %rax, %rbx
leaq 0x12e47(%rip), %rsi     # 0x176c8
movl $0x1, %edx
movq %rax, %rdi
callq 0x3290
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movl $0x2, %ebx
movq %rax, %rdi
callq 0x3180
jmp 0x4b72
leaq 0x98(%rsp), %rdi
callq 0x14554
movq %rax, %r14
testl %r14d, %r14d
je 0x4b1a
movl %r14d, %r12d
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x98(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x32c0
leaq 0x98(%rsp), %rdi
callq 0x3080
leaq 0x1aa63(%rip), %rsi     # 0x1f370
leaq 0x48(%rsp), %rdi
callq 0x139b2
movl %eax, %ebp
leaq 0x1aa78(%rip), %rsi     # 0x1f398
leaq 0x48(%rsp), %rdi
callq 0x139b2
movl $0x1, (%rsp)
leaq 0x2a0(%rsp), %rdi
movq %r15, %rsi
movl %r14d, %edx
movl %ebp, %ecx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x53e2
movl $0x4, %ebx
cmpb $0x0, 0x2a0(%rsp)
je 0x4b0b
leaq 0x2a0(%rsp), %rdi
callq 0x84d4
leaq 0x2a0(%rsp), %rdi
movl $0x1, %esi
callq 0x822c
leaq 0x2a0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x6502
callq 0x1494f
movsd %xmm0, 0x10(%rsp)
movq 0x1a626(%rip), %rdi     # 0x1efc8
leaq 0x12789(%rip), %rsi     # 0x17132
movl $0x8, %edx
callq 0x3290
movl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x12642(%rip), %xmm0    # 0x17008
movq 0x1a5fb(%rip), %rdi     # 0x1efc8
movsd %xmm0, 0x38(%rsp)
callq 0x33a0
movq %rax, %rbx
leaq 0x12759(%rip), %rsi     # 0x1713b
movl $0xe, %edx
movq %rax, %rdi
callq 0x3290
movsd 0x10(%rsp), %xmm0
subsd 0x40(%rsp), %xmm0
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x33a0
movq %rax, %rbx
leaq 0x12737(%rip), %rsi     # 0x1714a
movl $0xa, %edx
movq %rax, %rdi
callq 0x3290
movsd 0x38(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rbx, %rdi
callq 0x33a0
movq %rax, %rbx
leaq 0x12717(%rip), %rsi     # 0x17155
movl $0x7, %edx
movq %rax, %rdi
callq 0x3290
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq 0x1a54f(%rip), %rdi     # 0x1efc8
leaq 0x126dd(%rip), %rsi     # 0x1715d
movl $0x8, %edx
callq 0x3290
callq 0x14964
movl %eax, %esi
movq 0x1a530(%rip), %rbx     # 0x1efc8
movq %rbx, %rdi
callq 0x3170
movq %rax, %r14
leaq 0x126bc(%rip), %rsi     # 0x17166
movl $0x3, %edx
movq %rax, %rdi
callq 0x3290
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0x1a4cc(%rip), %rdi     # 0x1efc8
callq 0x3040
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x3180
leaq 0x2a0(%rsp), %rdi
callq 0x6444
jmp 0x4b72
movq 0x1a4cf(%rip), %rbx     # 0x1eff0
leaq 0x125ee(%rip), %rsi     # 0x17116
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x3290
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0x1a49d(%rip), %rdi     # 0x1eff0
callq 0x3040
movq %rax, %rdi
callq 0x3180
movl $0x3, %ebx
leaq 0x98(%rsp), %rdi
callq 0x3080
leaq 0x98(%rsp), %rdi
callq 0x3090
leaq 0x48(%rsp), %rdi
callq 0x4dc2
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4baa
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3250
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4bc5
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
movl %ebx, %eax
addq $0xad0, %rsp            # imm = 0xAD0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4be9
jmp 0x4be9
jmp 0x4bfe
jmp 0x4bfe
jmp 0x4c13
jmp 0x4c13
jmp 0x4c4e
jmp 0x4c46
jmp 0x4bfe
movq %rdx, %r14
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
callq 0x6444
jmp 0x4c04
movq %rdx, %r14
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x3090
jmp 0x4c19
movq %rdx, %r14
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4dc2
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c54
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0x4c54
movq %rdx, %r14
movq %rax, %rbx
jmp 0x4c6f
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c6f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
cmpl $0x1, %r14d
jne 0x4cf4
movq %rbx, %rdi
callq 0x32b0
movq 0x1a33c(%rip), %r14     # 0x1efc0
addq $0x10, %r14
movq %r14, 0x2a0(%rsp)
movq %rbx, %rdi
callq 0x30f0
movq 0x1a351(%rip), %rdi     # 0x1eff0
leaq 0x124c4(%rip), %rsi     # 0x1716a
movl $0x22, %edx
callq 0x3290
movq 0x1a339(%rip), %rdi     # 0x1eff0
callq 0x3110
movl $0x8, %edi
callq 0x3120
movq %r14, (%rax)
movq 0x1a300(%rip), %rsi     # 0x1efd0
movq 0x1a2d1(%rip), %rdx     # 0x1efa8
movq %rax, %rdi
callq 0x33e0
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
callq 0x3488
callq 0x33d0
movq %rbx, %rdi
callq 0x3430
movq %rax, %rdi
callq 0x4db7