// File: raemarks[P]SuffixTree/src/SuffixTree.cpp
// Function: suffixtree::Tree::insertSuffix(suffixtree::Node*, int)
Node*
Tree::insertSuffix(
	Node *prevLeaf,
	int suffix
	)
{
	Node *u = prevLeaf->parent;

	nLeaves++;

	if (prevLeaf == root) {
		return findPathAndInsert(root, suffix, 0, input.length());

	} else if (u->suffixLink != nullptr) {
		//Case 1: u already existed at the start of iteration (suffix-1).
		Node *v = u->suffixLink;
		//u->stringDepth is equivalent to the length of alpha
		return findPathAndInsert(v, suffix, suffix + v->stringDepth,
			input.length() - suffix - v->stringDepth);

	} else if (u->parent != root) {
		//Case 2A: u was created during iteration (suffix-1), and u' is not root
		Node *uprime = u->parent;
		Node *vprime = uprime->suffixLink;
		//Beta is the edge label between u and uprime.
		int beta = u->beg;
		int betalen = u->len;

		Node *v = nodeHopToV(vprime, beta, betalen);
		u->suffixLink = v;

		return findPathAndInsert(v, suffix, suffix + v->stringDepth,
			input.length() - suffix - v->stringDepth);

	} else {
		//Case 2B: u was created during iteration (suffix-1), and u' is the root
		Node *uprime = u->parent; // root
		Node *vprime = uprime->suffixLink; // also the root
		int betaprime = u->beg + 1; // beta = x betaprime
		int betaprimelen = u->len - 1;

		Node *v = nodeHopToV(vprime, betaprime, betaprimelen);
		u->suffixLink = v;

		return findPathAndInsert(v, suffix, suffix + v->stringDepth,
			input.length() - suffix - v->stringDepth);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl 0x5c(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rcx)
movq -0x18(%rbp), %rax
cmpq 0x10(%rcx), %rax
jne 0x284b
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x88(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, -0x7c(%rbp)
movq 0x40(%rax), %rdi
callq 0x2290
movq -0x78(%rbp), %rdi
movq -0x88(%rbp), %rsi
movl -0x7c(%rbp), %edx
movl %eax, %r8d
xorl %ecx, %ecx
callq 0x2b60
movq %rax, -0x8(%rbp)
jmp 0x2a5b
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x28cd
movq -0x78(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, -0x90(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rdx
addl 0x54(%rdx), %ecx
movl %ecx, -0x8c(%rbp)
movq 0x40(%rax), %rdi
callq 0x2290
movq -0x78(%rbp), %rdi
movq -0x98(%rbp), %rsi
movl -0x90(%rbp), %edx
movl -0x8c(%rbp), %ecx
movslq -0x1c(%rbp), %r8
subq %r8, %rax
movq -0x30(%rbp), %r8
movslq 0x54(%r8), %r8
subq %r8, %rax
movl %eax, %r8d
callq 0x2b60
movq %rax, -0x8(%rbp)
jmp 0x2a5b
movq -0x78(%rbp), %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq 0x10(%rcx), %rax
je 0x299e
movq -0x78(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl 0x48(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rsi
movl -0x44(%rbp), %edx
movl -0x48(%rbp), %ecx
callq 0x3050
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x50(%rbp)
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq %rdx, 0x18(%rcx)
movq -0x50(%rbp), %rcx
movq %rcx, -0xa8(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, -0xa0(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x50(%rbp), %rdx
addl 0x54(%rdx), %ecx
movl %ecx, -0x9c(%rbp)
movq 0x40(%rax), %rdi
callq 0x2290
movq -0x78(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl -0xa0(%rbp), %edx
movl -0x9c(%rbp), %ecx
movslq -0x1c(%rbp), %r8
subq %r8, %rax
movq -0x50(%rbp), %r8
movslq 0x54(%r8), %r8
subq %r8, %rax
movl %eax, %r8d
callq 0x2b60
movq %rax, -0x8(%rbp)
jmp 0x2a5b
movq -0x78(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0x48(%rax), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rax
movl 0x4c(%rax), %eax
subl $0x1, %eax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rsi
movl -0x64(%rbp), %edx
movl -0x68(%rbp), %ecx
callq 0x3050
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, -0x70(%rbp)
movq -0x70(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq %rdx, 0x18(%rcx)
movq -0x70(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, -0xb0(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x70(%rbp), %rdx
addl 0x54(%rdx), %ecx
movl %ecx, -0xac(%rbp)
movq 0x40(%rax), %rdi
callq 0x2290
movq -0x78(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movl -0xb0(%rbp), %edx
movl -0xac(%rbp), %ecx
movslq -0x1c(%rbp), %r8
subq %r8, %rax
movq -0x70(%rbp), %r8
movslq 0x54(%r8), %r8
subq %r8, %rax
movl %eax, %r8d
callq 0x2b60
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)