// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdi, %rbx
movl $0x0, 0x8(%rdi)
movsd %xmm0, 0x10(%rdi)
testl %esi, %esi
je 0x22358
movq %rcx, %r12
movq %rdx, %r13
movl %esi, %ebp
movl %r8d, (%rsp)
movl %r9d, 0x4(%rsp)
movslq %esi, %r15
leaq (,%r15,4), %rdi
shlq $0x3, %r15
testl %esi, %esi
movq $-0x1, %rax
cmovsq %rax, %rdi
cmovsq %rax, %r15
callq 0x4030
movq %rax, %r14
movq %rax, 0x18(%rbx)
movq %r15, %rdi
callq 0x4030
movq %rax, 0x20(%rbx)
testl %ebp, %ebp
jle 0x22366
movl %ebp, %edx
xorl %esi, %esi
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl (%rsp), %r8d
movsd (%r12,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x222c7
jnp 0x222dd
movl (%r13,%rsi,4), %edi
movslq %ecx, %rcx
movl %edi, (%r14,%rcx,4)
movsd %xmm1, (%rax,%rcx,8)
incl %ecx
movl %ecx, 0x8(%rbx)
incq %rsi
cmpq %rsi, %rdx
jne 0x222b9
testl %r8d, %r8d
sete %dl
cmpl $0x2, %ecx
setge %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x22368
movl %ecx, %edx
movl $0x1, %esi
xorl %edi, %edi
movq %rdi, %r8
movl 0x4(%r14,%r8,4), %r10d
movl %r8d, %r9d
movl (%r14,%r9,4), %r11d
cmpl %r11d, %r10d
jge 0x2233b
movl %r10d, (%r14,%r9,4)
movl %r11d, 0x4(%r14,%r8,4)
movsd (%rax,%r9,8), %xmm0
movsd 0x8(%rax,%r8,8), %xmm1
movsd %xmm1, (%rax,%r9,8)
movsd %xmm0, 0x8(%rax,%r8,8)
leaq -0x1(%r8), %r9
incq %r8
cmpq $0x1, %r8
movq %r9, %r8
jg 0x22307
incq %rsi
incq %rdi
cmpq %rdx, %rsi
jne 0x22304
jmp 0x22368
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rbx)
jmp 0x2243f
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rsp)
sete %dl
cmpl $0x2, %ecx
setge %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x2243f
leaq 0x5a015(%rip), %rdx     # 0x7c3a0
movsd (%rdx), %xmm0
movapd 0x36cc9(%rip), %xmm1    # 0x59060
xorpd %xmm0, %xmm1
xorl %edx, %edx
movslq %edx, %rsi
movl (%r14,%rsi,4), %edi
cmpl 0x4(%r14,%rsi,4), %edi
jne 0x22432
leaq 0x1(%rsi), %r9
movsd (%rax,%r9,8), %xmm2
addsd (%rax,%rsi,8), %xmm2
movsd %xmm2, (%rax,%rsi,8)
ucomisd %xmm1, %xmm2
jb 0x223fc
ucomisd %xmm2, %xmm0
jb 0x223fc
addl $-0x2, %ecx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
cmpl %ecx, %edx
jge 0x2242d
movslq %ecx, %r8
movl 0x8(%r14,%rsi,4), %r9d
movl %r9d, (%r14,%rsi,4)
movsd 0x10(%rax,%rsi,8), %xmm2
movsd %xmm2, (%rax,%rsi,8)
incq %rsi
cmpq %rsi, %r8
jne 0x223de
jmp 0x2242d
leal -0x1(%rcx), %r8d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpl %r8d, %r9d
jge 0x2242a
addl $-0x2, %ecx
movl 0x8(%r14,%rsi,4), %r9d
movl %r9d, 0x4(%r14,%rsi,4)
movsd 0x10(%rax,%rsi,8), %xmm2
movsd %xmm2, 0x8(%rax,%rsi,8)
incq %rsi
cmpl %esi, %ecx
jne 0x2240d
movl %r8d, %ecx
movl %ecx, 0x8(%rbx)
addl %edi, %edx
incl %edx
leal -0x1(%rcx), %esi
cmpl %esi, %edx
jl 0x2239d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq