// File: /workspace/github_cpp_makefile_star2_O2/jaxonwang[P]suffixtree/main.cc
// Function: SuffixTreeImpl::travel_down_and_extend(InternalNode*&, unsigned long, unsigned long, InternalNode*&, InternalNode*&, unsigned long&)
ExtensionType travel_down_and_extend(internal_node_ptr &node,
                                       const size_type _j, const size_type i,
                                       internal_node_ptr &stop_node,
                                       internal_node_ptr &suffix_link_node,
                                       size_type &travel_depth) {
    size_type j = _j;
    auto current_node = node;
    ExtensionType ret = ExtensionType::extendleaf;
    suffix_link_node = nullptr;
    travel_depth = 0;

    for (;;) {
      const CharT c = text[j];
      auto &edge = current_node->next_edge(c);
      if (!edge) { // node has no edge with label start with c
        new_leaf(current_node, j, leaf_end_pos);
        ret = ExtensionType::newleaf;
        break;
      }

      size_type remain_length = i - j + 1;
      size_type start_pos = edge->start_position();
      size_type edge_length = edge->length();
      if (remain_length <= edge_length) { // travel will end in this edge
        size_type possible_diverge_point = start_pos + remain_length - 1;
        if (text[possible_diverge_point] == text[i]) {
          break; // nothing to do, break and will return extendleaf
        } else {
          auto new_node =
              break_edge(current_node, c, possible_diverge_point - 1);
          new_leaf(new_node, i, leaf_end_pos);
          ret = ExtensionType::newnode;
          suffix_link_node = new_node;
          break;
        }
      }
      // skip this node and travel to next node
      j += edge_length;
      travel_depth += edge_length;
      auto maybe_internal =
          dynamic_cast<internal_node_ptr>(current_node->next_node(c));
      if (!maybe_internal) { // extend on leaf edge
        if (i - j + 1 != 1)
          throw("remain error");
        break;
      } else {
        current_node = maybe_internal; // go to next node
      }
    }
    stop_node = current_node;
    if (!suffix_link_node)
      suffix_link_node = stop_node;
    return ret;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, -0x38(%rbp)
movq %rcx, %r15
movq (%rsi), %r14
movq %r14, -0x48(%rbp)
movq %r9, -0x40(%rbp)
andq $0x0, (%r9)
movq 0x10(%rbp), %rax
andq $0x0, (%rax)
movq %rdx, %r13
movq %rcx, -0x58(%rbp)
movq %rdi, -0x50(%rbp)
movq (%rdi), %rax
movsbl (%rax,%r13), %ebx
movq %r14, %rdi
movl %ebx, %esi
callq 0x2ebe
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3d06
movq %rax, %r12
movl %ebx, -0x2c(%rbp)
subq %r13, %r15
leaq 0x1(%r15), %rax
movq %rax, -0x60(%rbp)
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %rbx
movq (%r12), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
cmpq %rax, -0x60(%rbp)
jbe 0x3d1e
addq %rax, %r13
movq 0x10(%rbp), %rcx
addq %rax, (%rcx)
movq %r14, %rdi
movl -0x2c(%rbp), %esi
callq 0x31c2
testq %rax, %rax
je 0x3cdd
movq %rax, %rdi
leaq 0x2f8a(%rip), %rsi      # 0x6c40
leaq 0x2fab(%rip), %rdx      # 0x6c68
xorl %ecx, %ecx
callq 0x2180
testq %rax, %rax
je 0x3cdd
movq %rax, %r14
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %rdi
jmp 0x3c46
cmpq -0x58(%rbp), %r13
je 0x3d35
pushq $0x8
popq %rdi
callq 0x20b0
leaq 0x39d(%rip), %rcx       # 0x408f
movq %rcx, (%rax)
movq 0x32b4(%rip), %rsi      # 0x6fb0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2250
movq -0x50(%rbp), %rdi
leaq 0x48(%rdi), %rcx
leaq -0x48(%rbp), %rsi
movq %r13, %rdx
callq 0x2f3e
xorl %eax, %eax
jmp 0x3d38
addq %r15, %rbx
movq -0x50(%rbp), %r15
movq (%r15), %rax
movb (%rax,%rbx), %dl
movq -0x58(%rbp), %r14
cmpb (%rax,%r14), %dl
jne 0x3d5f
pushq $0x2
popq %rax
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rcx
movq %rcx, (%rsi)
cmpq $0x0, (%rdx)
jne 0x3d50
movq %rcx, (%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decq %rbx
leaq -0x48(%rbp), %rsi
movq %r15, %rdi
movl -0x2c(%rbp), %edx
movq %rbx, %rcx
callq 0x302c
leaq -0x68(%rbp), %rbx
movq %rax, (%rbx)
leaq 0x48(%r15), %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2f3e
movq (%rbx), %rax
movq -0x40(%rbp), %rdx
movq %rax, (%rdx)
pushq $0x1
popq %rax
jmp 0x3d3c