// File: /workspace/github_cpp_makefile_star2_O3/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 0x20d71
jp 0x20d71
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %ebx
testl %esi, %esi
movsd %xmm0, 0x8(%rsp)
je 0x20e55
movl %r9d, %ebp
movl %r8d, %r15d
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, 0x10(%rsp)
movl $0x28, %edi
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 0x22238
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21848
testq %rax, %rax
je 0x20ea8
movq %rax, %r15
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rax)
movq %r14, %rdi
callq 0x224ea
movl $0x28, %esi
movq %r14, %rdi
callq 0x4290
movsd 0x10(%r15), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20d62
jp 0x20d62
movq %rbx, %rdi
movq %r15, %rsi
callq 0x273a8
subq (%rbx), %rax
shrq $0x3, %rax
movq %rbx, %rdi
movl %eax, %esi
callq 0x27550
movq %r15, %rdi
callq 0x224ea
movl $0x28, %esi
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 0x20e68
movq (%rdi), %rax
movq (%rax), %r14
cmpl $0x0, 0x8(%r14)
je 0x20ebe
movq %rdi, %r14
movl $0x28, %edi
callq 0x4280
movq %rax, %rbx
movl $0x0, 0x8(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rax)
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x273c6
movq %r14, (%rbx)
jmp 0x20d62
movl 0xc(%rbx), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x273c6
movq %rbx, (%r14)
jmp 0x20d62
addsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
ucomisd %xmm1, %xmm0
jne 0x20d62
jp 0x20d62
movq %rdi, %rbx
movq %r14, %rsi
callq 0x273a8
subq (%rbx), %rax
shrq $0x3, %rax
movq %rbx, %rdi
movl %eax, %esi
callq 0x27550
movq %r14, %rdi
callq 0x224ea
movl $0x28, %esi
movq %r14, %rdi
jmp 0x20e42
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44c0
nop