// File: jaxonwang[P]suffixtree/main.cc
// Function: SuffixTreeImpl::build()
void build() { // S[j,i)
    internal_node_ptr longest_node = root;
    internal_node_ptr node_to_start = longest_node;

    size_type j_start_with = 0;
    size_type start_depth = 0;

    for (size_type i = 0; i <= textlen; i++) {
      leaf_end_pos = i;
      internal_node_ptr node_to_update_suffix_link = nullptr;

      // find the depth of the longest_node

      for (size_type j = j_start_with; j <= i; j++) {

        internal_node_ptr stop_node;
        internal_node_ptr suffix_link_node;
        size_type travel_depth;
        auto ret =
            travel_down_and_extend(node_to_start, j + start_depth, i, stop_node,
                                   suffix_link_node, travel_depth);

        if (node_to_update_suffix_link) {
          // set suffix link
          node_to_update_suffix_link->set_suffix_link(suffix_link_node);
        }

        if (ret == ExtensionType::newnode) { // need to specify suffix link must
                                             // be updated
          node_to_update_suffix_link = suffix_link_node;
        } else {
          node_to_update_suffix_link = nullptr;
        }

        if (ret != ExtensionType::extendleaf) { // new leaf is created
          j_start_with = j + 1;
          node_to_start = stop_node->get_suffix_link(); // next to start
          start_depth += (stop_node == root) ? 0 : travel_depth - 1;
        } else { // no need for the future extend , since they must be extesion
                 // type 3
          break;
        }
      }
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq 0x20(%rcx), %rax
ja 0x2dba
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x2da7
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rdx
addq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x18(%rbp), %rsi
leaq -0x48(%rbp), %r8
leaq -0x50(%rbp), %r9
leaq -0x58(%rbp), %rax
movq %rax, (%rsp)
callq 0x9210
movl %eax, -0x5c(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x2d2b
movq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x32b0
cmpl $0x1, -0x5c(%rbp)
jne 0x2d3b
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x2d43
movq $0x0, -0x38(%rbp)
cmpl $0x2, -0x5c(%rbp)
je 0x2d92
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x48(%rbp), %rdi
callq 0x94b0
movq -0x68(%rbp), %rcx
movq %rax, -0x18(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x28(%rcx), %rax
jne 0x2d78
xorl %eax, %eax
movq %rax, -0x70(%rbp)
jmp 0x2d84
movq -0x58(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x2d94
jmp 0x2da7
jmp 0x2d96
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2cdd
jmp 0x2da9
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2caf
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)