// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movl %edi, %ebp
movq 0x190c1(%rip), %rdi     # 0x1cfc8
leaq 0xf1a8(%rip), %rsi      # 0x130b6
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
cmpl $0x1, %ebp
jg 0x3f43
movq 0x190a1(%rip), %rdi     # 0x1cfc8
leaq 0xf196(%rip), %rsi      # 0x130c4
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
pushq $0x1
popq %rbp
jmp 0x42d9
movq 0x8(%rbx), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x2a0(%rsp), %rdx
callq 0x4414
cmpl $0x2, %ebp
jne 0x3f63
pushq $0x1
popq %rbx
jmp 0x3f7e
movq 0x10(%rbx), %rdi
callq 0x3350
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
cmpl $0x40, %ecx
pushq $0x40
popq %rbx
cmovll %ecx, %ebx
movq 0x19043(%rip), %rdi     # 0x1cfc8
leaq 0xf15f(%rip), %rsi      # 0x130eb
callq 0x31f0
leaq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x31e0
movq %rax, %rdi
callq 0x30f0
movq 0x1901b(%rip), %rdi     # 0x1cfc8
leaq 0xf143(%rip), %rsi      # 0x130f7
callq 0x31f0
movq %rax, %rdi
movl %ebx, %esi
callq 0x33d0
movq %rax, %rdi
callq 0x30f0
movq 0x18ff6(%rip), %rdi     # 0x1cfc8
callq 0x30f0
leaq 0x19302(%rip), %rdx     # 0x1d2e0
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x44de
leaq 0x30(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x19352(%rip), %rsi     # 0x1d360
leaq 0x28(%rsp), %rdi
callq 0xfe04
leaq 0x19369(%rip), %rsi     # 0x1d388
leaq 0x28(%rsp), %rdi
callq 0xfe04
leaq 0x19380(%rip), %rsi     # 0x1d3b0
leaq 0x28(%rsp), %rdi
callq 0xfe04
leaq 0x19397(%rip), %rsi     # 0x1d3d8
leaq 0x28(%rsp), %rdi
callq 0xfe04
leaq 0x193ae(%rip), %rsi     # 0x1d400
leaq 0x28(%rsp), %rdi
callq 0xfe04
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1004c
leaq 0x28(%rsp), %rdi
callq 0x100fa
callq 0x10aa4
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rsi
leaq 0x98(%rsp), %rdi
pushq $0x4
popq %rbp
movl %ebp, %edx
callq 0x3420
movq 0x98(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xb8(%rsp,%rax)
je 0x40ec
movq 0x18f3c(%rip), %rdi     # 0x1cff0
leaq 0xf046(%rip), %rsi      # 0x13101
callq 0x31f0
leaq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x31e0
leaq 0xf5f4(%rip), %rsi      # 0x136c8
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
pushq $0x2
popq %rbp
jmp 0x42ae
leaq 0x98(%rsp), %rdi
callq 0x107a0
movq %rax, %r14
testl %r14d, %r14d
je 0x42eb
movl %r14d, %r12d
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
leaq 0x98(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x3280
leaq 0x98(%rsp), %rdi
callq 0x3080
leaq 0x19226(%rip), %rsi     # 0x1d360
leaq 0x28(%rsp), %rdi
callq 0xfdf2
movl %eax, %r12d
leaq 0x1923a(%rip), %rsi     # 0x1d388
leaq 0x28(%rsp), %rdi
callq 0xfdf2
movl $0x1, (%rsp)
leaq 0x2a0(%rsp), %rdi
movq %r15, %rsi
movl %r14d, %edx
movl %r12d, %ecx
movl %eax, %r8d
movl %ebx, %r9d
callq 0x4a1a
cmpb $0x0, 0x2a0(%rsp)
je 0x42a1
leaq 0x2a0(%rsp), %rdi
callq 0x7228
leaq 0x2a0(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x7064
leaq 0x2a0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5954
callq 0x10aa4
movsd %xmm0, 0x10(%rsp)
movq 0x18dfe(%rip), %rdi     # 0x1cfc8
leaq 0xef66(%rip), %rsi      # 0x13137
callq 0x31f0
movl %r14d, %ecx
cvtsi2sd %rcx, %xmm0
mulsd 0xee22(%rip), %xmm0     # 0x13008
movq %rax, %rdi
movsd %xmm0, 0x18(%rsp)
callq 0x3380
leaq 0xef45(%rip), %rsi      # 0x13140
movq %rax, %rdi
callq 0x31f0
movsd 0x10(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movq %rax, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x3380
leaq 0xef2b(%rip), %rsi      # 0x1314f
movq %rax, %rdi
callq 0x31f0
movsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rax, %rdi
callq 0x3380
leaq 0xef13(%rip), %rsi      # 0x1315a
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
movq 0x18d6a(%rip), %rdi     # 0x1cfc8
leaq 0xeefd(%rip), %rsi      # 0x13162
callq 0x31f0
movq %rax, %rbx
callq 0x10ab9
movl %eax, %esi
movq %rbx, %rdi
callq 0x3150
leaq 0xeee8(%rip), %rsi      # 0x1316b
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
xorl %ebp, %ebp
movq 0x18d2c(%rip), %rdi     # 0x1cfc8
callq 0x30f0
leaq 0x2a0(%rsp), %rdi
callq 0x589e
leaq 0x98(%rsp), %rdi
callq 0x3090
leaq 0x28(%rsp), %rdi
callq 0x4522
leaq 0x78(%rsp), %rdi
callq 0x3468
leaq 0x58(%rsp), %rdi
callq 0x3468
movl %ebp, %eax
addq $0xad0, %rsp            # imm = 0xAD0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x18cfe(%rip), %rdi     # 0x1cff0
leaq 0xee22(%rip), %rsi      # 0x1311b
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
leaq 0x98(%rsp), %rdi
callq 0x3080
pushq $0x3
jmp 0x40e6
jmp 0x432c
jmp 0x432c
jmp 0x4341
jmp 0x4341
jmp 0x4341
jmp 0x435e
jmp 0x435e
jmp 0x437a
jmp 0x4356
movq %rdx, %r14
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
callq 0x589e
jmp 0x4347
movq %rdx, %r14
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x3090
jmp 0x4364
movq %rdx, %r14
movq %rax, %rbx
jmp 0x438a
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4522
leaq 0x78(%rsp), %rdi
callq 0x3468
jmp 0x4380
movq %rdx, %r14
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3468
cmpl $0x1, %r14d
jne 0x4404
movq %rbx, %rdi
callq 0x3270
movq 0x18c21(%rip), %r14     # 0x1cfc0
addq $0x10, %r14
movq %r14, 0x2a0(%rsp)
movq %rbx, %rdi
callq 0x30d0
movq 0x18c36(%rip), %rdi     # 0x1cff0
leaq 0xedae(%rip), %rsi      # 0x1316f
callq 0x31f0
movq %rax, %rdi
callq 0x30f0
pushq $0x8
popq %rdi
callq 0x3100
movq %r14, (%rax)
movq 0x18bf0(%rip), %rsi     # 0x1cfd0
movq 0x18bc9(%rip), %rdx     # 0x1cfb0
movq %rax, %rdi
callq 0x33c0
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
callq 0x3470
callq 0x33b0
movq %rbx, %rdi
callq 0x3400
movq %rax, %rdi
callq 0x4516