// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbp
movl %ecx, %ebx
movl %esi, %r15d
movq %rdi, %r14
movq 0x8(%rdx), %rcx
movl %r8d, %r12d
cmpq %r12, %rcx
jne 0x3b2b
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl (%rax), %eax
cmpl $-0x1, %ebx
movl %ebx, %r12d
je 0x3bd8
testb $0x1, %al
jne 0x3cd2
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
cmpl $0x2, (%rax)
jb 0x3b00
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl (%rax), %eax
shrl %eax
cmpl %r12d, %eax
jne 0x3cd2
movq %r14, %rdi
movl %r12d, %esi
callq 0x3792
testb $0x1, (%rax)
movl %r12d, %ebx
jne 0x3c07
movq %r14, %rdi
movl %r12d, %esi
callq 0x3792
movl (%rax), %ebx
shrl %ebx
jmp 0x3c07
movq %rdx, %r13
movl %ebx, 0xc(%rsp)
decq %rcx
movq (%rdx), %rdx
movb (%rdx,%r12), %al
cmpq %r12, %rcx
movq %r8, 0x18(%rsp)
jne 0x3b50
movsbl %al, %edi
callq 0x4056
jmp 0x3b60
leal 0x1(%r8), %ecx
movzbl (%rdx,%rcx), %esi
movzbl %al, %edi
callq 0x406a
movl %eax, %ebx
movq %rbp, 0x20(%rsp)
cmpl $0x2, (%r14)
movslq %eax, %rbp
je 0x3b83
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl 0x8(%rax), %eax
btl %ebx, %eax
jae 0x3b9c
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movq 0xc(%rax), %rax
cmpl $0x0, (%rax,%rbp,4)
jne 0x3c45
movq %rbp, 0x10(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl 0x8(%rax), %eax
movl 0x28(%r14), %ebp
incl %ebp
cmpl 0x24(%r14), %eax
movl %ebp, 0x28(%r14)
movq %r14, %rdi
movl %r15d, %esi
jne 0x3c2c
callq 0x3792
movq 0xc(%rax), %rax
movq 0x10(%rsp), %rcx
movl %ebp, (%rax,%rcx,4)
jmp 0x3c35
testb $0x1, %al
jne 0x3cd2
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
pushq $-0x1
popq %rbx
testb $0x1, (%rax)
jne 0x3c07
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
cmpl $0x0, (%rax)
jne 0x3cd2
addl %ebx, %ebx
cmpl $-0x1, %r12d
movq %rbp, %r12
pushq $0x1
popq %rbp
cmovnel %ebx, %ebp
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl %ebp, (%rax)
movl %r15d, (%r12)
jmp 0x3cd2
movl %ebx, %edx
movl %ebp, %ecx
callq 0x3e96
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x3792
movl %r15d, 0x4(%rax)
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movq 0xc(%rax), %rbp
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl 0x8(%rax), %eax
cmpl 0x24(%r14), %eax
je 0x3c7f
movq %r14, %rdi
movl %r15d, %esi
callq 0x3792
movl 0x8(%rax), %edi
movl %ebx, %esi
callq 0x3ff5
movl %eax, %ebx
movl %ebx, %eax
movl (%rbp,%rax,4), %ebx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3350
movq (%r13), %rax
movzbl (%rax,%r12), %edi
callq 0x4097
movzbl %al, %eax
movq 0x18(%rsp), %rcx
leal (%rcx,%rax), %r8d
incl %r8d
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
movl 0xc(%rsp), %ecx
movq 0x20(%rsp), %r9
callq 0x3a92
leaq 0x28(%rsp), %rdi
callq 0x3120
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x3480