// File: /workspace/github_cpp_makefile_star2_O1/manschga[P]M5GB/M5GB.cpp
// Function: Tail_S_Reduce(Lab_poly const&, signature const&, int)
Lab_poly Tail_S_Reduce(const Lab_poly& F_,const signature& T, const int n)
{
#ifdef PRECISE_DEBUG
  cout << "doing Tail-reduce with polynomial " << F_ << endl;
#endif
  Lab_poly F = F_;
  signature S_F = F.Get_sign();
  //signature flag(table.size(),m);
  int rec_depth = 1; //cheap workaround until now
  if (rec_depth < 10000)
    {
      rec_depth++;
      polynomial f = F_.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();
#ifdef PRECISE_DEBUG
       cout << "t = " << monom->Get_term() << endl;
#endif
       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))//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))//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
	 }
     }
      F = Lab_poly(f_,S_F);
      Add_to_M(F);
      return F;
    }
  
  cout << "Too small recursion depth" << endl;
   
  return Lab_poly();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movl %ecx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x6314
movl 0x18(%rbx), %eax
movl %eax, 0x18(%r15)
movups 0x20(%rbx), %xmm0
movups %xmm0, 0x20(%r15)
movl 0x30(%rbx), %eax
movl %eax, 0x30(%r15)
movups 0x20(%rbx), %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x6314
movq %r12, 0x140(%rsp)
movl 0x18(%rbx), %eax
leaq 0xe0(%rsp), %rsi
movl %eax, 0x18(%rsi)
leaq 0x10(%rsp), %rdi
callq 0x6314
movl 0xf8(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0xe0(%rsp), %r13
leaq 0x10(%r13), %rax
cmpq 0xe8(%rsp), %rax
je 0xb074
leaq 0x30(%rsp), %r12
movslq 0x10(%r13), %r15
movl 0x18(%r13), %ecx
movl %ecx, 0xc(%rsp)
movq %rax, %r13
movq 0xf89e(%rip), %rdi      # 0x1a330
movq %r15, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq 0xf884(%rip), %rax      # 0x1a328
movq (%rax,%rdx,8), %r8
movl $0x0, %eax
testq %r8, %r8
je 0xaaeb
movq (%r8), %r9
movq %r8, %rax
cmpq 0x8(%r9), %r15
je 0xaaeb
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0xaae9
movq 0x8(%r9), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0xaaeb
movq %r10, %rax
cmpq %rsi, %r15
jne 0xaabe
jmp 0xaaeb
xorl %eax, %eax
testq %rax, %rax
je 0xac63
cmpq $0x0, (%rax)
je 0xac63
movq (%r8), %rax
movq %r8, %rdx
cmpq 0x8(%rax), %r15
je 0xab12
movq %rax, %rdx
movq (%rax), %rax
jmp 0xab04
movq (%rdx), %rax
movq 0x8(%r14), %r9
movl (%r14), %r10d
movl 0x10(%rax), %edx
xorl %esi, %esi
cmpl %edx, %r10d
setl %sil
xorl %r11d, %r11d
cmpq 0x18(%rax), %r9
setne %al
setl %r11b
cmovnel %r11d, %esi
cmpl %r10d, %edx
setne %dl
orb %al, %dl
testb %dl, %sil
je 0xac0a
movq %r15, %rax
xorl %edx, %edx
divq 0xf8b9(%rip)            # 0x1a410
movq 0xf8aa(%rip), %rax      # 0x1a408
movq (%rax,%rdx,8), %rax
movq (%rax), %rcx
cmpq 0x8(%rcx), %r15
je 0xab73
movq %rcx, %rax
movq (%rcx), %rcx
jmp 0xab65
movq (%rax), %rax
movslq 0x10(%rax), %rax
cmpq %rax, 0xf74f(%rip)      # 0x1a2d0
je 0xb062
movq %r15, %rdi
movq %r14, %rsi
callq 0x9da4
testb %al, %al
jne 0xac63
movq %r15, 0x30(%rsp)
leaq 0xf862(%rip), %rbp      # 0x1a408
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe152
movq 0xf718(%rip), %rbx      # 0x1a2d0
movq %r15, 0x30(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe934
movl %ebx, (%rax)
movq %r15, 0x30(%rsp)
leaq 0xf752(%rip), %rdi      # 0x1a328
movq %r12, %rsi
callq 0xe50c
movq %r15, %rdi
callq 0x9f88
movl %eax, %ebx
movq %rdx, %rbp
movq %r15, 0x30(%rsp)
leaq 0xf731(%rip), %rdi      # 0x1a328
movq %r12, %rsi
callq 0xe9de
movl %ebx, (%rax)
movq %rbp, 0x8(%rax)
jmp 0xb062
testq %r8, %r8
je 0xac44
movq (%r8), %r11
cmpq 0x8(%r11), %r15
je 0xac47
movq %r11, %rbp
movq (%r11), %r11
testq %r11, %r11
je 0xac44
movq 0x8(%r11), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %r8d
cmpq %rcx, %rdx
jne 0xac47
movq %rbp, %r8
cmpq %rsi, %r15
jne 0xac18
jmp 0xac47
xorl %r8d, %r8d
testq %r8, %r8
je 0xac51
movq (%r8), %rax
jmp 0xac53
xorl %eax, %eax
cmpl %r10d, 0x10(%rax)
jne 0xac63
cmpq %r9, 0x18(%rax)
je 0xab4b
movq 0xf61e(%rip), %rdi      # 0x1a288
movq %r15, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq 0xf604(%rip), %rax      # 0x1a280
movq (%rax,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0xacc3
movq (%rdx), %r8
movq %rdx, %rax
cmpq 0x8(%r8), %r15
je 0xacc3
movq %r8, %r9
movq (%r8), %r8
testq %r8, %r8
je 0xacc1
movq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0xacc3
movq %r9, %rax
cmpq %rsi, %r15
jne 0xac96
jmp 0xacc3
xorl %eax, %eax
testq %rax, %rax
je 0xaccd
movq (%rax), %rbp
jmp 0xaccf
xorl %ebp, %ebp
testq %rbp, %rbp
je 0xae90
movq %r14, %rbx
movq %r12, %r14
leaq 0x8(%rbp), %rcx
movq 0x8(%rbp), %r8
movq %r8, %rax
xorl %edx, %edx
divq 0xf606(%rip)            # 0x1a2f8
leaq 0xf5f7(%rip), %rdi      # 0x1a2f0
movq %rdx, %rsi
movq %rcx, %rdx
movq %r8, %rcx
callq 0xdd4a
leaq 0x168(%rsp), %r12
movq (%rax), %r15
leaq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x6314
movl 0x28(%r15), %eax
movl %eax, 0x180(%rsp)
movl 0x10(%rbp), %edx
movq 0x18(%rbp), %rcx
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x4bf0
movq %r14, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %r14
movq %rbx, %rdx
movl 0x8(%rsp), %ecx
callq 0xa15e
leaq 0x80(%rsp), %r15
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xad80
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x168(%rsp), %rdi
testq %rdi, %rdi
je 0xad9d
movq 0x178(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %r15, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
movl $0x1, %ecx
callq 0x6402
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x6640
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0xa8(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movq $0x0, 0xb0(%rsp)
testq %rdi, %rdi
je 0xae0e
subq %rdi, %rsi
callq 0x2180
movl 0xb8(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xae36
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xae53
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1213f
movups (%rax), %xmm0
incl 0xf345(%rip)            # 0x1a1b0
movaps %xmm0, 0x70(%rsp)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb062
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0xb062
movq %r15, %rdi
movq %r14, %rsi
callq 0x9da4
testb %al, %al
je 0xaff7
movq %r15, 0x30(%rsp)
leaq 0xf479(%rip), %rdi      # 0x1a328
movq %r12, %rsi
callq 0xe50c
movq %r15, 0x30(%rsp)
leaq 0xf545(%rip), %rdi      # 0x1a408
movq %r12, %rsi
callq 0xe152
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xa2c1
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl 0x8(%rsp), %ecx
callq 0xa9d4
leaq 0x188(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movl 0xc(%rsp), %edx
movl $0x1, %ecx
callq 0x6402
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq 0x6640
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x88(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
testq %rdi, %rdi
je 0xaf6c
subq %rdi, %rsi
callq 0x2180
movl 0x98(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xaf94
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x188(%rsp), %rdi
testq %rdi, %rdi
je 0xafb1
movq 0x198(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x70(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1213f
movups (%rax), %xmm0
incl 0xf1e4(%rip)            # 0x1a1b0
movaps %xmm0, 0x70(%rsp)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xae70
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0xae70
movq %r15, 0x30(%rsp)
leaq 0xf325(%rip), %rdi      # 0x1a328
movq %r12, %rsi
callq 0xe50c
movq %r15, %rdi
callq 0x9f88
movl %eax, %ebx
movq %rdx, %rbp
movq %r15, 0x30(%rsp)
leaq 0xf304(%rip), %rdi      # 0x1a328
movq %r12, %rsi
callq 0xe9de
movl %ebx, (%rax)
movq %rbp, 0x8(%rax)
movq %r15, 0x30(%rsp)
leaq 0xf3ca(%rip), %rbp      # 0x1a408
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe152
movq 0xf280(%rip), %rbx      # 0x1a2d0
movq %r15, 0x30(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe934
movl %ebx, (%rax)
leaq 0x10(%r13), %rax
cmpq 0xe8(%rsp), %rax
jne 0xaa7c
leaq 0x148(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x6314
movl 0x28(%rsp), %eax
leaq 0x148(%rsp), %rsi
movl %eax, 0x18(%rsi)
movl 0x70(%rsp), %edx
movq 0x78(%rsp), %rcx
leaq 0x30(%rsp), %rdi
callq 0x4bf0
leaq 0x120(%rsp), %rbx
movq -0x20(%rbx), %rdi
movq -0x10(%rbx), %rsi
movq 0x30(%rsp), %rax
movq %rax, -0x20(%rbx)
movups 0x38(%rsp), %xmm0
movups %xmm0, -0x18(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
testq %rdi, %rdi
movq 0x140(%rsp), %r14
je 0xb0f0
subq %rdi, %rsi
callq 0x2180
movl 0x48(%rsp), %eax
movl %eax, 0x118(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x10(%rbx)
movaps 0x50(%rsp), %xmm0
movups %xmm0, (%rbx)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb121
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0xb13e
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x100(%rsp), %rdi
callq 0x9823
movq 0x100(%rsp), %rax
movq %rax, (%r14)
movq 0x108(%rsp), %rax
movq %rax, 0x8(%r14)
movq 0x110(%rsp), %rax
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, 0x100(%rsp)
movq $0x0, 0x110(%rsp)
movl 0x118(%rsp), %eax
movl %eax, 0x18(%r14)
movl 0x10(%rbx), %eax
movl %eax, 0x30(%r14)
movups (%rbx), %xmm0
movups %xmm0, 0x20(%r14)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xb1b6
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0xb1d3
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0xb1f0
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %r14, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb341
movq %rax, %rbx
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0xb344
movq 0x158(%rsp), %rsi
jmp 0xb337
jmp 0xb341
movq %rax, %rbx
jmp 0xb35b
movq %rax, %rbx
jmp 0xb378
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb287
movq %rax, %rbx
movq 0x188(%rsp), %rdi
testq %rdi, %rdi
je 0xb28a
movq 0x198(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0xb28a
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xb2f1
movq 0xb0(%rsp), %rsi
jmp 0xb2e9
jmp 0xb2c7
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb341
jmp 0xb2c7
movq %rax, %rbx
jmp 0xb2f1
movq %rax, %rbx
jmp 0xb322
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xb2f1
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb344
movq 0x40(%rsp), %rsi
jmp 0xb337
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0xb322
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x168(%rsp), %rdi
testq %rdi, %rdi
je 0xb344
movq 0x178(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0xb344
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xb35b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0xb378
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0xb395
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2240