// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, %r13
movl %esi, %r15d
movq %rdi, %r14
movq 0x8(%rdx), %rax
movl %r8d, %edx
cmpq %rdx, %rax
jne 0x3c8a
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl (%rax), %eax
cmpl $-0x1, %ebx
movl %ebx, %r12d
je 0x3d45
testb $0x1, %al
jne 0x3e86
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
cmpl $0x2, (%rax)
jb 0x3c5f
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl (%rax), %eax
shrl %eax
cmpl %r12d, %eax
jne 0x3e86
movq %r14, %rdi
movl %r12d, %esi
callq 0x383c
testb $0x1, (%rax)
movl %r12d, %ebx
jne 0x3d76
movq %r14, %rdi
movl %r12d, %esi
callq 0x383c
movl (%rax), %ebx
shrl %ebx
jmp 0x3d76
movl %r8d, %r12d
movl %ebx, 0xc(%rsp)
decq %rax
movq (%r13), %rcx
movsbl (%rcx,%rdx), %edi
movq %rdx, 0x18(%rsp)
cmpq %rdx, %rax
jne 0x3cbb
callq 0x33a0
addl $-0x61, %eax
cmpl $0x1a, %eax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmovbl %eax, %ebx
jmp 0x3ccf
leal 0x1(%r12), %eax
movzbl (%rcx,%rax), %esi
movzbl %dil, %edi
callq 0x4316
movl %eax, %ebx
movq %rbp, 0x20(%rsp)
cmpl $0x2, (%r14)
movslq %ebx, %rbp
je 0x3cf0
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl 0x8(%rax), %eax
btl %ebx, %eax
jae 0x3d09
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movq 0xc(%rax), %rax
cmpl $0x0, (%rax,%rbp,4)
jne 0x3db6
movq %rbp, 0x10(%rsp)
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
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 0x3d9d
callq 0x383c
movq 0xc(%rax), %rax
movq 0x10(%rsp), %rcx
movl %ebp, (%rax,%rcx,4)
jmp 0x3da6
testb $0x1, %al
jne 0x3e86
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb $0x1, (%rax)
jne 0x3d76
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
cmpl $0x0, (%rax)
jne 0x3e86
addl %ebx, %ebx
cmpl $-0x1, %r12d
movq %rbp, %r12
movl $0x1, %ebp
cmovnel %ebx, %ebp
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl %ebp, (%rax)
movl %r15d, (%r12)
jmp 0x3e86
movl %ebx, %edx
movl %ebp, %ecx
callq 0x4134
movl 0x28(%r14), %esi
movq %r14, %rdi
callq 0x383c
movl %r15d, 0x4(%rax)
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movq 0xc(%rax), %rbp
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl 0x8(%rax), %eax
cmpl 0x24(%r14), %eax
je 0x3df0
movq %r14, %rdi
movl %r15d, %esi
callq 0x383c
movl 0x8(%rax), %edi
movl %ebx, %esi
callq 0x42b1
movl %eax, %ebx
movl %ebx, %eax
movl (%rbp,%rax,4), %ebx
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0xafec
incl %r12d
movq (%r13), %rax
movq 0x18(%rsp), %rcx
movb (%rax,%rcx), %al
movl $0x1, %ecx
cmpb $-0x3c, %al
movq 0x20(%rsp), %r9
je 0x3e56
xorl %esi, %esi
leaq 0x8206(%rip), %rcx      # 0xc040
movq %rsi, %rdx
cmpq $0x9, %rsi
je 0x3e4d
leaq 0x1(%rdx), %rsi
cmpb %al, 0x2(%rcx,%rdx,2)
jne 0x3e3a
xorl %ecx, %ecx
cmpq $0x9, %rdx
setb %cl
addl %ecx, %r12d
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
movl 0xc(%rsp), %ecx
movl %r12d, %r8d
callq 0x3bee
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x3e86
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x3eaf
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34b0
nop