// File: raemarks[P]SuffixTree/src/SuffixTree.cpp
// Function: suffixtree::Tree::findPathAndInsert(suffixtree::Node*, int, int, int)
Node *
Tree::findPathAndInsert(
	Node *node,
	int suffix,
	int beg,
	int len
	)
{
	/*
	printf("= findPathAndInsert: ");
	PrintSegment(beg, len);
	printf(" -> ");
	PrintNodeLabel(node);
	printf("\n");
	*/

	if (len == 0) {
		printf("len is zero. why?\n");
	}

	Node *child = getChildByLabelBeginning(node, input[beg]);

	// No child by that label
	if (child == nullptr) {
		Node *nchild = newNode(suffix, node, beg, len);
		addChildToNode(node, nchild);

		return nchild;
	}

	for (int i = 1; i < len; i++) {
		if (i >= child->len) {
			return findPathAndInsert(child, suffix, beg + i, len - i);
		}
		if (input[beg + i] != input[child->beg + i]) {
			// SPLIT
			Node *mid = breakEdge(child, i);
			//printf("= splitting: ");
			//PrintNodeLabel(mid);
			//printf("\n");
			return findPathAndInsert(mid, suffix, (beg + i), (len - i));
		}
	}

	panic("shouldnt actually happen");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x2b96
leaq 0x2475(%rip), %rdi      # 0x5004
movb $0x0, %al
callq 0x2030
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x40(%rax), %rdi
movslq -0x20(%rbp), %rsi
callq 0x2340
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movsbl (%rax), %edx
callq 0x2d10
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x2c03
movq -0x50(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x18(%rbp), %rdx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
callq 0x26b0
movq -0x50(%rbp), %rdi
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x2d90
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2cfb
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2cd7
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jl 0x2c49
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
addl -0x3c(%rbp), %ecx
movl -0x24(%rbp), %r8d
subl -0x3c(%rbp), %r8d
callq 0x2b60
movq %rax, -0x8(%rbp)
jmp 0x2cfb
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x20(%rbp), %eax
addl -0x3c(%rbp), %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x50(%rbp), %rax
movsbl (%rcx), %ecx
movl %ecx, -0x5c(%rbp)
movq 0x40(%rax), %rdi
movq -0x30(%rbp), %rax
movl 0x48(%rax), %eax
addl -0x3c(%rbp), %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movl -0x5c(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x2cc7
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x2ef0
movq -0x50(%rbp), %rdi
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
addl -0x3c(%rbp), %ecx
movl -0x24(%rbp), %r8d
subl -0x3c(%rbp), %r8d
callq 0x2b60
movq %rax, -0x8(%rbp)
jmp 0x2cfb
jmp 0x2cc9
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2c0a
leaq 0x2339(%rip), %rdi      # 0x5017
leaq 0x2352(%rip), %rsi      # 0x5037
movl $0x57, %edx
movb $0x0, %al
callq 0x2030
movl $0x1, %edi
callq 0x2210
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)