// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpq $0x0, 0x8(%rsi)
je 0x4953
movl %edx, %r14d
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x2260
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4ab6
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
callq 0x2100
testb %bpl, %bpl
je 0x495a
xorl %eax, %eax
jmp 0x4a92
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x2260
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4b2a
movq %rax, %r15
movl %edx, %ebp
movq %r13, %rdi
callq 0x2100
cmpq $0x0, 0x8(%rbx)
je 0x49a2
movq %rbx, %r13
movl %ebp, %eax
movq 0x8(%r12), %rbx
cmpq %rax, %rbx
jne 0x49e9
movb $0x1, 0x20(%r15)
jmp 0x4a90
movl %r14d, 0xc(%rsp)
pushq $0x28
popq %rdi
callq 0x21d0
movq %rax, %r15
movq (%r12), %rax
movq %rax, 0x10(%rsp)
movb (%rax), %al
movb %al, (%r15)
andl $0x0, 0x24(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movups %xmm0, 0x11(%r15)
movq %r15, 0x8(%rbx)
movl (%rbx), %r14d
incl %r14d
movq %rbx, %r13
movl %r14d, (%rbx)
movq 0x8(%r12), %rbx
jmp 0x4a40
movl %r14d, 0xc(%rsp)
movb (%r15), %r14b
movq (%r12), %rcx
movq %rcx, 0x10(%rsp)
movb (%rcx,%rax), %r12b
cmpb %r12b, %r14b
jne 0x4a09
movl (%r13), %r14d
jmp 0x4a40
pushq $0x28
popq %rdi
callq 0x21d0
andl $0x0, 0x24(%rax)
movb %r12b, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x11(%rax)
cmpb %r14b, %r12b
jge 0x4a2e
movq %rax, 0x8(%r15)
jmp 0x4a32
movq %rax, 0x10(%r15)
movl (%r13), %r14d
incl %r14d
movl %r14d, (%r13)
movq %rax, %r15
incl %r14d
incl %ebp
pushq $0x28
popq %r12
movl %ebp, %ebp
cmpq %rbp, %rbx
jbe 0x4a83
movq %r12, %rdi
callq 0x21d0
movq 0x10(%rsp), %rcx
movb (%rcx,%rbp), %cl
movb %cl, (%rax)
andl $0x0, 0x24(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x11(%rax)
movups %xmm0, 0x8(%rax)
movq %rax, 0x18(%r15)
movl %r14d, (%r13)
incl %r14d
incl %ebp
movq %rax, %r15
jmp 0x4a49
movb $0x1, 0x20(%r15)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%r15)
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x22e0