// File: tomfunkhouser[P]gaps/pkgs/RNMath/RNAlgebraic.cpp
// Function: gaps::RNAlgebraic::Construct(int, gaps::RNPolynomial*, double, int)
void RNAlgebraic::
Construct(int op, RNPolynomial *operand1, RNScalar operand2, RNBoolean force)
{
  // NOTE: THIS TAKES OVER MANAGEMENT OF MEMORY FOR OPERAND1
  // SPECIFICALLY, OPERANDS WILL BE DELETED WHEN ALGEBRAIC IS DECONSTRUCTED
  assert(operand1);

  // Check type of operand1
  if (!force && (operand1->NTerms() == 1) && (operand1->Term(0)->Degree() == 0)) {
    Construct(op, operand1->Term(0)->Coefficient(), operand2);
    delete operand1;
  }
  else {
    switch(op) {
    case RN_ZERO_OPERATION:
      break;

    case RN_ADD_OPERATION:
      if ((operand1->IsZero()) && (operand2 == 0)) {
        delete operand1;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Add(operand2);
      }
      break; 

    case RN_SUBTRACT_OPERATION: 
      if ((operand1->IsZero()) && (operand2 == 0)) {
        delete operand1;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Subtract(operand2);
      }
      break; 

    case RN_MULTIPLY_OPERATION: 
      if ((operand1->IsZero()) || (operand2 == 0)) {
        delete operand1;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Multiply(operand2);
      }
      break; 

    case RN_DIVIDE_OPERATION:
      if (operand1->IsZero()) {
        delete operand1;
      }
      else if (operand2 == 0) {
        operation = RN_POLYNOMIAL_OPERATION;
        polynomial = new RNPolynomial(RN_INFINITY, 0);
        delete operand1;
      }  
      else if (operand1->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(operand2, 0);
        delete operand1;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Divide(operand2);
      }
      break;

    case RN_POW_OPERATION:
      if (operand1->IsZero()) {
        delete operand1;
      }
      else if (operand2 == 0) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(1.0, 0);
        delete operand1;
      }
      else if (operand2 == 1.0) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
      }
      else {      
        Construct(op, new RNAlgebraic(operand1), new RNAlgebraic(operand2, 0), TRUE);
      }
      break;

    default:
      RNAbort("Invalid operation %d\n", op);
      break;
    }
  }

  // Just checking
  assert(IsValid());
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movl %esi, 0xbc(%rsp)
movq %rdx, 0xb0(%rsp)
movsd %xmm0, 0xa8(%rsp)
movl %ecx, 0xa4(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpl $0x0, 0xa4(%rsp)
jne 0x411fc
movq 0xb0(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x411fc
movq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3cc40
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x411fc
jp 0x411fc
movl 0xbc(%rsp), %eax
movl %eax, 0x7c(%rsp)
movq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3f6a0
movq 0x88(%rsp), %rdi
movl 0x7c(%rsp), %esi
movsd 0xa8(%rsp), %xmm1
xorl %edx, %edx
callq 0x40080
movq 0xb0(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, %rax
je 0x411f7
movq 0x80(%rsp), %rdi
callq 0x3cac0
movq 0x80(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x417e4
movl 0xbc(%rsp), %eax
movq %rax, 0x70(%rsp)
subq $0x6, %rax
ja 0x417cd
movq 0x70(%rsp), %rax
leaq 0x6b65e(%rip), %rcx     # 0xac87c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x417e2
movq 0xb0(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41280
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x41280
jp 0x41280
movq 0xb0(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x4127e
movq 0x68(%rsp), %rdi
callq 0x3cac0
movq 0x68(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x412ad
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xa8(%rsp), %xmm0
callq 0xb6c0
jmp 0x417e2
movq 0xb0(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41306
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x41306
jp 0x41306
movq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, %rax
je 0x41304
movq 0x60(%rsp), %rdi
callq 0x3cac0
movq 0x60(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41333
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xa8(%rsp), %xmm0
callq 0xb4a0
jmp 0x417e2
movq 0xb0(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
jne 0x4135e
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4138c
jp 0x4138c
movq 0xb0(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x4138a
movq 0x58(%rsp), %rdi
callq 0x3cac0
movq 0x58(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x413b9
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xa8(%rsp), %xmm0
callq 0x3d4e0
jmp 0x417e2
movq 0xb0(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41401
movq 0xb0(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x413fc
movq 0x50(%rsp), %rdi
callq 0x3cac0
movq 0x50(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x415b7
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x414c9
jp 0x414c9
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x48(%rsp)
leaq 0x88ea0(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x4145f
movq 0x88(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x4149c
movq 0x40(%rsp), %rdi
callq 0x3cac0
movq 0x40(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x415b5
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x417ec
movq 0xb0(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x41586
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movsd 0xa8(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x4151f
movq 0x88(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x4155c
movq 0x30(%rsp), %rdi
callq 0x3cac0
movq 0x30(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x415b3
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x417ec
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xa8(%rsp), %xmm0
callq 0x3d580
jmp 0x415b5
jmp 0x415b7
jmp 0x417e2
movq 0xb0(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x415ff
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x415fa
movq 0x28(%rsp), %rdi
callq 0x3cac0
movq 0x28(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x417cb
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x416c4
jp 0x416c4
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
xorl %r8d, %r8d
movl %r8d, %ecx
movsd 0x699d3(%rip), %xmm0    # 0xab020
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x4165a
movq 0x88(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x41697
movq 0x18(%rsp), %rdi
callq 0x3cac0
movq 0x18(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x417c9
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x417ec
movsd 0xa8(%rsp), %xmm0
movsd 0x6994b(%rip), %xmm1    # 0xab020
ucomisd %xmm1, %xmm0
jne 0x416fd
jp 0x416fd
movq 0x88(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xb0(%rsp), %rcx
movq %rcx, 0x20(%rax)
jmp 0x417c7
movl 0xbc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movq 0xb0(%rsp), %rsi
callq 0x3f8b0
jmp 0x4172c
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movsd 0xa8(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3fcb0
jmp 0x4175c
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
movl 0xc(%rsp), %esi
movq 0x88(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x417c7
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x417ec
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x417ec
jmp 0x417c9
jmp 0x417cb
jmp 0x417e2
movl 0xbc(%rsp), %esi
leaq 0x6b222(%rip), %rdi     # 0xac9fd
movb $0x0, %al
callq 0x4c6e0
jmp 0x417e4
addq $0xc8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x44b0
nopl (%rax)