// File: IHorvalds[P]RomAna/./processing/InflexEngineCore.cpp
// Function: InflexEngine::insert(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, unsigned int, int&)
void InflexEngine::insert(int32_t ptr, std::string str, int32_t connect, uint32_t pos, int32_t& finalPtr) {
// Insert in InflexEngine "str", the position to be regarded is now pos. "connect" is the node which str should be connected with.
// If "str" itself is a rootWord, connect must be set on -1.
  // It is not <= because even with a diacritica we arrive at the end of str.
  if (str.size() == pos) {
    // to keep track of which words are root words, and which words are derived from root words, the "code" variable of
    // each InflexEngine node will use its 0th bit set to 1 if the word is a root word, and to 0 otherwise.
    // if the 0th bit is set to 1, then all other bits keep the frequency of the word in the input text
    // if the 0th bit is set to 0, then all other bits keep the index of its root word in the InflexEngine array
    // Note:
    // + 2 in decimal <=> + 10 in binary, ie. add 1 to all bits but the 0th
    // * 2 in decimal <> << 1 in binary, ie. make sure 0th bit stays 0
    uint32_t hideme = (connect == -1);

    // why the third condition? - question for Alex.
    // Alex: good question
    if (hideme && (staticTrieAccess(ptr)->code & 1)) {
      // daca intai a fost root, iar acum vrea din nou sa fie root.
      // Exemplu: abate are 2 liste in inflections.in
      return;
    }
    if (hideme && !(staticTrieAccess(ptr)->code & 1) && (staticTrieAccess(ptr)->code != 0)) {
      // daca intai a fost trimis ca derivat, iar acum imi cere sa-l fac root
      // ce fac? il las derivat, n-a zis sudo. adica nu mai modific nimic
      return;
    }
    if (!hideme && (staticTrieAccess(ptr)->code & 1)) {
      // daca intai a fost trimis ca root, si acum vrea derivat
      // ce fac? e bou, il las root, cum sa cedezi puterea? adica nu mai modific nimic
      return;
    }
    if (!hideme && (staticTrieAccess(ptr)->code >> 1) && static_cast<uint32_t>(connect) != (staticTrieAccess(ptr)->code >> 1)) {
      // daca deja e derivatul cuiva, si acum vrea sa devina derivatul altcuiva
      // stai cuminte la casa ta, ca nu e dupa tine aici. adica nu mai modific nimic
      return;
    }
    if (!hideme && (!(staticTrieAccess(connect)->code & 1))) {
      // daca vrea să devină derivat al unui derivat al unei rădăcini
      // nouă nu ne place birocrația și nici ierarhia. devino direct derivat al rădăcinii
      // ie. path-compression
      connect = staticTrieAccess(connect)->code >> 1;
    }
    staticTrieAccess(ptr)->code = (hideme) ? 1 : connect * 2;
    finalPtr = ptr;
    return;
  }
  int32_t encoding = (pos == str.size() - 1) ? 
                      romanian::encode(str[pos]) : 
                      romanian::diacriticEncode(str[pos], str[pos + 1]);
                      
  // If the coming edge does not yet exist, create it.
  // There are 2 cases: either we build up the InflexEngine or not.
  // In the latter, we can without any problems use the "configuration" of the current node.
  // But if we build up the InflexEngine, we do not have any configuration, so we only work with the pointers to the sons.
  if (((generationMode != Build) && (!bitOp::getBit(staticTrieAccess(ptr)->configuration, encoding))) || (!staticTrieAccess(ptr)->sons[encoding])) {
    // If the configuration is not at its fullest, we decide to expand the node.
    // Please note here that "configuration" in "Build"-mode has been already set to "fullBits", so we cannot get in the "updateMihailsJmenuri" 
    if (staticTrieAccess(ptr)->configuration != fullBits)
      updateMihailsJmenuri(ptr, encoding, ++bufferLastPtr);
    else
      staticTrieAccess(ptr)->sons[encoding] = ++bufferLastPtr;
    
    // Save the parent and also the encoding of the edge, which represents the index of the character in alphabet
    staticTrieAccess(bufferLastPtr)->parent = ptr;
  }
  
  // If encoding shows we reached a diacritica, move to the next byte
  insert(staticTrieAccess(ptr)->sons[staticTrieAccess(ptr)->configuration != fullBits ? 
        bitOp::orderOfBit(staticTrieAccess(ptr)->configuration, encoding) : 
        encoding],
        str,
        connect,
        pos + 1 + romanian::isDiacritic(str[pos]), finalPtr);
}

// Assembly:
subq $0xc8, %rsp
movq %rdx, 0x50(%rsp)
movq %rdi, %rax
movq 0x50(%rsp), %rdi
movq %rdi, 0x58(%rsp)
movq %rax, 0xc0(%rsp)
movl %esi, 0xbc(%rsp)
movq %rdi, 0xb0(%rsp)
movl %ecx, 0xac(%rsp)
movl %r8d, 0xa8(%rsp)
movq %r9, 0xa0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x60(%rsp)
callq 0x4260
movl 0xa8(%rsp), %ecx
cmpq %rcx, %rax
jne 0x5370
cmpl $-0x1, 0xac(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, 0x9c(%rsp)
je 0x521a
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x521a
jmp 0x55f6
cmpl $0x0, 0x9c(%rsp)
je 0x525a
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x525a
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
cmpl $0x0, (%rax)
je 0x525a
jmp 0x55f6
cmpl $0x0, 0x9c(%rsp)
jne 0x5284
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x5284
jmp 0x55f6
cmpl $0x0, 0x9c(%rsp)
jne 0x52d8
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
shrl %eax
cmpl $0x0, %eax
je 0x52d8
movq 0x60(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0xbc(%rsp), %esi
callq 0x4bd0
movq %rax, %rcx
movl 0x4c(%rsp), %eax
movl (%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x52d8
jmp 0x55f6
cmpl $0x0, 0x9c(%rsp)
jne 0x5319
movq 0x60(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x5319
movq 0x60(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x4bd0
movl (%rax), %eax
shrl %eax
movl %eax, 0xac(%rsp)
cmpl $0x0, 0x9c(%rsp)
je 0x532e
movl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x533b
movl 0xac(%rsp), %eax
shll %eax
movl %eax, 0x48(%rsp)
movq 0x60(%rsp), %rdi
movl 0x48(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl 0x44(%rsp), %ecx
movl %ecx, (%rax)
movl 0xbc(%rsp), %ecx
movq 0xa0(%rsp), %rax
movl %ecx, (%rax)
jmp 0x55f6
movq 0x58(%rsp), %rdi
movl 0xa8(%rsp), %eax
movq %rax, 0x38(%rsp)
callq 0x4260
movq %rax, %rcx
movq 0x38(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x53b8
movq 0x58(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, %esi
callq 0x46c0
movsbl (%rax), %edi
callq 0x5c80
movl %eax, 0x34(%rsp)
jmp 0x53fc
movq 0x58(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, %esi
callq 0x46c0
movq 0x58(%rsp), %rdi
movb (%rax), %al
movb %al, 0x33(%rsp)
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x46c0
movb 0x33(%rsp), %cl
movzbl %cl, %edi
movzbl (%rax), %esi
callq 0x5cd0
movl %eax, 0x34(%rsp)
movq 0x60(%rsp), %rax
movl 0x34(%rsp), %ecx
movl %ecx, 0x98(%rsp)
cmpl $0x2, (%rax)
je 0x5437
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl 0x8(%rax), %edi
movl 0x98(%rsp), %esi
callq 0x5c00
testb $0x1, %al
jne 0x5437
jmp 0x545e
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movq 0xc(%rax), %rax
movslq 0x98(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x54ef
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movq 0x60(%rsp), %rcx
movl 0x8(%rax), %eax
cmpl 0x24(%rcx), %eax
je 0x549f
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl 0x98(%rsp), %edx
movl 0x28(%rdi), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rdi)
callq 0x5970
jmp 0x54d0
movq 0x60(%rsp), %rdi
movl 0x28(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl %eax, 0x28(%rdi)
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl 0x2c(%rsp), %edx
movq 0xc(%rax), %rax
movslq 0x98(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rdi), %esi
callq 0x4bd0
movl 0x28(%rsp), %ecx
movl %ecx, 0x4(%rax)
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movq 0x60(%rsp), %rdi
movq 0xc(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0xbc(%rsp), %esi
callq 0x4bd0
movq 0x60(%rsp), %rcx
movl 0x8(%rax), %eax
cmpl 0x24(%rcx), %eax
je 0x554d
movq 0x60(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x4bd0
movl 0x8(%rax), %edi
movl 0x98(%rsp), %esi
callq 0x5c20
movl %eax, 0x1c(%rsp)
jmp 0x5558
movl 0x98(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x58(%rsp), %rsi
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x4170
movq 0x58(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, %esi
movl %esi, %eax
incl %eax
movl %eax, 0xc(%rsp)
callq 0x46c0
movq %rax, 0x10(%rsp)
jmp 0x55a6
movq 0x10(%rsp), %rax
movzbl (%rax), %edi
callq 0x5d60
movb %al, 0x3(%rsp)
jmp 0x55b9
movl 0x8(%rsp), %ecx
movl 0x4(%rsp), %esi
movq 0x60(%rsp), %rdi
movl 0xc(%rsp), %r8d
movb 0x3(%rsp), %al
movzbl %al, %eax
andl $0x1, %eax
addl %eax, %r8d
movq 0xa0(%rsp), %r9
leaq 0x78(%rsp), %rdx
callq 0x5170
jmp 0x55ec
leaq 0x78(%rsp), %rdi
callq 0x4200
addq $0xc8, %rsp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x4200
movq 0x70(%rsp), %rdi
callq 0x4620