// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x58(%rbp)
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x16d80
cltq
movq %rax, -0x28(%rbp)
leaq 0x2c7d0(%rip), %rdi     # 0x40448
leaq -0x28(%rbp), %rsi
callq 0x16f60
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
leaq 0x2c74b(%rip), %rdi     # 0x403d8
leaq -0x28(%rbp), %rsi
callq 0x16fc0
movq (%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x2c67c(%rip), %rdi     # 0x40330
callq 0x16cc0
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jne 0x13d0a
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x171d0
testb $0x1, %al
jne 0x13ce9
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xbe50
testb $0x1, %al
jne 0x13ce9
jmp 0x13d0a
movq -0x58(%rbp), %rdi
movl 0x2c539(%rip), %eax     # 0x4022c
addl $0x1, %eax
movl %eax, 0x2c530(%rip)     # 0x4022c
movq -0x10(%rbp), %rsi
callq 0x16be0
jmp 0x13dc6
movslq -0x2c(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq 0x2c617(%rip), %rdi     # 0x40330
callq 0x16cc0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x13d43
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0x145a0
jmp 0x13dc6
movslq -0x2c(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq 0x2c5de(%rip), %rdi     # 0x40330
callq 0x16cc0
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jne 0x13d9b
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xbea0
testb $0x1, %al
jne 0x13d76
jmp 0x13d9b
movq -0x58(%rbp), %rdi
movl 0x2c4b0(%rip), %eax     # 0x40230
addl $0x1, %eax
movl %eax, 0x2c4a7(%rip)     # 0x40230
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0x145a0
jmp 0x13dc6
movq 0x2c236(%rip), %rdi     # 0x3ffd8
leaq 0x175f1(%rip), %rsi     # 0x2b39a
callq 0x2190
movq %rax, %rdi
movq 0x2c210(%rip), %rsi     # 0x3ffc8
callq 0x21d0
movq -0x58(%rbp), %rdi
callq 0x9cf0
movq -0x50(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq