// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0xcb20
cltq
movq %rsp, %r12
movq %rax, (%r12)
leaq 0xfce6(%rip), %rdi      # 0x1a3e0
movq %r12, %rsi
callq 0xe68e
movslq (%rax), %r13
leaq 0xfc64(%rip), %rdi      # 0x1a370
movq %r12, %rsi
callq 0xea16
cmpq %r13, 0xfbc5(%rip)      # 0x1a2e0
jne 0xa792
movl (%rax), %edx
movq 0x8(%rax), %rax
movq 0x8(%r15), %rcx
movl (%r15), %esi
xorl %edi, %edi
cmpl %edx, %esi
setl %dil
xorl %r8d, %r8d
cmpq %rax, %rcx
setne %r9b
setl %r8b
cmovnel %r8d, %edi
cmpl %esi, %edx
setne %r8b
orb %r9b, %r8b
testb %r8b, %dil
jne 0xa768
cmpl %esi, %edx
sete %dil
cmpq %rcx, %rax
sete %r8b
andb %dil, %r8b
cmpb $0x1, %r8b
jne 0xa7a6
incl 0xfa56(%rip)            # 0x1a1c4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68a4
movl 0x18(%r14), %eax
movl %eax, 0x18(%rbx)
movups 0x20(%r14), %xmm0
movups %xmm0, 0x20(%rbx)
movl 0x30(%r14), %eax
movl %eax, 0x30(%rbx)
jmp 0xa810
jbe 0xa7c6
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xaf68
jmp 0xa810
xorl %edi, %edi
cmpl %esi, %edx
setl %dil
xorl %edx, %edx
cmpq %rcx, %rax
setl %dl
cmovel %edi, %edx
cmpb $0x1, %dl
jne 0xa7c6
incl 0xfa04(%rip)            # 0x1a1c8
jmp 0xa794
movq 0xf80b(%rip), %r14      # 0x19fd8
leaq 0x8c20(%rip), %rsi      # 0x133f4
movl $0x35, %edx
movq %r14, %rdi
callq 0x21a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x5038
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq