// File: /workspace/github_cpp_makefile_star2_O1/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 0x22575
movq %rcx, %r12
movq %rdx, %r13
movl %esi, %ebp
movl %r9d, 0x4(%rsp)
movl $0x0, 0x8(%rbx)
movslq %esi, %r15
leaq (,%r15,4), %rdi
shlq $0x3, %r15
testl %esi, %esi
movq $-0x1, %rax
cmovsq %rax, %rdi
cmovsq %rax, %r15
movl %r8d, (%rsp)
callq 0x4030
movq %rax, %r14
movq %rax, 0x18(%rbx)
movq %r15, %rdi
callq 0x4030
movl (%rsp), %r8d
movq %rax, 0x20(%rbx)
testl %ebp, %ebp
jle 0x22437
movl 0x8(%rbx), %ecx
movl %ebp, %edx
xorl %esi, %esi
xorpd %xmm0, %xmm0
movsd (%r12,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x22419
jnp 0x2242f
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 0x2240b
testl %r8d, %r8d
jne 0x2249d
movslq 0x8(%rbx), %rcx
cmpq $0x2, %rcx
jl 0x2249d
movl $0x1, %edx
xorl %esi, %esi
movq %rsi, %rdi
movl 0x4(%r14,%rdi,4), %r9d
movl %edi, %r8d
movl (%r14,%r8,4), %r10d
cmpl %r10d, %r9d
jge 0x22482
movl %r9d, (%r14,%r8,4)
movl %r10d, 0x4(%r14,%rdi,4)
movsd (%rax,%r8,8), %xmm0
movsd 0x8(%rax,%rdi,8), %xmm1
movsd %xmm1, (%rax,%r8,8)
movsd %xmm0, 0x8(%rax,%rdi,8)
leaq -0x1(%rdi), %r8
incq %rdi
cmpq $0x1, %rdi
movq %r8, %rdi
jg 0x22450
incq %rdx
incq %rsi
cmpq %rcx, %rdx
jne 0x2244d
cmpl $0x0, 0x4(%rsp)
jne 0x2257e
movl 0x8(%rbx), %ecx
cmpl $0x2, %ecx
jl 0x2257e
leaq 0x59edd(%rip), %rdx     # 0x7c398
movsd (%rdx), %xmm0
movapd 0x36b99(%rip), %xmm1    # 0x59060
xorpd %xmm0, %xmm1
xorl %edx, %edx
movslq %edx, %rdi
movl (%r14,%rdi,4), %esi
cmpl 0x4(%r14,%rdi,4), %esi
jne 0x22566
leaq 0x1(%rdi), %r8
movsd (%rax,%r8,8), %xmm2
addsd (%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
ucomisd %xmm1, %xmm2
jb 0x2252c
ucomisd %xmm2, %xmm0
jb 0x2252c
addl $-0x2, %ecx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
cmpl %ecx, %edx
jge 0x22561
movslq %ecx, %r8
movl 0x8(%r14,%rdi,4), %r9d
movl %r9d, (%r14,%rdi,4)
movsd 0x10(%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %r8
jne 0x2250e
jmp 0x22561
leal -0x1(%rcx), %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpl %edi, %r8d
jge 0x2255f
movslq %edx, %r8
addl $-0x2, %ecx
movl 0x8(%r14,%r8,4), %r9d
movl %r9d, 0x4(%r14,%r8,4)
movsd 0x10(%rax,%r8,8), %xmm2
movsd %xmm2, 0x8(%rax,%r8,8)
incq %r8
cmpl %r8d, %ecx
jne 0x2253f
movl %edi, %ecx
movl %ecx, 0x8(%rbx)
addl %esi, %edx
incl %edx
leal -0x1(%rcx), %esi
cmpl %esi, %edx
jl 0x224cd
jmp 0x2257e
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop