// File: 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
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x2, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x21c0
movb (%rax), %al
movb %al, -0x45(%rbp)
movq -0x40(%rbp), %rdi
movsbl -0x45(%rbp), %esi
callq 0x57b0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5850
testb $0x1, %al
jne 0x92cc
movq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq %rdi, %rcx
addq $0x48, %rcx
leaq -0x40(%rbp), %rsi
callq 0x5880
movl $0x0, -0x44(%rbp)
jmp 0x9478
movq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5a50
movq %rax, %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5a50
movq %rax, %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x68(%rbp), %rax
ja 0x93b8
movq -0x88(%rbp), %rdi
movq -0x60(%rbp), %rax
addq -0x58(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
callq 0x21c0
movq -0x88(%rbp), %rdi
movsbl (%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x20(%rbp), %rsi
callq 0x21c0
movq %rax, %rcx
movl -0x8c(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x9364
jmp 0x9478
movq -0x88(%rbp), %rdi
movb -0x45(%rbp), %al
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
leaq -0x40(%rbp), %rsi
movsbl %al, %edx
callq 0x5a70
movq -0x88(%rbp), %rdi
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rdx
movq %rdi, %rcx
addq $0x48, %rcx
leaq -0x78(%rbp), %rsi
callq 0x5880
movl $0x1, -0x44(%rbp)
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x9478
movq -0x68(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rcx
movq 0x10(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x40(%rbp), %rdi
movsbl -0x45(%rbp), %esi
callq 0x5d50
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x9413
movq -0x98(%rbp), %rdi
leaq 0x77de(%rip), %rsi      # 0x10bd8
leaq 0x77ff(%rip), %rdx      # 0x10c00
xorl %eax, %eax
movl %eax, %ecx
callq 0x21d0
movq %rax, -0xa0(%rbp)
jmp 0x941e
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x941e
movq -0xa0(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x946b
movq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
addq $0x1, %rax
cmpq $0x1, %rax
je 0x9469
movl $0x8, %edi
callq 0x20a0
movq %rax, %rdi
leaq 0xc39(%rip), %rax       # 0xa08f
movq %rax, (%rdi)
movq 0x7b50(%rip), %rsi      # 0x10fb0
xorl %eax, %eax
movl %eax, %edx
callq 0x2270
jmp 0x9478
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x9272
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x949b
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x44(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)