// File: /workspace/github_cpp_makefile_star2_O3/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, -0x58(%rbp)
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %r15
movq 0x10(%rbp), %rcx
movq (%rsi), %rbx
movq %rbx, -0x38(%rbp)
xorl %eax, %eax
movq %r9, -0x40(%rbp)
movq %rax, (%r9)
movq %rax, (%rcx)
movq (%rdi), %rax
movb (%rax,%rdx), %al
movb %al, -0x29(%rbp)
movsbl %al, %esi
movq %rbx, %rdi
callq 0x302c
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3d4c
movq %rax, %r12
movq %r15, -0x50(%rbp)
movq %r13, -0x48(%rbp)
movq %r13, %r15
subq %r14, %r15
leaq 0x1(%r15), %rax
movq %rax, -0x60(%rbp)
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r13
movq (%r12), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
cmpq %rax, -0x60(%rbp)
jbe 0x3da2
addq %rax, %r14
movq 0x10(%rbp), %rcx
addq %rax, (%rcx)
movsbl -0x29(%rbp), %esi
movq %rbx, %rdi
callq 0x3374
testq %rax, %rax
je 0x3d6a
movq %rax, %rdi
leaq 0x3f3a(%rip), %rsi      # 0x7c40
leaq 0x3f5b(%rip), %rdx      # 0x7c68
xorl %ecx, %ecx
callq 0x21b0
testq %rax, %rax
je 0x3d6a
movq %rax, %rbx
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %r15
movq (%r15), %rax
movb (%rax,%r14), %al
movb %al, -0x29(%rbp)
movsbl %al, %esi
movq %rbx, %rdi
callq 0x302c
movq %rax, %r12
movq (%rax), %rdi
testq %rdi, %rdi
movq -0x48(%rbp), %r13
jne 0x3cb6
leaq 0x48(%r15), %rcx
leaq -0x38(%rbp), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x307c
movq -0x40(%rbp), %r14
xorl %eax, %eax
jmp 0x3dfd
movl $0x2, %eax
cmpq -0x48(%rbp), %r14
movq -0x40(%rbp), %r14
je 0x3dfd
movl $0x8, %edi
callq 0x20b0
leaq 0x132b(%rip), %rcx      # 0x50b9
movq %rcx, (%rax)
movq 0x4220(%rip), %rsi      # 0x7fb8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
addq %r15, %r13
movq -0x50(%rbp), %r15
movq (%r15), %rdx
movb (%rdx,%r13), %cl
movl $0x2, %eax
movq -0x48(%rbp), %r12
cmpb (%rdx,%r12), %cl
movq -0x40(%rbp), %r14
je 0x3dfd
decq %r13
movsbl -0x29(%rbp), %edx
leaq -0x38(%rbp), %rsi
movq %r15, %rdi
movq %r13, %rcx
callq 0x3172
leaq -0x68(%rbp), %rbx
movq %rax, (%rbx)
leaq 0x48(%r15), %rcx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x307c
movq (%rbx), %rax
movq %rax, (%r14)
movl $0x1, %eax
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq %rdx, (%rcx)
cmpq $0x0, (%r14)
jne 0x3e11
movq %rdx, (%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq