// File: /workspace/github_cpp_makefile_star2_O1/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 0xc69a
cltq
movq %rsp, %r12
movq %rax, (%r12)
leaq 0x10244(%rip), %rdi     # 0x1a3d0
movq %r12, %rsi
callq 0xe288
movslq (%rax), %r13
leaq 0x101c2(%rip), %rdi     # 0x1a360
movq %r12, %rsi
callq 0xe642
movl (%rax), %ecx
movq 0x8(%rax), %rax
movq 0x1011d(%rip), %rdx     # 0x1a2d0
cmpq %r13, %rdx
jne 0xa22b
movq 0x8(%r15), %rsi
movl (%r15), %edi
xorl %r8d, %r8d
cmpl %ecx, %edi
setl %r8b
xorl %r9d, %r9d
cmpq %rax, %rsi
setne %r10b
setl %r9b
cmovnel %r9d, %r8d
cmpl %edi, %ecx
setne %r9b
orb %r10b, %r9b
testb %r9b, %r8b
jne 0xa1fe
cmpl %edi, %ecx
sete %dil
cmpq %rsi, %rax
sete %sil
andb %dil, %sil
cmpb $0x1, %sil
jne 0xa22b
incl 0xffb0(%rip)            # 0x1a1b4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6314
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 0xa2af
cmpq %r13, %rdx
jbe 0xa242
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xa9d4
jmp 0xa2af
jne 0xa265
xorl %edx, %edx
cmpl (%r15), %ecx
setl %dl
xorl %ecx, %ecx
cmpq 0x8(%r15), %rax
setl %cl
cmovel %edx, %ecx
cmpb $0x1, %cl
jne 0xa265
incl 0xff55(%rip)            # 0x1a1b8
jmp 0xa230
movq 0xfd6c(%rip), %r14      # 0x19fd8
leaq 0x9124(%rip), %rsi      # 0x13397
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 0x4b5c
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq