// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RNMath/RNPolynomial.cpp
// Function: gaps::RNPolynomialTerm::RNPolynomialTerm(double, int, int const*, double const*, int, int)
RNPolynomialTerm::
RNPolynomialTerm(RNScalar _c, int _n, const int *_v, const RNScalar *_e, 
  RNBoolean already_sorted, RNBoolean already_unique)
  : n(0),
    c(_c)
{
  // Check number of variables
  if (_n == 0) {
#ifndef RN_POLYNOMIAL_TERM_STATIC_MEMORY
    v = NULL;
    e = NULL;
#endif
  }
  else {
    // Copy term info
    n = 0;
#ifndef RN_POLYNOMIAL_TERM_STATIC_MEMORY
    v = new int [ _n ];
    e = new RNScalar [ _n ];
#else
    // Check number of variables
    if (_n > max_variables) {
      RNFail("RNPolynomialTerm max variables exceeded\n");
      return;
    }
#endif
    for (int i = 0; i < _n; i++) {
      if (_e[i] != 0.0) {
        assert(n < _n);
        v[n] = _v[i];
        e[n] = _e[i];
        n++;
      }
    }

    // Sort variables
    if (!already_sorted) {
      for (int i = 1; i < n; i++) {
        for (int j = i; j > 0; j--) {
          if (v[j] < v[j-1]) {
            assert(j > 0);
            assert(j < _n);
            int vswap = v[j-1];
            v[j-1] = v[j];
            v[j] = vswap;
            double eswap = e[j-1];
            e[j-1] = e[j];
            e[j] = eswap;
          }
        }
      }
    }

    // Check for duplicate variables
    if (!already_unique) {
      for (int i = 0; i < n-1; i++) {
        assert(i >= 0);
        assert(n >= 0);
        assert(i < n-1);
        if (v[i] == v[i+1]) {
          assert(i < n-1);
          e[i] += e[i+1];
          if (RNIsZero(e[i])) {
            for (int j = i; j < n-2; j++) {
              assert(j >= 0);
              assert(j < n-2);
              v[j] = v[j+2];
              e[j] = e[j+2];
            }
            n -= 2;
            i -= 2;
          }
          else {
            for (int j = i+1; j < n-1; j++) {
              assert(j >= 0);
              assert(j < n-1);
              v[j] = v[j+1];
              e[j] = e[j+1];
            }
            n -= 1;
            i--;
          }
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
andl $0x0, 0x8(%rdi)
movsd %xmm0, 0x10(%rdi)
testl %esi, %esi
je 0x1feab
movq %rcx, %r12
movq %rdx, %r13
movl %r9d, 0xc(%rsp)
movslq %esi, %rdi
xorl %r15d, %r15d
testl %esi, %esi
movl $0x0, %ebp
cmovgl %esi, %ebp
leaq (,%rdi,8), %rcx
testl %esi, %esi
pushq $-0x1
popq %rax
cmovsq %rax, %rcx
movq %rcx, 0x10(%rsp)
shlq $0x2, %rdi
testl %esi, %esi
cmovsq %rax, %rdi
movl %r8d, 0x8(%rsp)
callq 0x4030
movq %rax, %r14
movq %rax, 0x18(%rbx)
movq 0x10(%rsp), %rdi
callq 0x4030
movl 0x8(%rsp), %esi
movq %rax, 0x20(%rbx)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
cmpq %r15, %rbp
je 0x1fdd8
movsd (%r12,%r15,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1fdbd
jnp 0x1fdd3
movl (%r13,%r15,4), %edx
movslq %ecx, %rcx
movl %edx, (%r14,%rcx,4)
movsd %xmm1, (%rax,%rcx,8)
incl %ecx
movl %ecx, 0x8(%rbx)
incq %r15
jmp 0x1fdaa
testl %esi, %esi
movl 0xc(%rsp), %r9d
je 0x1fec3
testl %r9d, %r9d
jne 0x1feb4
leaq 0x575b3(%rip), %rdx     # 0x773a8
movsd (%rdx), %xmm0
movapd 0x3125f(%rip), %xmm1    # 0x51060
xorpd %xmm0, %xmm1
xorl %edx, %edx
pushq $-0x1
popq %rsi
pushq $-0x2
popq %rdi
leal -0x1(%rcx), %r8d
cmpl %r8d, %edx
jge 0x1feb4
movslq %edx, %r9
movl (%r14,%r9,4), %r10d
cmpl 0x4(%r14,%r9,4), %r10d
jne 0x1fea4
leaq 0x1(%r9), %r10
movsd (%rax,%r10,8), %xmm2
addsd (%rax,%r9,8), %xmm2
movsd %xmm2, (%rax,%r9,8)
ucomisd %xmm1, %xmm2
jb 0x1fe70
ucomisd %xmm2, %xmm0
jb 0x1fe70
addl $-0x2, %ecx
movslq %ecx, %r8
cmpq %r8, %r9
jge 0x1fe9b
movl 0x8(%r14,%r9,4), %r10d
movl %r10d, (%r14,%r9,4)
movsd 0x10(%rax,%r9,8), %xmm2
movsd %xmm2, (%rax,%r9,8)
incq %r9
jmp 0x1fe50
movslq %r8d, %rcx
cmpq %rcx, %r10
jge 0x1fe93
movl 0x4(%r14,%r10,4), %r9d
movl %r9d, (%r14,%r10,4)
movsd 0x8(%rax,%r10,8), %xmm2
movsd %xmm2, (%rax,%r10,8)
incq %r10
jmp 0x1fe73
movl %r8d, %ecx
movl %esi, %r8d
jmp 0x1fe9e
movl %edi, %r8d
movl %ecx, 0x8(%rbx)
addl %r8d, %edx
incl %edx
jmp 0x1fe0d
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdx
cmpl %edx, %ecx
jle 0x1fde5
movq %rdx, %rsi
testq %rsi, %rsi
jle 0x1ff03
movl -0x4(%r14,%rsi,4), %edi
movl (%r14,%rsi,4), %r8d
cmpl %edi, %r8d
jge 0x1fefe
movl %r8d, -0x4(%r14,%rsi,4)
movl %edi, (%r14,%rsi,4)
movupd -0x8(%rax,%rsi,8), %xmm0
shufpd $0x1, %xmm0, %xmm0      # xmm0 = xmm0[1,0]
movupd %xmm0, -0x8(%rax,%rsi,8)
decq %rsi
jmp 0x1fed1
incq %rdx
jmp 0x1fec6