// File: 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:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movl %esi, 0x54(%rsp)
movq %rdx, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movl %r8d, 0x3c(%rsp)
movl %r9d, 0x38(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x8(%rax)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
cmpl $0x0, 0x54(%rsp)
jne 0x3ef74
movq 0x8(%rsp), %rax
movq $0x0, 0x18(%rax)
movq $0x0, 0x20(%rax)
jmp 0x3f34a
movq 0x8(%rsp), %rax
movl $0x0, 0x8(%rax)
movslq 0x54(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
movslq 0x54(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x3f063
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3f012
jp 0x3f012
jmp 0x3f054
movq 0x8(%rsp), %rax
movq 0x48(%rsp), %rcx
movslq 0x34(%rsp), %rdx
movl (%rcx,%rdx,4), %esi
movq 0x18(%rax), %rcx
movslq 0x8(%rax), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x40(%rsp), %rcx
movslq 0x34(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rcx
movslq 0x8(%rax), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x3f056
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x3efec
cmpl $0x0, 0x3c(%rsp)
jne 0x3f17e
movl $0x1, 0x30(%rsp)
movq 0x8(%rsp), %rcx
movl 0x30(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x3f17c
movl 0x30(%rsp), %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jle 0x3f16a
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
movslq 0x2c(%rsp), %rdx
movl (%rax,%rdx,4), %eax
movq 0x18(%rcx), %rcx
movl 0x2c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x3f158
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
movl 0x2c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, 0x28(%rsp)
movq 0x18(%rax), %rcx
movslq 0x2c(%rsp), %rdx
movl (%rcx,%rdx,4), %esi
movq 0x18(%rax), %rcx
movl 0x2c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movl %esi, (%rcx,%rdx,4)
movl 0x28(%rsp), %esi
movq 0x18(%rax), %rcx
movslq 0x2c(%rsp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x20(%rax), %rcx
movl 0x2c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x20(%rax), %rcx
movslq 0x2c(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rcx
movl 0x2c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsd %xmm0, (%rcx,%rdx,8)
movsd 0x20(%rsp), %xmm0
movq 0x20(%rax), %rax
movslq 0x2c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x3f15a
movl 0x2c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3f090
jmp 0x3f16c
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x3f076
jmp 0x3f17e
cmpl $0x0, 0x38(%rsp)
jne 0x3f348
movl $0x0, 0x1c(%rsp)
movq 0x8(%rsp), %rcx
movl 0x1c(%rsp), %eax
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3f346
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
movslq 0x1c(%rsp), %rdx
movl (%rax,%rdx,4), %eax
movq 0x18(%rcx), %rcx
movl 0x1c(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x3f334
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rcx
movl 0x1c(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rcx
movslq 0x1c(%rsp), %rdx
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x8b0c8(%rip), %rax     # 0xca2d8
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x3f2ab
movl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x8(%rsp), %rcx
movl 0x18(%rsp), %eax
movl 0x8(%rcx), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jge 0x3f28d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
movl 0x18(%rsp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %esi
movq 0x18(%rax), %rcx
movslq 0x18(%rsp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x20(%rax), %rcx
movl 0x18(%rsp), %edx
addl $0x2, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rax
movslq 0x18(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x3f22a
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
subl $0x2, %ecx
movl %ecx, 0x8(%rax)
movl 0x1c(%rsp), %eax
subl $0x2, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3f332
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3f319
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
movl 0x14(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %esi
movq 0x18(%rax), %rcx
movslq 0x14(%rsp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x20(%rax), %rcx
movl 0x14(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rax
movslq 0x14(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x3f2b6
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x8(%rax)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3f334
jmp 0x3f336
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3f191
jmp 0x3f348
jmp 0x3f34a
addq $0x68, %rsp
retq
nop