// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %r14
movq 0x8(%rdx), %rax
movl %r8d, %r15d
cmpq %r15, %rax
jne 0x3c92
movq %r14, %rdi
movl %esi, %r12d
callq 0x384c
movl (%rax), %eax
cmpl $-0x1, %ebx
movl %ebx, %r15d
je 0x3d56
testb $0x1, %al
jne 0x3eaa
cmpl $-0x1, %r15d
je 0x3e84
movq %r14, %rdi
movl %r12d, %esi
callq 0x384c
cmpl $0x2, (%rax)
jb 0x3c67
movq %r14, %rdi
movl %r12d, %esi
callq 0x384c
movl (%rax), %eax
shrl %eax
cmpl %r15d, %eax
jne 0x3eaa
movq %r14, %rdi
movl %r15d, %esi
callq 0x384c
testb $0x1, (%rax)
movl %r15d, %ebx
jne 0x3e89
movq %r14, %rdi
movl %r15d, %esi
callq 0x384c
movl (%rax), %ebx
shrl %ebx
jmp 0x3e89
movl %r8d, %r12d
movq %rdx, %r13
movl %esi, 0xc(%rsp)
movl %ebx, 0x1c(%rsp)
decq %rax
movq (%rdx), %rcx
movsbl (%rcx,%r15), %edi
cmpq %r15, %rax
jne 0x3cc5
callq 0x33a0
addl $-0x61, %eax
cmpl $0x1a, %eax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmovbl %eax, %ebx
jmp 0x3cd9
leal 0x1(%r12), %eax
movzbl (%rcx,%rax), %esi
movzbl %dil, %edi
callq 0x433a
movl %eax, %ebx
movq %rbp, 0x20(%rsp)
cmpl $0x2, (%r14)
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
movl 0xc(%rsp), %ebp
je 0x3d02
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movl 0x8(%rax), %eax
btl %ebx, %eax
jae 0x3d1f
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movq 0xc(%rax), %rax
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x3daf
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movl 0x8(%rax), %eax
movl %ebp, %ecx
movl 0x28(%r14), %ebp
incl %ebp
cmpl 0x24(%r14), %eax
movl %ebp, 0x28(%r14)
movq %r14, %rdi
movl %ecx, %esi
jne 0x3d93
callq 0x384c
movq 0xc(%rax), %rax
movq 0x10(%rsp), %rcx
movl %ebp, (%rax,%rcx,4)
jmp 0x3d9c
testb $0x1, %al
jne 0x3eaa
movq %r14, %rdi
movl %r12d, %r13d
movl %r12d, %esi
callq 0x384c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb $0x1, (%rax)
jne 0x3e89
movq %r14, %rdi
movl %r13d, %esi
callq 0x384c
cmpl $0x0, (%rax)
jne 0x3eaa
jmp 0x3e89
movl %ebx, %edx
movl %ebp, %ecx
callq 0x4158
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x384c
movl 0xc(%rsp), %ebp
movl %ebp, 0x4(%rax)
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movq 0xc(%rax), %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movl 0x8(%rax), %eax
cmpl 0x24(%r14), %eax
je 0x3deb
movq %r14, %rdi
movl %ebp, %esi
callq 0x384c
movl 0x8(%rax), %edi
movl %ebx, %esi
callq 0x42d5
movl %eax, %ebx
movl %ebx, %eax
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %ebx
leaq 0x38(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0xb0ae
incl %r12d
movq (%r13), %rax
movb (%rax,%r15), %al
movl $0x1, %esi
cmpb $-0x3c, %al
movl 0x1c(%rsp), %ecx
je 0x3e51
xorl %esi, %esi
leaq 0x820c(%rip), %rdi      # 0xc040
movq %rsi, %rdx
cmpq $0x9, %rsi
je 0x3e47
leaq 0x1(%rdx), %rsi
cmpb %al, 0x2(%rdi,%rdx,2)
jne 0x3e34
xorl %esi, %esi
cmpq $0x9, %rdx
setb %sil
addl %esi, %r12d
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %r8d
movq 0x20(%rsp), %r9
callq 0x3bf2
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3eaa
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x3eaa
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
addl %ebx, %ebx
cmpl $-0x1, %r15d
movq %rbp, %r15
movl $0x1, %ebp
cmovnel %ebx, %ebp
movq %r14, %rdi
movl %r12d, %esi
callq 0x384c
movl %ebp, (%rax)
movl %r12d, (%r15)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3ed3
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34b0
nop