// File: manschga[P]M5GB/poly.cpp
// Function: reduce(polynomial&, polynomial const&, bool&, int&)
void reduce(polynomial &G_i, const polynomial &G_j, bool &changed, int& reductions)
{
  term_vec LT_j_vec = table[G_j.Get_LT()].first;
  for (int terms = 0; terms < G_i.size(); ++terms)
    {
      monomial monom = G_i.Get_monomials()[terms];
      int coeff = monom.Get_coeff();
      vector<int> monom_vec = table[monom.Get_term()].first;
      if (isDivisible(monom_vec,LT_j_vec))
	{
	  changed = true;
	  reductions ++;
	  /*
	  for (int i = 0; i < monom_vec.size(); ++i)
	    {
	      cout << monom_vec[i] << ",";
	    }
	  cout << "LT_G_j " << endl;
	  for (int i = 0; i < LT_j_vec.size(); ++i)
	    {
	      cout << LT_j_vec[i] << ",";
	    }
	  cout << endl;
	  changed = true;
	  /*
	  cout << "G_i before: " << G_i << endl << endl;
	  cout << "G_j: " << polynomial({monomial(get_remainder(monom.Get_term(),G_j.Get_LT()),monom.Get_coeff()*Inverse(G_j.Get_LC(),monom.Get_p()),monom.Get_p())}) << endl << endl;
	  */
	  polynomial vG_j = G_j.Multiply(get_remainder(monom_vec,LT_j_vec)); //term multi
	  //vG_j = G_j.Multiply(monom.Get_coeff()*Inverse(G_j.Get_LC(),monom.Get_p()),true,table);//constant multi
	  int c = coeff*Inverse(vG_j.Get_LC(),vG_j.Get_p());
	  /*


	  cout << "G_i: " << G_i << endl;
	  */
	  G_i = G_i - vG_j.Multiply(c,true);
	  //cout << "G_i after : " << G_i<< endl << endl;
	}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x17490
movslq %eax, %rsi
leaq 0x195b2(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x5ff0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x138(%rbp)
movq -0x8(%rbp), %rdi
callq 0x17120
movl %eax, -0x134(%rbp)
jmp 0x26c60
movl -0x138(%rbp), %eax
movl -0x134(%rbp), %ecx
cmpl %ecx, %eax
jge 0x26f4f
movq -0x8(%rbp), %rdi
callq 0x31f0
movq %rax, -0x140(%rbp)
jmp 0x26c86
movq -0x140(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x3200
movups (%rax), %xmm0
movaps %xmm0, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x3220
movl %eax, -0x144(%rbp)
jmp 0x26cae
movl -0x144(%rbp), %eax
movl %eax, -0x64(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xda60
movq %rax, -0x150(%rbp)
jmp 0x26cc9
movq -0x150(%rbp), %rsi
leaq 0x19509(%rip), %rdi     # 0x401e0
callq 0xd480
movq %rax, %rsi
leaq -0x80(%rbp), %rdi
callq 0x5ff0
jmp 0x26cea
leaq -0x80(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5880
movb %al, -0x151(%rbp)
jmp 0x26cff
movb -0x151(%rbp), %al
testb $0x1, %al
jne 0x26d0e
jmp 0x26f2d
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x5ff0
jmp 0x26d3c
leaq -0xe8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5ff0
jmp 0x26d4e
leaq -0xb8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x262f0
jmp 0x26d6a
movq -0x160(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0xcfc0
jmp 0x26d86
leaq -0xb8(%rbp), %rdi
callq 0x3b70
leaq -0xe8(%rbp), %rdi
callq 0x3b70
leaq -0xd0(%rbp), %rdi
callq 0x3b70
movl -0x64(%rbp), %eax
movl %eax, -0x168(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xe7c0
movl %eax, -0x164(%rbp)
jmp 0x26dc7
leaq -0xa0(%rbp), %rdi
callq 0xb890
movl %eax, -0x16c(%rbp)
jmp 0x26ddb
movl -0x16c(%rbp), %esi
movl -0x164(%rbp), %edi
callq 0x5000
movl %eax, -0x170(%rbp)
jmp 0x26df4
movl -0x170(%rbp), %ecx
movl -0x168(%rbp), %eax
imull %ecx, %eax
movl %eax, -0xec(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
movl -0xec(%rbp), %edx
leaq -0x130(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x1, %ecx
callq 0xd4a0
jmp 0x26e34
movq -0x178(%rbp), %rsi
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq 0xd790
jmp 0x26e50
movq -0x8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xb810
leaq -0x110(%rbp), %rdi
callq 0x44a0
leaq -0x130(%rbp), %rdi
callq 0x44a0
leaq -0xa0(%rbp), %rdi
callq 0x44a0
jmp 0x26f2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26f61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26f44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26eeb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26edf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x3b70
leaq -0xe8(%rbp), %rdi
callq 0x3b70
leaq -0xd0(%rbp), %rdi
callq 0x3b70
jmp 0x26f44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x26f1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x44a0
leaq -0xa0(%rbp), %rdi
callq 0x44a0
jmp 0x26f44
leaq -0x80(%rbp), %rdi
callq 0x3b70
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x26c46
leaq -0x80(%rbp), %rdi
callq 0x3b70
jmp 0x26f61
leaq -0x38(%rbp), %rdi
callq 0x3b70
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x3b70
movq -0x48(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)