// File: /workspace/github_cpp_makefile_star2_O2/manschga[P]M5GB/M5GB.cpp
// Function: Update_m(Lab_poly const&, signature const&, int)
Lab_poly Update_m(const Lab_poly& m, const signature& T, const int n)//needs huge improvment -> ineffective!
{
  term_int LT = m.Get_LT();
  int generation = Generations_M[LT];
  signature Flag = Flags_M[LT];
  //4 cases; check if current generation and flag >= T
  if (generation == G_.size() && (Flag > T || Flag == T))
    {
      straight_m_reduces++;
      return m;
    }
  //m_re_reduced++;
  
  if (generation < G_.size())
    {
      /*
      Lab_poly F = m;
      signature S_F = m.Get_sign();
      polynomial f = m.Get_poly();
      polynomial f_ = f;
      for (auto monom = ++f.Get_monomials().begin(); monom != f.Get_monomials().end(); ++monom) //leave out LT, need to stay at that datastructure, otherwise for-loop! (poss error source)
	{
	  int t = monom->Get_term();
	  int c = monom->Get_coeff();
	  if (Flags_T.find(t) != Flags_T.end() &&  (Flags_T.find(t)->second > T || Flags_T.find(t)->second == T )) 
	    {
	      if (Generations_T.find(t)->second == G_.size())//t is known to be irreducible -> nothing needs to be done!
		{
		  continue;
		}
	      else if (!Check_TGFT(t,T,generation))//if not reducible by G, possible improvement: check only for newer elements, nothing for counting argument!
		{
		  Generations_T.erase(t);
		  Generations_T[t] = G_.size();
		  Flags_T.erase(t);//update signature flag since new generation, improvement: take only newer ons and do min with old flag
		  Flags_T[t] = Create_term_flag(t);
		  continue;
		}
	    }
	  auto itr = LT_M.find(t);
	  if (itr != LT_M.end()) //element found, check on order if checking by div by G is faster?
	    {
	      Lab_poly m_ = Update_m(Lab_poly(M.find(itr->first)->second,itr->second),T,n); //bring m in right form such that it is tail-irreducible again
	      f_ = f_ - m_.Get_poly().Multiply(c,true); //f - c_t(f)*m_t
	      S_F = max(S_F,m_.Get_sign());
	      number_of_reductions++;
	    }
	  else if (Check_TGFT(t,T,generation))//if reducible by G
	{
	  Flags_T.erase(t);//t is no longer irreducible (only first one needed, rest for less storage) 
	  Generations_T.erase(t);
	  
	  Lab_poly vM = Div_by_M(t,T);//find best divisor in M, improvment: map G -> M, only need to check that m's on divisibility
	  Lab_poly m_ = Tail_S_Reduce(vM,T,n); //create new element in M such that it is tail-irreducible
	  f_ = f_ - m_.Get_poly().Multiply(c,true); //f - c_t(f)*m_t
	  S_F = max(S_F,m_.Get_sign());
	  number_of_reductions++;	
	}
	  else
	    {
	      //not yet reducible -> keep t as irreducible term!
	      Flags_T.erase(t);//update signature flag since new generation, improvement: take only newer ons and do min with old flag
	      Flags_T[t] = Create_term_flag(t);
	      Generations_T.erase(t);
	      Generations_T[t] = G_.size();//set current generation when t was updated the last time
	    }
	}
      //add_to?
      F = Lab_poly(f_,S_F);
      Add_to_M(F);
      return F;
      */
      return Tail_S_Reduce(m,T,n); //need only to check for newer generations -> improvement, but not for counting argument!
    }
  if (generation == G_.size() && Flag < T)
    {
        m_re_reduced++;
	return Tail_S_Reduce(m,T,n); //need only to check for newer generations -> improvement, but not for counting argument!); //need only to check for those terms -> improvement, but not for counting argument!
    }
  cout << "Error occured! element newer than current generation!" << endl;
  return Lab_poly();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x97f2
cltq
leaq 0x10(%rsp), %r12
movq %rax, (%r12)
leaq 0x10457(%rip), %rdi     # 0x183d8
movq %r12, %rsi
callq 0xb4c0
movslq (%rax), %r13
leaq 0x103d5(%rip), %rdi     # 0x18368
movq %r12, %rsi
callq 0xb908
movups (%rax), %xmm0
movaps %xmm0, (%rsp)
cmpq %r13, 0x1032f(%rip)     # 0x182d8
jne 0x7fe0
movq %rsp, %rdi
movq %r15, %rsi
callq 0x984e
testb %al, %al
je 0x7fcd
incl 0x101fc(%rip)           # 0x181bc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x97ca
jmp 0x8049
movl (%rsp), %eax
cmpl (%r15), %eax
jne 0x7fe0
movq 0x8(%rsp), %rax
cmpq 0x8(%r15), %rax
je 0x7fba
cmpq %r13, 0x102f1(%rip)     # 0x182d8
jbe 0x7ffb
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x84e9
jmp 0x8049
jne 0x8026
movq 0x8(%rsp), %rax
movl (%rsp), %ecx
xorl %edx, %edx
cmpl (%r15), %ecx
setl %dl
xorl %ecx, %ecx
cmpq 0x8(%r15), %rax
setl %cl
cmovel %edx, %ecx
cmpb $0x1, %cl
jne 0x8026
incl 0x1019c(%rip)           # 0x181c0
jmp 0x7fe9
movq 0xffab(%rip), %rdi      # 0x17fd8
leaq 0x7370(%rip), %rsi      # 0xf3a4
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x44b4
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq