// File: adamnovak[P]rlcsa/display_test.cpp
// Function: main
int main(int argc, char** argv)
{
  std::cout << "RLCSA display test" << std::endl;
  if(argc < 5)
  {
    std::cout << "Usage: display_test basename weights sequences length [random_seed]" << std::endl;
    return 1;
  }
  std::cout << std::endl;

  std::cout << "Base name: " << argv[1] << std::endl;
  std::cout << "Weights: " << argv[2] << std::endl; 

  usint sequences = std::max(atoi(argv[3]), 1);
  std::cout << "Sequences: " << sequences << std::endl;

  usint seq_length = std::max(atoi(argv[4]), 1);
  std::cout << "Sequence length: " << seq_length << std::endl;

  usint seed = 0xDEADBEEF;
  if(argc > 5)
  {
    seed = atoi(argv[5]);
  }
  std::cout << "Random seed: " << seed << std::endl; 
  std::cout << std::endl;

  RLCSA rlcsa(argv[1]);
  if(!rlcsa.isOk() || !rlcsa.supportsDisplay())
  {
    std::cerr << "Error: Display is not supported!" << std::endl;
    return 2;
  }
  rlcsa.printInfo();
  rlcsa.reportSize(true);
  usint size = rlcsa.getSize();


  // Read weights and determine positions.
  weight_type* weights = new weight_type[size];
  std::ifstream weight_file(argv[2], std::ios_base::binary);
  if(!weight_file)
  {
    std::cerr << "Error: Cannot open weight file!" << std::endl;
    delete[] weights;
    return 3;
  }
  weight_file.read((char*)weights, size * sizeof(weight_type));
  weight_file.close();
  srand(seed);
  usint* positions = generatePositions(weights, size, sequences);
  delete[] weights;
  std::cout << "Generated " << sequences << " positions." << std::endl;
  std::cout << std::endl;


  // Do & measure the actual work.
  uchar* buffer = new uchar[seq_length];
  double start = readTimer();
  for(usint i = 0; i < sequences; i++)
  {
    rlcsa.display(0, pair_type(positions[i], positions[i] + seq_length - 1), buffer);
  }
  double time = readTimer() - start;
  std::cout << "Total time: " << time << " seconds." << std::endl;
  std::cout << std::endl;


  delete[] buffer;
  delete[] positions;
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xba0, %rsp            # imm = 0xBA0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x323b0(%rip), %rdi     # 0x36fd0
leaq 0x23483(%rip), %rsi     # 0x280aa
callq 0x32b0
movq %rax, %rdi
movq 0x3237a(%rip), %rsi     # 0x36fb0
callq 0x3310
cmpl $0x5, -0x8(%rbp)
jge 0x4c6f
movq 0x32388(%rip), %rdi     # 0x36fd0
leaq 0x2346e(%rip), %rsi     # 0x280bd
callq 0x32b0
movq %rax, %rdi
movq 0x32352(%rip), %rsi     # 0x36fb0
callq 0x3310
movl $0x1, -0x4(%rbp)
jmp 0x536c
movq 0x3235a(%rip), %rdi     # 0x36fd0
movq 0x32333(%rip), %rsi     # 0x36fb0
callq 0x3310
movq 0x32347(%rip), %rdi     # 0x36fd0
leaq 0x23471(%rip), %rsi     # 0x28101
callq 0x32b0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x32b0
movq %rax, %rdi
movq 0x32301(%rip), %rsi     # 0x36fb0
callq 0x3310
movq 0x32315(%rip), %rdi     # 0x36fd0
leaq 0x2344b(%rip), %rsi     # 0x2810d
callq 0x32b0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x32b0
movq %rax, %rdi
movq 0x322cf(%rip), %rsi     # 0x36fb0
callq 0x3310
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x34b0
movl %eax, -0x18(%rbp)
movl $0x1, -0x1c(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x59a0
movl (%rax), %eax
movl %eax, -0x14(%rbp)
movq 0x322ba(%rip), %rdi     # 0x36fd0
leaq 0x233fa(%rip), %rsi     # 0x28117
callq 0x32b0
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x3400
movq %rax, %rdi
movq 0x32279(%rip), %rsi     # 0x36fb0
callq 0x3310
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x34b0
movl %eax, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x59a0
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movq 0x32264(%rip), %rdi     # 0x36fd0
leaq 0x233b0(%rip), %rsi     # 0x28123
callq 0x32b0
movq %rax, %rdi
movl -0x20(%rbp), %esi
callq 0x3400
movq %rax, %rdi
movq 0x32223(%rip), %rsi     # 0x36fb0
callq 0x3310
movl $0xdeadbeef, -0x2c(%rbp) # imm = 0xDEADBEEF
cmpl $0x5, -0x8(%rbp)
jle 0x4daf
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x34b0
movl %eax, -0x2c(%rbp)
movq 0x3221a(%rip), %rdi     # 0x36fd0
movq %rdi, -0xb00(%rbp)
leaq 0x23371(%rip), %rsi     # 0x28135
callq 0x32b0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x3400
movq %rax, %rdi
movq 0x321d2(%rip), %rsi     # 0x36fb0
movq %rsi, -0xaf8(%rbp)
callq 0x3310
movq -0xb00(%rbp), %rdi
movq -0xaf8(%rbp), %rsi
callq 0x3310
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xaf0(%rbp)
leaq -0x881(%rbp), %rdi
movq %rdi, -0xae8(%rbp)
callq 0x3540
movq -0xaf0(%rbp), %rsi
movq -0xae8(%rbp), %rdx
leaq -0x880(%rbp), %rdi
callq 0x56d0
jmp 0x4e3b
leaq -0x860(%rbp), %rdi
leaq -0x880(%rbp), %rsi
xorl %edx, %edx
callq 0x5d10
jmp 0x4e52
leaq -0x880(%rbp), %rdi
callq 0x35d8
leaq -0x881(%rbp), %rdi
callq 0x3330
leaq -0x860(%rbp), %rdi
callq 0x59e0
movb %al, -0xb01(%rbp)
jmp 0x4e7e
movb -0xb01(%rbp), %al
testb $0x1, %al
jne 0x4e8a
jmp 0x4eae
leaq -0x860(%rbp), %rdi
callq 0x5a00
movb %al, -0xb02(%rbp)
jmp 0x4e9e
movb -0xb02(%rbp), %al
testb $0x1, %al
jne 0x4f4f
jmp 0x4eae
movq 0x3213b(%rip), %rdi     # 0x36ff0
leaq 0x23287(%rip), %rsi     # 0x28143
callq 0x32b0
movq %rax, -0xb10(%rbp)
jmp 0x4eca
movq -0xb10(%rbp), %rdi
movq 0x320d8(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x4edf
movl $0x2, -0x4(%rbp)
movl $0x1, -0x898(%rbp)
jmp 0x5350
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x890(%rbp)
movl %eax, -0x894(%rbp)
jmp 0x4f27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x890(%rbp)
movl %eax, -0x894(%rbp)
leaq -0x880(%rbp), %rdi
callq 0x35d8
leaq -0x881(%rbp), %rdi
callq 0x3330
jmp 0x5378
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x890(%rbp)
movl %eax, -0x894(%rbp)
jmp 0x535e
leaq -0x860(%rbp), %rdi
callq 0xacf0
jmp 0x4f5d
leaq -0x860(%rbp), %rdi
movl $0x1, %esi
callq 0xaa80
jmp 0x4f70
leaq -0x860(%rbp), %rdi
callq 0x5a20
movl %eax, -0xb14(%rbp)
jmp 0x4f84
movl -0xb14(%rbp), %eax
movl %eax, -0x89c(%rbp)
movl -0x89c(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x3030
movq %rax, -0xb20(%rbp)
jmp 0x4faa
movq -0xb20(%rbp), %rax
movq %rax, -0x8a8(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0xab0(%rbp), %rdi
movl $0x4, %edx
callq 0x3560
jmp 0x4fd3
movq -0xab0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0xab0(%rbp,%rax), %rdi
callq 0x3160
movb %al, -0xb21(%rbp)
jmp 0x4ff3
movb -0xb21(%rbp), %al
testb $0x1, %al
jne 0x5002
jmp 0x508c
movq 0x31fe7(%rip), %rdi     # 0x36ff0
leaq 0x23154(%rip), %rsi     # 0x28164
callq 0x32b0
movq %rax, -0xb30(%rbp)
jmp 0x501e
movq -0xb30(%rbp), %rdi
movq 0x31f84(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x5033
movq -0x8a8(%rbp), %rax
movq %rax, -0xb38(%rbp)
cmpq $0x0, %rax
je 0x5053
movq -0xb38(%rbp), %rdi
callq 0x33e0
movl $0x3, -0x4(%rbp)
movl $0x1, -0x898(%rbp)
jmp 0x5344
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x890(%rbp)
movl %eax, -0x894(%rbp)
leaq -0xab0(%rbp), %rdi
callq 0x30d0
jmp 0x535e
movq -0x8a8(%rbp), %rsi
movl -0x89c(%rbp), %eax
movl %eax, %edx
shlq $0x2, %rdx
leaq -0xab0(%rbp), %rdi
callq 0x33c0
jmp 0x50ad
leaq -0xab0(%rbp), %rdi
callq 0x30b0
jmp 0x50bb
movl -0x2c(%rbp), %edi
callq 0x3290
movq -0x8a8(%rbp), %rdi
movl -0x89c(%rbp), %esi
movl -0x14(%rbp), %edx
callq 0x5390
movq %rax, -0xb40(%rbp)
jmp 0x50e1
movq -0xb40(%rbp), %rax
movq %rax, -0xab8(%rbp)
movq -0x8a8(%rbp), %rax
movq %rax, -0xb48(%rbp)
cmpq $0x0, %rax
je 0x510f
movq -0xb48(%rbp), %rdi
callq 0x33e0
movq 0x31eba(%rip), %rdi     # 0x36fd0
leaq 0x23067(%rip), %rsi     # 0x28184
callq 0x32b0
movq %rax, -0xb50(%rbp)
jmp 0x512b
movq -0xb50(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3400
movq %rax, -0xb58(%rbp)
jmp 0x5143
movq -0xb58(%rbp), %rdi
leaq 0x2303e(%rip), %rsi     # 0x2818f
callq 0x32b0
movq %rax, -0xb60(%rbp)
jmp 0x515f
movq -0xb60(%rbp), %rdi
movq 0x31e43(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x5174
movq 0x31e55(%rip), %rdi     # 0x36fd0
movq 0x31e2e(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x5189
movl -0x20(%rbp), %eax
movl %eax, %edi
callq 0x3030
movq %rax, -0xb68(%rbp)
jmp 0x519c
movq -0xb68(%rbp), %rax
movq %rax, -0xac0(%rbp)
callq 0x23180
movsd %xmm0, -0xb70(%rbp)
jmp 0x51b9
movsd -0xb70(%rbp), %xmm0
movsd %xmm0, -0xac8(%rbp)
movl $0x0, -0xacc(%rbp)
movl -0xacc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x5249
movq -0xab8(%rbp), %rax
movl -0xacc(%rbp), %ecx
leaq (%rax,%rcx,4), %rsi
movl (%rax,%rcx,4), %edx
movl -0x20(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
movl %eax, -0xad8(%rbp)
leaq -0xad4(%rbp), %rdi
leaq -0xad8(%rbp), %rdx
callq 0x5a40
jmp 0x5218
movq -0xac0(%rbp), %rcx
movq -0xad4(%rbp), %rdx
leaq -0x860(%rbp), %rdi
xorl %esi, %esi
callq 0x9a80
jmp 0x5236
jmp 0x5238
movl -0xacc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xacc(%rbp)
jmp 0x51d3
callq 0x23180
movsd %xmm0, -0xb78(%rbp)
jmp 0x5258
movsd -0xb78(%rbp), %xmm0
movsd -0xac8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xae0(%rbp)
movq 0x31d55(%rip), %rdi     # 0x36fd0
leaq 0x22f19(%rip), %rsi     # 0x2819b
callq 0x32b0
movq %rax, -0xb80(%rbp)
jmp 0x5290
movq -0xb80(%rbp), %rdi
movsd -0xae0(%rbp), %xmm0
callq 0x3570
movq %rax, -0xb88(%rbp)
jmp 0x52ad
movq -0xb88(%rbp), %rdi
leaq 0x22eed(%rip), %rsi     # 0x281a8
callq 0x32b0
movq %rax, -0xb90(%rbp)
jmp 0x52c9
movq -0xb90(%rbp), %rdi
movq 0x31cd9(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x52de
movq 0x31ceb(%rip), %rdi     # 0x36fd0
movq 0x31cc4(%rip), %rsi     # 0x36fb0
callq 0x3310
jmp 0x52f3
movq -0xac0(%rbp), %rax
movq %rax, -0xb98(%rbp)
cmpq $0x0, %rax
je 0x5313
movq -0xb98(%rbp), %rdi
callq 0x33e0
movq -0xab8(%rbp), %rax
movq %rax, -0xba0(%rbp)
cmpq $0x0, %rax
je 0x5333
movq -0xba0(%rbp), %rdi
callq 0x33e0
movl $0x0, -0x4(%rbp)
movl $0x1, -0x898(%rbp)
leaq -0xab0(%rbp), %rdi
callq 0x30d0
leaq -0x860(%rbp), %rdi
callq 0x7e90
jmp 0x536c
leaq -0x860(%rbp), %rdi
callq 0x7e90
jmp 0x5378
movl -0x4(%rbp), %eax
addq $0xba0, %rsp            # imm = 0xBA0
popq %rbp
retq
movq -0x890(%rbp), %rdi
callq 0x3530
nopw %cs:(%rax,%rax)