// File: manschga[P]M5GB/M5GB.cpp
// Function: Update_pairs(Lab_poly const&, std::vector<polynomial, std::allocator<polynomial>> const&, int, int&)
void Update_pairs(const Lab_poly& F, const vector<polynomial>& system, const int n, int& BB_counter)
{
  //cout << "pair update: " << LT_G.size() << endl;
  for (auto& it : LT_G) 
    {
      //cout << Generations_G.find(it.first)->second << endl;
      if (Generations_G.find(it.first)->second <= n) //do not add spols coming from field equations!
	{
	  //cout << "Field equation detected!" << endl;
	  continue;
	}      
      
      if (First_Buchberger(table[F.Get_LT()].first,table[it.first].first))
	{
	  //inefficient!
	  /*
	  pair<Lab_poly,Lab_poly> Spair = spair(F,Lab_poly(G_.find(it.first)->second,it.second));
	  Lab_poly Spol = Spair.first-Spair.second;
	  H[Spol.Get_sign().Get_index()].emplace_back(Spol.Get_sign().Get_term());
	  */
	  BB_counter ++;
	  continue;
	}
     
      //*/
      pair<Lab_poly,Lab_poly> Spair = spair(F,Lab_poly(G_.find(it.first)->second,it.second));
      pair<pair<Lab_poly,int>,pair<Lab_poly,int>> spair_gen = make_pair(make_pair(Spair.first,G_.size()+1),make_pair(Spair.second,Generations_G.find(it.first)->second));
      if (Spair.first.Get_sign() == Spair.second.Get_sign()) // remove pair if same signature! Get counter for statistic
	{
	  continue;
	  //cout << "singular pair!" << endl;
	}
      else
	{
	  if (Spair.first.Get_sign() < Spair.second.Get_sign()) 
	    {
	      //change order to ensure first > second
	      Spair = make_pair(Spair.second,Spair.first);
	      spair_gen = make_pair(spair_gen.second,spair_gen.first);
	    }
	  //insert correctly

	  Spol_sign.insert(Spair.first.Get_sign()); //not efficient, only inserted when signature not yet in
	  Spairs_map.insert(make_pair(Spair.first.Get_sign(),spair_gen.first)); //not efficient, only inserted when signature not yet in
	  //auto correct_insert = P.insert(Spair);
	  /*
	  if (!correct_insert.second)
	    {
	      std::cout << "Failed to insert element!" << std::endl;
	      cout << Spair.first.Get_sign() << endl;
	      for (auto it = P.begin(); it != P.end(); ++it)
		{
		  cout << it->first.Get_sign() << endl;
		}
	      
	    }
	  */
	}
    }

      
#ifdef DEBUG
    for (auto it = P.begin(); it != P.end(); ++it)
    {
      cout << it->first.Get_sign() << ","  << it->second.Get_sign() << endl;
    }
#endif

  //cout << "size of P after update: " << P.size() << endl;
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4c0, %rsp            # imm = 0x4C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
leaq 0x2d77f(%rip), %rax     # 0x402c0
movq %rax, -0x28(%rbp)
leaq 0x2d774(%rip), %rdi     # 0x402c0
callq 0x16ff0
movq %rax, -0x30(%rbp)
leaq 0x2d764(%rip), %rdi     # 0x402c0
callq 0x17020
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x17050
testb $0x1, %al
jne 0x12b7b
jmp 0x130c7
leaq -0x30(%rbp), %rdi
callq 0x17080
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
leaq 0x2d87d(%rip), %rdi     # 0x40410
callq 0x17220
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x17250
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
jg 0x12bb2
jmp 0x1309f
movq -0x8(%rbp), %rdi
callq 0x16d80
movslq %eax, %rsi
leaq 0x2d61b(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, -0x438(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rsi
leaq 0x2d601(%rip), %rdi     # 0x401e0
callq 0xd480
movq -0x438(%rbp), %rdi
movq %rax, %rsi
callq 0x15160
testb $0x1, %al
jne 0x12bf9
jmp 0x12c09
movq -0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1309f
movq -0x8(%rbp), %rax
movq %rax, -0x448(%rbp)
movq -0x40(%rbp), %rsi
leaq 0x2d711(%rip), %rdi     # 0x40330
callq 0x170c0
movq %rax, -0x118(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x170f0
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x440(%rbp)
callq 0xd980
movq -0x440(%rbp), %rsi
movq -0x40(%rbp), %rax
movups 0x8(%rax), %xmm0
movaps %xmm0, -0x130(%rbp)
movl -0x130(%rbp), %edx
movq -0x128(%rbp), %rcx
leaq -0xf0(%rbp), %rdi
callq 0x9d90
jmp 0x12c82
movq -0x448(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0xa0a0
jmp 0x12c9e
leaq -0xf0(%rbp), %rdi
callq 0xbc80
leaq -0x110(%rbp), %rdi
callq 0x44a0
leaq 0x2d673(%rip), %rdi     # 0x40330
callq 0x16cc0
incq %rax
movq %rax, -0x288(%rbp)
leaq -0x280(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0x288(%rbp), %rdx
callq 0x17500
jmp 0x12ce8
leaq -0x80(%rbp), %rax
movq %rax, -0x458(%rbp)
movq -0x40(%rbp), %rsi
leaq 0x2d712(%rip), %rdi     # 0x40410
callq 0x17220
movq %rax, -0x450(%rbp)
jmp 0x12d0c
movq -0x450(%rbp), %rax
movq %rax, -0x2d0(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x17250
movq -0x458(%rbp), %rsi
movq %rax, %rdx
addq $0x8, %rdx
leaq -0x2c8(%rbp), %rdi
callq 0x17540
jmp 0x12d42
leaq -0x240(%rbp), %rdi
leaq -0x280(%rbp), %rsi
leaq -0x2c8(%rbp), %rdx
callq 0x174c0
jmp 0x12d5e
leaq -0x1c0(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x17580
jmp 0x12d73
leaq -0x240(%rbp), %rdi
callq 0x175c0
leaq -0x2c8(%rbp), %rdi
callq 0x175f0
leaq -0x280(%rbp), %rdi
callq 0x17610
leaq -0xb8(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x460(%rbp)
jmp 0x12dac
leaq -0x80(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x468(%rbp)
jmp 0x12dbe
movq -0x468(%rbp), %rsi
movq -0x460(%rbp), %rdi
callq 0xbe50
movb %al, -0x469(%rbp)
jmp 0x12dd9
movb -0x469(%rbp), %al
testb $0x1, %al
jne 0x12de8
jmp 0x12ecb
movl $0x3, -0x2d4(%rbp)
jmp 0x13077
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
jmp 0x12e29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xbc80
leaq -0x110(%rbp), %rdi
callq 0x44a0
jmp 0x130d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
jmp 0x130b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
jmp 0x12ea3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
jmp 0x12e97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x175c0
leaq -0x2c8(%rbp), %rdi
callq 0x175f0
leaq -0x280(%rbp), %rdi
callq 0x17610
jmp 0x130b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
jmp 0x130ad
leaq -0xb8(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x478(%rbp)
jmp 0x12ee0
leaq -0x80(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x480(%rbp)
jmp 0x12ef2
movq -0x480(%rbp), %rsi
movq -0x478(%rbp), %rdi
callq 0xbea0
movb %al, -0x481(%rbp)
jmp 0x12f0d
movb -0x481(%rbp), %al
testb $0x1, %al
jne 0x12f19
jmp 0x12f93
leaq -0x80(%rbp), %rsi
leaq -0x348(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x17630
jmp 0x12f32
leaq -0xb8(%rbp), %rdi
leaq -0x348(%rbp), %rsi
movq %rsi, -0x490(%rbp)
callq 0x10420
movq -0x490(%rbp), %rdi
callq 0x103f0
leaq -0x180(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x17670
jmp 0x12f74
leaq -0x1c0(%rbp), %rdi
leaq -0x3c8(%rbp), %rsi
callq 0x176b0
leaq -0x3c8(%rbp), %rdi
callq 0x176f0
leaq -0xb8(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x498(%rbp)
jmp 0x12fa8
movq -0x498(%rbp), %rsi
leaq 0x2d51a(%rip), %rdi     # 0x404d0
callq 0x17720
movb %dl, -0x4a1(%rbp)
movq %rax, -0x4a0(%rbp)
jmp 0x12fca
movb -0x4a1(%rbp), %al
movq -0x4a0(%rbp), %rcx
movq %rcx, -0x3d8(%rbp)
movb %al, -0x3d0(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xb8d0
movq %rax, -0x4b0(%rbp)
jmp 0x12ff9
movq -0x4b0(%rbp), %rsi
leaq -0x428(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x177a0
jmp 0x13015
leaq 0x2d4e4(%rip), %rdi     # 0x40500
leaq -0x428(%rbp), %rsi
callq 0x17770
movq %rax, -0x4b8(%rbp)
jmp 0x13031
movq -0x4b8(%rbp), %rax
movq %rax, -0x430(%rbp)
leaq -0x428(%rbp), %rdi
callq 0x177e0
jmp 0x1306d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
leaq -0x428(%rbp), %rdi
callq 0x177e0
jmp 0x130ad
movl $0x0, -0x2d4(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x176f0
leaq -0xb8(%rbp), %rdi
callq 0x103f0
movl -0x2d4(%rbp), %eax
testl %eax, %eax
je 0x1309d
jmp 0x1309b
jmp 0x1309f
jmp 0x1309f
leaq -0x30(%rbp), %rdi
callq 0x17140
jmp 0x12b65
leaq -0x1c0(%rbp), %rdi
callq 0x176f0
leaq -0xb8(%rbp), %rdi
callq 0x103f0
jmp 0x130d0
addq $0x4c0, %rsp            # imm = 0x4C0
popq %rbp
retq
movq -0x138(%rbp), %rdi
callq 0x2300
nopl (%rax)