// File: adamnovak[P]rlcsa/alphabet.cpp
// Function: CSA::Alphabet::initialize(unsigned int const*)
void
Alphabet::initialize(const usint* counts)
{
  if(counts == 0) { return; }

  this->size = 0; this->chars = 0;
  for(usint c = 0; c < CHARS; c++)
  {
    this->index_ranges[c] = ((counts[c] > 0 || this->size > 0) ?
                             pair_type(this->size, this->size + counts[c] - 1) :
                             EMPTY_PAIR);
    if(counts[c] > 0)
    {
      this->text_chars[this->chars] = c;
      this->chars++;
    }
    size += counts[c];
  }

  this->index_rate = std::max((this->size + CHARS - 1) / CHARS, (usint)1);
  usint current = 0;

  for(usint c = 0, i = 0; c < this->chars; c++)
  {
    pair_type range = this->index_ranges[this->text_chars[c]];
    while(current <= range.second)
    {
      this->index_pointers[i] = c;
      current += this->index_rate;
      i++;
    }
  }

  this->ok = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x16e54
jmp 0x17004
movq -0x48(%rbp), %rax
movl $0x0, 0x800(%rax)
movl $0x0, 0xc04(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x100, -0x14(%rbp)     # imm = 0x100
jae 0x16f47
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
ja 0x16e9a
movq -0x48(%rbp), %rax
cmpl $0x0, 0x800(%rax)
jbe 0x16ecd
movq -0x48(%rbp), %rax
movq %rax, %rsi
addq $0x800, %rsi            # imm = 0x800
movl 0x800(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
movl %eax, -0x20(%rbp)
leaq -0x1c(%rbp), %rdi
leaq -0x20(%rbp), %rdx
callq 0xea30
jmp 0x16ed8
movq 0x12870(%rip), %rax     # 0x29744
movq %rax, -0x1c(%rbp)
movq -0x48(%rbp), %rdi
movl -0x14(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x1c(%rbp), %rsi
callq 0xe630
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jbe 0x16f1f
movq -0x48(%rbp), %rax
movl -0x14(%rbp), %edx
movl 0xc04(%rax), %ecx
movl %edx, 0x804(%rax,%rcx,4)
movl 0xc04(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc04(%rax)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
addl 0x800(%rax), %ecx
movl %ecx, 0x800(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x16e73
movq -0x48(%rbp), %rax
movl 0x800(%rax), %eax
addl $0x100, %eax            # imm = 0x100
subl $0x1, %eax
shrl $0x8, %eax
movl %eax, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
leaq -0x24(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x6360
movq %rax, %rcx
movq -0x48(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, 0x1008(%rax)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x48(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0xc04(%rcx), %eax
jae 0x16ff9
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %ecx
movl 0x804(%rax,%rcx,4), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x3c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x38(%rbp), %eax
ja 0x16fec
movq -0x48(%rbp), %rax
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movl %edx, 0xc08(%rax,%rcx,4)
movl 0x1008(%rax), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x16fbc
jmp 0x16fee
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x16f97
movq -0x48(%rbp), %rax
movb $0x1, 0x100c(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)