// File: migueeelvargas[P]Auto-Complete/DictionaryTrie.cpp
// Function: DictionaryTrie::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int)
bool DictionaryTrie::insert(std::string word, unsigned int freq)
{
  // Check for empty strings
  if (word.length() == 0)
    return false;

  // Checks if the word is in TST, if it is returns false
  if (find(word))
    return false;

  // If not in TST, traverse TST
  auto retVal = traverse(word);
  auto curr = retVal.first;

  // Check if TST is empty
  if (root == NULL) {
    root = new TrieNode(word[0]);
    curr = root;
    ++num_vertices;
  }

  // Sets flag if word was already in BST
  else if (retVal.second == word.length()) {
    retVal.first->isAWord = true;
    return true;
  }
  // Check if we need to add a new branch to TST
  else if (retVal.first->symbol != word[retVal.second]) {
    if (word[retVal.second] < curr->symbol) {
      curr->left = new TrieNode(word[retVal.second]);
      curr = curr->left;
      ++num_vertices;
    }
    else {
      curr->right = new TrieNode(word[retVal.second]);
      curr = curr->right;
      ++num_vertices;
    }
  }

  // Now, add rest of characters down the middle
  for (auto i = retVal.second + 1; i < word.length(); ++i) {
    curr->mid = new TrieNode(word[i]);
    curr = curr->mid;
    ++num_vertices;
  }

  // Sets word flag & word frequency
  curr->isAWord = true;
  curr->freq = freq;

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rsi, -0xb8(%rbp)
movq %rdi, %rax
movq -0xb8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0x3360
cmpq $0x0, %rax
jne 0x8f8d
movb $0x0, -0x1(%rbp)
jmp 0x9447
movq -0xb0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x30f0
movq -0xa8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x9460
movb %al, -0xb9(%rbp)
jmp 0x8fbf
leaq -0x40(%rbp), %rdi
callq 0x3170
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x8fd4
jmp 0x8ff7
movb $0x0, -0x1(%rbp)
jmp 0x9447
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3170
jmp 0x9455
movq -0xb0(%rbp), %rsi
leaq -0x80(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x30f0
movq -0xa8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x9590
movl %edx, -0xd4(%rbp)
movq %rax, -0xd0(%rbp)
jmp 0x9030
movl -0xd4(%rbp), %eax
movq -0xd0(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movl %eax, -0x88(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3170
movq -0xa8(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, -0x98(%rbp)
cmpq $0x0, 0x8(%rax)
jne 0x9140
movl $0x28, %edi
callq 0x3220
movq -0xb0(%rbp), %rdi
movq %rax, -0xf0(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0xe8(%rbp)
jmp 0x90ae
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rax
movsbl (%rax), %esi
movq %rsp, %rax
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x9f30
jmp 0x90db
movq -0xa8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rcx
movq %rcx, -0x98(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9333
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3170
jmp 0x9455
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x3230
jmp 0x9455
movq -0xb0(%rbp), %rdi
movl -0x58(%rbp), %eax
movq %rax, -0xf8(%rbp)
callq 0x3360
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jne 0x9176
movq -0x60(%rbp), %rax
movb $0x1, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x9447
movq -0xb0(%rbp), %rdi
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0xfc(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, %rcx
movl -0xfc(%rbp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x932f
movq -0xb0(%rbp), %rdi
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3430
movsbl (%rax), %eax
movq -0x98(%rbp), %rcx
movsbl (%rcx), %ecx
cmpl %ecx, %eax
jge 0x927f
movl $0x28, %edi
callq 0x3220
movq -0xb0(%rbp), %rdi
movq %rax, -0x110(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x108(%rbp)
jmp 0x91f9
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rax
movsbl (%rax), %esi
movq %rsp, %rax
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x9f30
jmp 0x9226
movq -0xa8(%rbp), %rax
movq -0x110(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq %rdx, 0x8(%rcx)
movq -0x98(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, -0x98(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x932d
movq -0x110(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x3230
jmp 0x9455
movl $0x28, %edi
callq 0x3220
movq -0xb0(%rbp), %rdi
movq %rax, -0x120(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x118(%rbp)
jmp 0x92aa
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rax
movsbl (%rax), %esi
movq %rsp, %rax
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x9f30
jmp 0x92d7
movq -0xa8(%rbp), %rax
movq -0x120(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq %rdx, 0x10(%rcx)
movq -0x98(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq %rcx, -0x98(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x932d
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x3230
jmp 0x9455
jmp 0x932f
jmp 0x9331
jmp 0x9333
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
movq -0xb0(%rbp), %rdi
movl -0x9c(%rbp), %eax
movq %rax, -0x128(%rbp)
callq 0x3360
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x942b
movl $0x28, %edi
callq 0x3220
movq -0xb0(%rbp), %rdi
movq %rax, -0x138(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x130(%rbp)
jmp 0x9399
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rax
movsbl (%rax), %esi
movq %rsp, %rax
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x9f30
jmp 0x93c6
movq -0xa8(%rbp), %rax
movq -0x138(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq %rdx, 0x18(%rcx)
movq -0x98(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x98(%rbp)
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x933f
movq -0x138(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x28, %esi
callq 0x3230
jmp 0x9455
movq -0x98(%rbp), %rax
movb $0x1, 0x20(%rax)
movl -0x1c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x24(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x33c0
nop