// File: /workspace/github_cpp_makefile_star2_O3/elarielcl[P]ArielsSequitur/Dictionary.cpp
// Function: Dictionary::putUnique(Node*)
void Dictionary::putUnique(Node* node) {
  Node** insertAt = this->get(node);
  if ((*insertAt) == NULL || (*insertAt) ->isGuard()){ // is there a better way?
    *insertAt = node;
    this->a++;
  }
  else {
    Node* m = *insertAt;
    if (m->digramOverlap(node)) return;

    if (m->prev->isGuard() && m->next->next->isGuard()) {

      this->grammar->grammarSize--;

      Rule* existentRule = m->prev->rule;
      existentRule->usage++;

      //Create one new node
      Node* one = new Node(existentRule, existentRule->symbol);

      // remove existent digrams in the index
      if (!node->prev->isGuard())
        this->remove(node->prev);
      if (!node->next->next->isGuard())
        this->remove(node->next);

      //Insert one
      node->prev->connect(one);
      one->connect(node->next->next);

      // put new digrams in the index
      if (!one->prev->isGuard())
        this->putUnique(one->prev);
      if (!one->next->isGuard())
        this->putUnique(one);

        //DELETING
        delete node->next;
        delete node;
        //DELETING

        // delete deprecatedOcurrences
        Node* dOcc = existentRule->guard->next; //deprecatedOccurrence
        while (!dOcc->isGuard()) {
          if(dOcc->symbol>=this->grammar->M) {
            dOcc->rule->usage--;
            if (dOcc->rule->usage <= 1) {
              this->grammar->numberOfRules--;
              this->grammar->grammarSize--;

              // remove existent digrams in the index
              if (!dOcc->prev->isGuard())
                this->remove(dOcc->prev);
              if (!dOcc->next->isGuard())
                this->remove(dOcc);

              // restructure pointers
              dOcc->prev->connect(dOcc->rule->guard->next);
              dOcc->rule->guard->prev->connect(dOcc->next);

              // put new digrams in the index
              if (!dOcc->next->isGuard())
                this->putUnique(dOcc->next->prev);
              if (!dOcc->prev->isGuard())
                this->putUnique(dOcc->prev);

              Node* next = dOcc->next;

              //DELETING
              delete dOcc->rule->guard;
              delete dOcc->rule;
              delete dOcc;
              //DELETING
              dOcc = next;
            }else dOcc = dOcc->next;
          }else dOcc = dOcc->next;
      }
    }else {
      this->grammar->numberOfRules++;
      int ruleName = (this->grammar->nextRuleName ++); // Another idea?
      Rule* newRule = new Rule(ruleName, this->grammar);
      newRule->usage = 2;

      //Create two new nodes
      Node* one = new Node(newRule, ruleName);
      Node* two = new Node(newRule, ruleName);

      // remove existent digrams in the index
      if (!node->prev->isGuard())
        this->remove(node->prev);
      if (!node->next->next->isGuard())
        this->remove(node->next);

      //Insert one
      node->prev->connect(one);
      one->connect(node->next->next);

      //Here put in the index the new digram (case not before)
      if (!one->prev->isGuard())
        this->put(one->prev); //Not recursive
      if (!one->next->isGuard())
        this->put(one); //Not recursive

      // remove existent digrams in the index
      if (!m->prev->isGuard())
        this->remove(m->prev);
      if (!m->next->next->isGuard())
        this->remove(m->next);

      //Insert two
      m->prev->connect(two);
      two->connect(m->next->next);

      //Here put in the index the new digrams (case not before)
      if (!two->prev->isGuard())
        this->put(two->prev); //Not recursive
      if (!two->next->isGuard())
        this->put(two); //Not recursive

      //Put the symbols in the new rule
      newRule->guard->connect(m);
      m->next->connect(newRule->guard);

      //DELETING
      delete node->next;
      delete node;
      //DELETING

      // delete deprecatedOcurrences
      Node* dOcc = newRule->guard->next; //deprecatedOccurrence
      while (!dOcc->isGuard()) {
        if(dOcc->symbol>=this->grammar->M) {
          dOcc->rule->usage--;
          if (dOcc->rule->usage <= 1) {
            this->grammar->grammarSize--;
            this->grammar->numberOfRules--;

            // remove existent digrams in the index
            if (!dOcc->prev->isGuard())
              this->remove(dOcc->prev);
            if (!dOcc->next->isGuard())
              this->remove(dOcc);

            // restructure pointers
            dOcc->prev->connect(dOcc->rule->guard->next);
            dOcc->rule->guard->prev->connect(dOcc->next);

            // put new digrams in the index
            if (!dOcc->next->isGuard())
              this->putUnique(dOcc->next->prev);
            if (!dOcc->prev->isGuard())
              this->putUnique(dOcc->prev);

            Node* next = dOcc->next;

            //DELETING
            delete dOcc->rule->guard;
            delete dOcc->rule;
            delete dOcc;
            //DELETING
            dOcc = next;
          }else dOcc = dOcc->next;
        }else dOcc = dOcc->next;
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
callq 0x1e26
movq %rax, %r15
movq (%rax), %rdi
testq %rdi, %rdi
je 0x18ea
callq 0x2574
testb %al, %al
je 0x18ff
movq %r14, (%r15)
incl 0x14(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x259e
testb %al, %al
jne 0x18f0
movq 0x10(%r15), %rdi
callq 0x2574
testb %al, %al
je 0x1b18
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x2574
testb %al, %al
je 0x1b18
movq (%rbx), %rax
decl 0x10(%rax)
movq 0x10(%r15), %rax
movq 0x18(%rax), %r15
incl 0x8(%r15)
movl $0x20, %edi
callq 0x10f0
movq %rax, %r12
movl 0xc(%r15), %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2568
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x197e
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2574
testb %al, %al
jne 0x199b
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x10(%r14), %rdi
movq %r12, %rsi
callq 0x25b4
movq 0x8(%r14), %rax
movq 0x8(%rax), %rsi
movq %r12, %rdi
callq 0x25b4
movq 0x10(%r12), %rdi
callq 0x2574
testb %al, %al
jne 0x19d2
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x18ba
movq 0x8(%r12), %rdi
callq 0x2574
testb %al, %al
jne 0x19eb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x18ba
movq 0x8(%r14), %rdi
movl $0x20, %esi
callq 0x1100
movl $0x20, %esi
movq %r14, %rdi
callq 0x1100
movq (%r15), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
callq 0x2574
testb %al, %al
jne 0x18f0
movl (%r14), %ecx
movq (%rbx), %rax
cmpl 0x20(%rax), %ecx
jl 0x1b0f
movq 0x18(%r14), %rcx
movl 0x8(%rcx), %edx
leal -0x1(%rdx), %esi
movl %esi, 0x8(%rcx)
cmpl $0x2, %edx
jg 0x1b0f
decl 0x18(%rax)
decl 0x10(%rax)
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1a61
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x8(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1a79
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1f26
movq 0x10(%r14), %rdi
movq 0x18(%r14), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
callq 0x25b4
movq 0x8(%r14), %rsi
movq 0x18(%r14), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdi
callq 0x25b4
movq 0x8(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1abe
movq 0x8(%r14), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x18ba
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1ad7
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x18ba
movq 0x8(%r14), %r15
movq 0x18(%r14), %rax
movq (%rax), %rdi
movl $0x20, %esi
callq 0x1100
movq 0x18(%r14), %rdi
movl $0x18, %esi
callq 0x1100
movl $0x20, %esi
movq %r14, %rdi
callq 0x1100
movq %r15, %r14
jmp 0x1a0d
movq 0x8(%r14), %r14
jmp 0x1a0d
movq %r15, -0x40(%rbp)
movq (%rbx), %r12
incl 0x18(%r12)
movl 0x1c(%r12), %r13d
leal 0x1(%r13), %eax
movl %eax, 0x1c(%r12)
movl $0x18, %edi
callq 0x10f0
movq %rax, %r15
movq %rax, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x2108
movl $0x2, 0x8(%r15)
movl $0x20, %edi
callq 0x10f0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x2568
movl $0x20, %edi
callq 0x10f0
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r15, -0x38(%rbp)
movq %r15, %rsi
movl %r13d, %edx
callq 0x2568
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1ba9
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x2574
testb %al, %al
movq -0x38(%rbp), %r15
movq -0x30(%rbp), %r13
jne 0x1bce
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x10(%r14), %rdi
movq %r12, %rsi
callq 0x25b4
movq 0x8(%r14), %rax
movq 0x8(%rax), %rsi
movq %r12, %rdi
callq 0x25b4
movq 0x10(%r12), %rdi
callq 0x2574
testb %al, %al
jne 0x1c05
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x1816
movq 0x8(%r12), %rdi
callq 0x2574
testb %al, %al
jne 0x1c1e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1816
movq -0x40(%rbp), %r12
movq 0x10(%r12), %rdi
callq 0x2574
testb %al, %al
jne 0x1c3d
movq 0x10(%r12), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x2574
testb %al, %al
jne 0x1c5c
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x10(%r12), %rdi
movq %r13, %rsi
callq 0x25b4
movq 0x8(%r12), %rax
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x25b4
movq 0x10(%r13), %rdi
callq 0x2574
testb %al, %al
jne 0x1c93
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x1816
movq 0x8(%r13), %rdi
callq 0x2574
testb %al, %al
jne 0x1cab
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1816
movq (%r15), %rdi
movq %r12, %rsi
callq 0x25b4
movq 0x8(%r12), %rdi
movq (%r15), %rsi
callq 0x25b4
movq 0x8(%r14), %rdi
movl $0x20, %esi
callq 0x1100
movl $0x20, %esi
movq %r14, %rdi
callq 0x1100
movq (%r15), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
callq 0x2574
testb %al, %al
jne 0x18f0
movl (%r14), %ecx
movq (%rbx), %rax
cmpl 0x20(%rax), %ecx
jl 0x1de7
movq 0x18(%r14), %rcx
movl 0x8(%rcx), %edx
leal -0x1(%rdx), %esi
movl %esi, 0x8(%rcx)
cmpl $0x2, %edx
jg 0x1de7
decl 0x10(%rax)
decl 0x18(%rax)
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1d39
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x1f26
movq 0x8(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1d51
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1f26
movq 0x10(%r14), %rdi
movq 0x18(%r14), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
callq 0x25b4
movq 0x8(%r14), %rsi
movq 0x18(%r14), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdi
callq 0x25b4
movq 0x8(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1d96
movq 0x8(%r14), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x18ba
movq 0x10(%r14), %rdi
callq 0x2574
testb %al, %al
jne 0x1daf
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x18ba
movq 0x8(%r14), %r15
movq 0x18(%r14), %rax
movq (%rax), %rdi
movl $0x20, %esi
callq 0x1100
movq 0x18(%r14), %rdi
movl $0x18, %esi
callq 0x1100
movl $0x20, %esi
movq %r14, %rdi
callq 0x1100
movq %r15, %r14
jmp 0x1ce5
movq 0x8(%r14), %r14
jmp 0x1ce5
jmp 0x1e00
movq %rax, %rbx
movl $0x20, %esi
movq -0x30(%rbp), %rdi
jmp 0x1e18
movq %rax, %rbx
movl $0x20, %esi
movq %r12, %rdi
jmp 0x1e18
movq %rax, %rbx
movl $0x18, %esi
movq %r15, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x11a0
nop