// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, %r15
movq %r8, -0x70(%rbp)
movq %rcx, -0x60(%rbp)
movq %rdx, %r13
movq %rdi, %r14
movq 0x10(%rbp), %rcx
movq (%rsi), %rax
leaq -0x38(%rbp), %rdx
movq %rax, (%rdx)
xorl %eax, %eax
movq %rax, (%r9)
movq %rax, (%rcx)
leaq 0x48(%rdi), %rax
movq %rax, -0x58(%rbp)
movl $0x2, -0x2c(%rbp)
movq %r9, -0x40(%rbp)
movq %rdi, -0x50(%rbp)
movq (%r14), %rax
movq -0x38(%rbp), %rdi
movsbl (%rax,%r13), %ebx
movl %ebx, %esi
callq 0x303c
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3d6f
movq %rax, %r14
movq -0x60(%rbp), %r12
movq %r13, %rax
movq %r12, %r13
movq %rax, -0x48(%rbp)
subq %rax, %r13
leaq 0x1(%r13), %rax
movq %rax, -0x78(%rbp)
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r15
movq (%r14), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
cmpq %rax, -0x78(%rbp)
jbe 0x3d90
movq %rax, %r14
movq 0x10(%rbp), %rax
addq %r14, (%rax)
movq -0x38(%rbp), %rdi
movl %ebx, %esi
callq 0x3394
testq %rax, %rax
je 0x3df1
movq %rax, %rdi
leaq 0x3ee4(%rip), %rsi      # 0x7c40
leaq 0x3f05(%rip), %rdx      # 0x7c68
xorl %ecx, %ecx
callq 0x21b0
jmp 0x3df3
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
movq %r13, %rdx
movq -0x58(%rbp), %rcx
callq 0x308c
xorl %eax, %eax
movl $0x0, -0x2c(%rbp)
jmp 0x3e1b
addq %r13, %r15
movq -0x50(%rbp), %r14
movq (%r14), %rax
movb (%rax,%r15), %cl
cmpb (%rax,%r12), %cl
jne 0x3dac
xorl %eax, %eax
movq -0x40(%rbp), %r15
jmp 0x3deb
decq %r15
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
movl %ebx, %edx
movq %r15, %rcx
callq 0x318e
movq %rax, -0x68(%rbp)
movq %r14, %rdi
leaq -0x68(%rbp), %rsi
movq %r12, %rdx
movq -0x58(%rbp), %rcx
callq 0x308c
movq -0x68(%rbp), %rax
movq -0x40(%rbp), %r15
movq %rax, (%r15)
movl $0x1, -0x2c(%rbp)
xorl %eax, %eax
movq -0x48(%rbp), %r13
jmp 0x3e1b
xorl %eax, %eax
movq -0x40(%rbp), %r15
movq -0x48(%rbp), %r13
addq %r14, %r13
testq %rax, %rax
je 0x3e0f
movq %rax, -0x38(%rbp)
movb $0x1, %al
movq -0x50(%rbp), %r14
jmp 0x3e1b
cmpq -0x60(%rbp), %r13
movq -0x50(%rbp), %r14
jne 0x3e49
xorl %eax, %eax
testb %al, %al
jne 0x3ce5
movq -0x38(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, (%r15)
jne 0x3e37
movq %rax, (%r15)
movl -0x2c(%rbp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x20b0
leaq 0x125f(%rip), %rcx      # 0x50b9
movq %rcx, (%rax)
movq 0x4154(%rip), %rsi      # 0x7fb8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280