// File: jaxonwang[P]suffixtree/main.cc
// Function: SuffixTreeNaive::build()
void build() {
    for (size_t i = 0; i <= textlen; i++) {
      int j = i;
      internal_node_ptr current_node = root;

      while (true) { // will eventually create a leaf
        CharT current_char = text[j];

        auto &nextedge_ptr = current_node->next_edge(current_char);
        if (!nextedge_ptr) {
          new_leaf(current_node, j, textlen);
          goto end_insert_i;
        }

        size_t start = nextedge_ptr->start_position();
        size_t end = nextedge_ptr->end_position();
        for (; start <= end; start++, j++) {
          if (text[start] != text[j]) {
            auto new_node = break_edge(current_node, current_char, start - 1);
            new_leaf(new_node, j, textlen); // include the term symbol
            goto end_insert_i;
          }
        }
        current_node = dynamic_cast<internal_node_ptr>(
            current_node->next_node(current_char));
        if (!current_node)
          throw std::logic_error("Goes to Leaf!");
      }

    end_insert_i:;
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x10(%rbp)
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
cmpq 0x20(%rcx), %rax
ja 0x2b67
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
movl %ecx, -0x14(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x60(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x21c0
movb (%rax), %al
movb %al, -0x21(%rbp)
movq -0x20(%rbp), %rdi
movsbl -0x21(%rbp), %esi
callq 0x57b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x5850
testb $0x1, %al
jne 0x29f1
movq -0x60(%rbp), %rdi
movslq -0x14(%rbp), %rdx
movq %rdi, %rcx
addq $0x20, %rcx
leaq -0x20(%rbp), %rsi
callq 0x5880
jmp 0x2b54
movq -0x30(%rbp), %rdi
callq 0x5a50
movq %rax, %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x5a50
movq %rax, %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
ja 0x2ab0
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x21c0
movq -0x60(%rbp), %rdi
movsbl (%rax), %eax
movl %eax, -0x64(%rbp)
movslq -0x14(%rbp), %rsi
callq 0x21c0
movq %rax, %rcx
movl -0x64(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x2a94
movq -0x60(%rbp), %rdi
movb -0x21(%rbp), %al
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
leaq -0x20(%rbp), %rsi
movsbl %al, %edx
callq 0x5a70
movq -0x60(%rbp), %rdi
movq %rax, -0x48(%rbp)
movslq -0x14(%rbp), %rdx
movq %rdi, %rcx
addq $0x20, %rcx
leaq -0x48(%rbp), %rsi
callq 0x5880
jmp 0x2b54
jmp 0x2a96
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2a1d
movq -0x20(%rbp), %rdi
movsbl -0x21(%rbp), %esi
callq 0x5d50
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x2ae8
movq -0x70(%rbp), %rdi
leaq 0xe106(%rip), %rsi      # 0x10bd8
leaq 0xe127(%rip), %rdx      # 0x10c00
xorl %eax, %eax
movl %eax, %ecx
callq 0x21d0
movq %rax, -0x78(%rbp)
jmp 0x2af0
xorl %eax, %eax
movq %rax, -0x78(%rbp)
jmp 0x2af0
movq -0x78(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x2b4f
movl $0x10, %edi
callq 0x20a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
leaq 0x755f(%rip), %rsi      # 0xa079
callq 0x2220
jmp 0x2b21
movq -0x80(%rbp), %rdi
movq 0xe4b4(%rip), %rsi      # 0x10fe0
movq 0xe48d(%rip), %rdx      # 0x10fc0
callq 0x2270
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x2120
jmp 0x2b70
jmp 0x29a4
jmp 0x2b56
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x297f
addq $0x80, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x2280
nopl (%rax)