// File: manschga[P]M5GB/M5GB.cpp
// Function: Low_pol(signature const&, Lab_poly const&, int, int)
Lab_poly Low_pol(const signature& smallest_sign, const Lab_poly& uF, const int generation, int n)
{
  term_int LT_low = 0;
  term_int remainder_low = 0;
  Lab_poly Low = uF;
  bool not_yet = true;
#ifdef LARGEST_LT //no sense to do largest lt heuristic here
  return uF;
#endif
  for (auto& it : LT_M)
    {
      if (smallest_sign.Get_index() == it.second.Get_index())
	{
	  if (isDivisible(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first))
	    {
	      polynomial test = M.find(it.first)->second.Multiply(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first));
	      
#ifdef LARGE_GEN
	      if (not_yet) //still uF is best
		{

	       if (Generations_G.find(it.first)->second > generation) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		    }
		}
	      else
		{
		  if (Generations_G.find(it.first)->second > Generations_G.find(LT_low)->second) //different heuristics to check
		     {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		     }
		}
#endif
	      //*
#ifdef NUMBER_OF_TERMS
	      if (not_yet) //still uF is best
		{
		  if (test.size() <= uF.Get_poly().size()) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		    }
		}
	      else
		{
		  if (test.size() < Low.Get_poly().size()) //different heuristics to check
		     {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		     }
		}
#endif
	      //*/
#ifdef SMALLEST_REMAINDER
	      if (not_yet) //still uF is best
		{
		  if (test.size() < uF.Get_poly().size()) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		      remainder_low = term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n);
		    }
		}
	      else
		{
		  if(term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n) > remainder_low)
		    {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		       remainder_low = term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n);
		     }
		}
#endif
#ifdef SMALLEST_LT
	      if (not_yet) //still uF is best
		{
		  if (test.Get_LT() <= uF.Get_LT()) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		    }
		}
	      else
		{
		  if (test.Get_LT() <= Low.Get_LT()) //different heuristics to check
		     {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		     }
		}
#endif
	    }
	}
    }
	  return Low;
	  /*

  
  for (auto& it : LT_M) 
    {
      if (Lab_pol_M.find(it.first)->second == true) //check if correct label!
	{
      if (smallest_sign.Get_index() == it.second.Get_index())
	{
	  if (isDivisible(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first))
	    {
	      polynomial test = M.find(it.first)->second.Multiply(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first));
#ifdef high_gen
	      if (not_yet) //still uF is best
		{

	       if (Generations_M.find(it.first)->second > generation) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		    }
		}
	      else
		{
		  if (Generations_M.find(it.first)->second > Generations_M.find(LT_low)->second) //different heuristics to check
		     {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		     }
		}
#endif

#ifdef smallest_size
	      if (not_yet) //still uF is best
		{
		  if (test.size() <= uF.Get_poly().size()) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		    }
		}
	      else
		{
		  if (test.size() < Low.Get_poly().size()) //different heuristics to check
		     {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		     }
		}
#endif
#ifdef smallest_remainder
	      if (not_yet) //still uF is best
		{
		  if (test.size() <= uF.Get_poly().size()) //different heuristics to check
		    {
		      not_yet = false;
		      Low = Lab_poly(test,smallest_sign);
		      LT_low = it.first;
		      remainder_low = term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n);
		    }
		}
	      else
		{
		  if(term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n) > remainder_low)
		    {
		       Low = Lab_poly(test,smallest_sign);
		       LT_low = it.first;
		       remainder_low = term_to_int(get_remainder(table[smallest_sign.Get_term()].first,table[it.second.Get_term()].first),n);
		     }
		}
#endif
	    }
	    }
	}
    }
  return Low;
	  */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x1c0(%rbp)
movq %rdi, %rax
movq %rax, -0x1b8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
movq -0x18(%rbp), %rsi
callq 0x16be0
movb $0x1, -0x32(%rbp)
leaq 0x2e771(%rip), %rax     # 0x402f8
movq %rax, -0x40(%rbp)
leaq 0x2e766(%rip), %rdi     # 0x402f8
callq 0x16ff0
movq %rax, -0x48(%rbp)
leaq 0x2e756(%rip), %rdi     # 0x402f8
callq 0x17020
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x17050
testb $0x1, %al
jne 0x11bc1
jmp 0x12016
leaq -0x48(%rbp), %rdi
callq 0x17080
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x170b0
movl %eax, -0x1c4(%rbp)
jmp 0x11bdf
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
callq 0x170b0
movl %eax, -0x1c8(%rbp)
jmp 0x11bf4
movl -0x1c4(%rbp), %eax
movl -0x1c8(%rbp), %ecx
cmpl %ecx, %eax
jne 0x12006
movq -0x10(%rbp), %rdi
callq 0x16b30
movq %rax, -0x1d0(%rbp)
jmp 0x11c1a
movq -0x1d0(%rbp), %rsi
leaq 0x2e5b8(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, -0x1e0(%rbp)
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
callq 0x16b30
movq %rax, -0x1d8(%rbp)
jmp 0x11c4a
movq -0x1d8(%rbp), %rsi
leaq 0x2e588(%rip), %rdi     # 0x401e0
callq 0xd480
movq -0x1e0(%rbp), %rdi
movq %rax, %rsi
callq 0x5880
movb %al, -0x1e1(%rbp)
jmp 0x11c74
movb -0x1e1(%rbp), %al
testb $0x1, %al
jne 0x11c83
jmp 0x12004
movq -0x58(%rbp), %rsi
leaq 0x2e6da(%rip), %rdi     # 0x40368
callq 0x170c0
movq %rax, -0x1f0(%rbp)
jmp 0x11c9c
movq -0x1f0(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x170f0
addq $0x8, %rax
movq %rax, -0x200(%rbp)
movq -0x10(%rbp), %rdi
callq 0x16b30
movq %rax, -0x1f8(%rbp)
jmp 0x11cd3
movq -0x1f8(%rbp), %rsi
leaq 0x2e4ff(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, %rsi
leaq -0xc0(%rbp), %rdi
callq 0x5ff0
jmp 0x11cf7
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
callq 0x16b30
movq %rax, -0x208(%rbp)
jmp 0x11d0d
movq -0x208(%rbp), %rsi
leaq 0x2e4c5(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, %rsi
leaq -0xd8(%rbp), %rdi
callq 0x5ff0
jmp 0x11d31
leaq -0xa8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x262f0
jmp 0x11d4d
movq -0x200(%rbp), %rsi
leaq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xcfc0
jmp 0x11d69
leaq -0xa8(%rbp), %rdi
callq 0x3b70
leaq -0xd8(%rbp), %rdi
callq 0x3b70
leaq -0xc0(%rbp), %rdi
callq 0x3b70
testb $0x1, -0x32(%rbp)
je 0x11efe
leaq -0x88(%rbp), %rdi
callq 0x17120
movl %eax, -0x20c(%rbp)
jmp 0x11dab
movq -0x18(%rbp), %rdi
callq 0xb8c0
movq %rax, -0x218(%rbp)
jmp 0x11dbd
movq -0x218(%rbp), %rdi
callq 0x17120
movl %eax, -0x21c(%rbp)
jmp 0x11dd1
movl -0x20c(%rbp), %eax
movl -0x21c(%rbp), %ecx
cmpl %ecx, %eax
jg 0x11ef9
movb $0x0, -0x32(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xd980
jmp 0x11dfe
movq -0x10(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x140(%rbp)
movl -0x140(%rbp), %edx
movq -0x138(%rbp), %rcx
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x9d90
jmp 0x11e2e
movq -0x1c0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xbf00
leaq -0x110(%rbp), %rdi
callq 0xbc80
leaq -0x130(%rbp), %rdi
callq 0x44a0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x11ef9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1203c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x11eba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x11eae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3b70
leaq -0xd8(%rbp), %rdi
callq 0x3b70
leaq -0xc0(%rbp), %rdi
callq 0x3b70
jmp 0x1203c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x11ff6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x44a0
jmp 0x11ff6
jmp 0x11fe8
leaq -0x88(%rbp), %rdi
callq 0x17120
movl %eax, -0x220(%rbp)
jmp 0x11f12
movq -0x1c0(%rbp), %rdi
callq 0xb8c0
movq %rax, -0x228(%rbp)
jmp 0x11f27
movq -0x228(%rbp), %rdi
callq 0x17120
movl %eax, -0x22c(%rbp)
jmp 0x11f3b
movl -0x220(%rbp), %eax
movl -0x22c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x11fe6
leaq -0x198(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xd980
jmp 0x11f64
movq -0x10(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x1b0(%rbp)
movl -0x1b0(%rbp), %edx
movq -0x1a8(%rbp), %rcx
leaq -0x178(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x9d90
jmp 0x11f94
movq -0x1c0(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0xbf00
leaq -0x178(%rbp), %rdi
callq 0xbc80
leaq -0x198(%rbp), %rdi
callq 0x44a0
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x11fe6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x44a0
jmp 0x11ff6
jmp 0x11fe8
leaq -0x88(%rbp), %rdi
callq 0x44a0
jmp 0x12004
leaq -0x88(%rbp), %rdi
callq 0x44a0
jmp 0x1203c
jmp 0x12006
jmp 0x12008
leaq -0x48(%rbp), %rdi
callq 0x17140
jmp 0x11bab
movb $0x1, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
jne 0x1202c
movq -0x1c0(%rbp), %rdi
callq 0xbc80
movq -0x1b8(%rbp), %rax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
movq -0x1c0(%rbp), %rdi
callq 0xbc80
movq -0x60(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)