// File: adamnovak[P]rlcsa/sampler.cpp
// Function: CSA::WeightedSampler::calculateEdges(unsigned int*)
void
WeightedSampler::calculateEdges(weight_type* weights)
{
  // Build nodes only for text positions with a positive weight.
  this->nodes = 2;
  for(uint i = 1; i < this->size; i++) { if(weights[i] > 0) { this->nodes++; } }
  this->node_positions = new uint[this->nodes];
  this->node_positions[0] = 0; this->node_positions[this->nodes - 1] = this->size;

  this->edge_totals  = new sum_type[this->nodes];
  this->edge_totals[0] = (this->use_psi ? 0 : weights[0]);
  for(uint i = 1, j = 1; i < this->size; i++)
  {
    if(weights[i] > 0) { this->edge_totals[j] = weights[i]; this->node_positions[j] = i; j++; }
  }
  this->edge_totals[this->nodes - 1] = 0;
  delete[] weights;
  this->edge_weights = new sum_type[this->nodes];


  if(this->use_psi)
  {
    this->edge_weights[0] = 0;
    for(uint i = 1; i < this->nodes; i++)
    {
      this->edge_weights[i] = this->edge_weights[i - 1] + this->getDistance(i - 1, i) * this->edge_totals[i - 1];
      this->edge_totals[i] += this->edge_totals[i - 1];
    }
  }
  else
  {
    this->edge_weights[this->nodes - 1] = 0;
    for(uint i = this->nodes - 1; i > 0; i--)
    {
      this->edge_weights[i - 1] = this->edge_weights[i] + this->getDistance(i - 1, i) * this->edge_totals[i];
      this->edge_totals[i - 1] += this->edge_totals[i];
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x2, 0x48(%rax)
movl $0x1, -0x14(%rbp)
movq -0x30(%rbp), %rcx
movl -0x14(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x17739
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jbe 0x1772c
movq -0x30(%rbp), %rax
movl 0x48(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x48(%rax)
jmp 0x1772e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x17706
movq -0x30(%rbp), %rax
movl 0x48(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x50(%rax)
movq 0x50(%rax), %rcx
movl $0x0, (%rcx)
movl 0x8(%rax), %esi
movq 0x50(%rax), %rcx
movl 0x48(%rax), %edx
subl $0x1, %edx
movl %edx, %edx
movl %esi, (%rcx,%rdx,4)
movl 0x48(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
testb $0x1, 0x1c(%rax)
je 0x177c4
xorl %eax, %eax
movl %eax, -0x34(%rbp)
jmp 0x177cd
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movq 0x40(%rax), %rax
movl %ecx, (%rax)
movl $0x1, -0x18(%rbp)
movl $0x1, -0x1c(%rbp)
movq -0x30(%rbp), %rcx
movl -0x18(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x1783c
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jbe 0x1782f
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x18(%rbp), %edx
movl (%rcx,%rdx,4), %esi
movq 0x40(%rax), %rcx
movl -0x1c(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
movl -0x18(%rbp), %edx
movq 0x50(%rax), %rax
movl -0x1c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x17831
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x177e8
movq -0x30(%rbp), %rcx
movq 0x40(%rcx), %rax
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x1786a
movq -0x40(%rbp), %rdi
callq 0x33e0
movq -0x30(%rbp), %rax
movl 0x48(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
testb $0x1, 0x1c(%rax)
je 0x1793d
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x0, (%rax)
movl $0x1, -0x20(%rbp)
movq -0x30(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x48(%rcx), %eax
jae 0x17938
movq -0x30(%rbp), %rdi
movq 0x38(%rdi), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movl -0x20(%rbp), %esi
subl $0x1, %esi
movl -0x20(%rbp), %edx
callq 0x18dc0
movl -0x44(%rbp), %esi
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdx
movl -0x20(%rbp), %edi
subl $0x1, %edi
movl %edi, %edi
imull (%rdx,%rdi,4), %ecx
addl %ecx, %esi
movq 0x38(%rax), %rcx
movl -0x20(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
movq 0x40(%rax), %rcx
movl -0x20(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %edx
movq 0x40(%rax), %rax
movl -0x20(%rbp), %ecx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x178bc
jmp 0x179d0
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rcx
movl 0x48(%rax), %edx
subl $0x1, %edx
movl %edx, %edx
movl $0x0, (%rcx,%rdx,4)
movl 0x48(%rax), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jbe 0x179ce
movq -0x30(%rbp), %rdi
movq 0x38(%rdi), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
movl -0x24(%rbp), %esi
subl $0x1, %esi
movl -0x24(%rbp), %edx
callq 0x18dc0
movl -0x48(%rbp), %esi
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdx
movl -0x24(%rbp), %edi
imull (%rdx,%rdi,4), %ecx
addl %ecx, %esi
movq 0x38(%rax), %rcx
movl -0x24(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
movl %esi, (%rcx,%rdx,4)
movq 0x40(%rax), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %edx
movq 0x40(%rax), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1795d
jmp 0x179d0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)