// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RNMath/RNPolynomial.cpp
// Function: gaps::RNPolynomial::AddTerm(double, int, int const*, double const*, int, int)
void RNPolynomial::
AddTerm(RNScalar c, int n, const int *v, const RNScalar *e, 
  RNBoolean already_sorted, RNBoolean already_unique)
{
  // Check coefficient
  if (c == 0) return;

  // Check everything else
  assert((n == 0) || ((v != NULL) && (e != NULL)));

  // Check if term is constant (handle separately for efficiency)
  if (n == 0) {
    // Add constant term
    if (NTerms() > 0) {
      // Check for existing constant term
      RNPolynomialTerm *match = Term(0);
      if (match->IsConstant()) {
        // Add constant to existing constant term
        match->c += c;
        if (match->c == 0.0) {
          // Remove unneeded term
          terms.Remove(match);
          delete match;
        }
      }
      else {
        // Create new constant term (put first in list)
        RNPolynomialTerm *term = new RNPolynomialTerm(c, 0, NULL, NULL, TRUE, TRUE);
        terms.InsertHead(term);
        term->polynomial = this; 
      }
    }
    else {
      // Create new constant term 
      RNPolynomialTerm *term = new RNPolynomialTerm(c, 0, NULL, NULL, TRUE, TRUE);
      terms.InsertHead(term);
      term->polynomial = this; 
    }
  }
  else {
    // Add variable term
    RNPolynomialTerm *term = new RNPolynomialTerm(c, n, v, e, already_sorted, already_unique);
    RNPolynomialTerm *match = FindTermWithSameVariables(term);
    if (match) { 
      // Update existing term
      match->c += c;
      delete term; 
      if (match->c == 0.0) {
        // Remove unneeded term
        terms.Remove(match);
        delete match;
      }
    }
    else { 
      // Insert new term
      terms.Insert(term);
      term->polynomial = this; 
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1ea18
jnp 0x1eb67
movl %esi, %ebx
testl %esi, %esi
movsd %xmm0, 0x8(%rsp)
je 0x1eae5
movl %r9d, %ebp
movl %r8d, %r15d
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, 0x10(%rsp)
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
movl %ebx, %esi
movq %r13, %rdx
movq %r12, %rcx
movl %r15d, %r8d
movl %ebp, %r9d
callq 0x1fd24
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1f3be
testq %rax, %rax
je 0x1eb23
movq %rax, %r15
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rax)
movq %r14, %rdi
callq 0x1ffb0
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x4290
movsd 0x10(%r15), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1eb67
jp 0x1eb67
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2001e
movq %r15, %rdi
callq 0x1ffb0
pushq $0x28
popq %rsi
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4290
cmpl $0x0, 0xc(%rdi)
jle 0x1eb30
movq (%rdi), %rax
movq (%rax), %r14
cmpl $0x0, 0x8(%r14)
je 0x1eb76
movq %rdi, %rbx
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
pushq $0x1
popq %r8
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r8d, %r9d
callq 0x1fd24
jmp 0x1eb59
movq %rbx, %rdi
movq %r14, %rsi
callq 0x11a32
jmp 0x1eb64
movq %rdi, %rbx
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
pushq $0x1
popq %r8
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r8d, %r9d
callq 0x1fd24
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20040
movq %rbx, (%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
ucomisd %xmm1, %xmm0
jne 0x1eb67
jp 0x1eb67
movq %r14, %rsi
callq 0x2001e
movq %r14, %rdi
callq 0x1ffb0
pushq $0x28
popq %rsi
movq %r14, %rdi
jmp 0x1ead2
jmp 0x1eba9
jmp 0x1eba9
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44d0
nop