// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movapd %xmm0, %xmm1
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
testl %ecx, %ecx
jne 0x232fd
cmpl $0x1, 0xc(%rbx)
jne 0x232fd
movq (%rbx), %rax
movq (%rax), %rdi
movapd %xmm1, (%rsp)
callq 0x210ca
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd (%rsp), %xmm1
jne 0x232fd
jp 0x232fd
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r14, %rdi
movl %ebp, %esi
callq 0x22b24
jmp 0x2352b
cmpl $0x6, %ebp
ja 0x23406
movl %ebp, %eax
leaq 0x37411(%rip), %rcx     # 0x5a720
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movapd %xmm1, (%rsp)
callq 0x2129e
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2333e
jp 0x2333e
testl %eax, %eax
jne 0x2352b
movl $0x1, 0x8(%r14)
jmp 0x2337e
movq %rbx, %rdi
movapd %xmm1, (%rsp)
callq 0x2129e
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2336e
jp 0x2336e
testl %eax, %eax
jne 0x2352b
movl $0x1, 0x8(%r14)
xorpd 0x35ce2(%rip), %xmm0    # 0x59060
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x20e38
movapd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23526
xorpd %xmm0, %xmm0
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x234c5
jp 0x234c5
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movsd 0x35c20(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x23522
leaq 0x37494(%rip), %rdi     # 0x5a8a1
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x297ae
movq %rbx, %rdi
movapd %xmm1, (%rsp)
callq 0x2129e
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x23444
jnp 0x2352b
testl %eax, %eax
jne 0x2352b
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x21548
movapd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23526
xorpd %xmm0, %xmm0
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x234e7
jp 0x234e7
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
leaq 0x58efa(%rip), %rax     # 0x7c3a8
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x23522
ucomisd 0x35b43(%rip), %xmm1    # 0x59010
jne 0x23559
jp 0x23559
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
jmp 0x2354c
movq %rbx, %rdi
callq 0x212a8
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x235b9
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movapd (%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
movq %r15, 0x20(%r14)
testq %rbx, %rbx
je 0x2354c
movq %rbx, %rdi
callq 0x21014
movl $0x10, %esi
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4280
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movapd (%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2290a
movq %r14, %rdi
movl $0x6, %esi
movq %r15, %rdx
movq %r12, %rcx
movl $0x1, %r8d
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x23dbe
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movaps (%rsp), %xmm0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x21582
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x235fb
movq %rax, %rbx
movl $0x28, %esi
jmp 0x235f8
jmp 0x235f0
jmp 0x235f0
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x44b0