// File: /workspace/github_cpp_makefile_star2_O2/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 0x20bbd
cmpl $0x1, 0xc(%rbx)
jne 0x20bbd
movq (%rbx), %rax
movq (%rax), %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1ec74
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd 0x8(%rsp), %xmm1
jne 0x20bbd
jp 0x20bbd
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r14, %rdi
movl %ebp, %esi
callq 0x20456
jmp 0x20df7
cmpl $0x6, %ebp
ja 0x20c8e
movl %ebp, %eax
leaq 0x31d0d(%rip), %rcx     # 0x528dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1ee0e
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20c0d
jp 0x20c0d
testl %eax, %eax
jne 0x20df7
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 0x7966
movsd %xmm1, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x20df2
xorpd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x20d97
jp 0x20d97
movl $0x1, 0x8(%r14)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r15
movsd 0x30398(%rip), %xmm0    # 0x51010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x20dee
leaq 0x31dc8(%rip), %rdi     # 0x52a5d
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x264a8
movq %rbx, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1ee0e
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20cd2
jp 0x20cd2
testl %eax, %eax
jne 0x20df7
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 0x7a02
movq %rbx, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x1ee0e
movsd 0x8(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20d16
jnp 0x20df7
testl %eax, %eax
jne 0x20df7
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 0x1f05a
movsd %xmm1, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x20df2
xorpd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x20db4
jp 0x20db4
movl $0x1, 0x8(%r14)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r15
leaq 0x56638(%rip), %rax     # 0x773b8
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x20dee
ucomisd 0x30271(%rip), %xmm1    # 0x51010
jne 0x20e16
jp 0x20e16
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
jmp 0x20bd8
movq %rbx, %rdi
callq 0x1ee18
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x20e6f
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
movq %r15, 0x20(%r14)
testq %rbx, %rbx
je 0x20dff
movq %rbx, %rdi
callq 0x1ebc0
pushq $0x10
popq %rsi
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4290
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20266
pushq $0x6
popq %rsi
pushq $0x1
popq %r8
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x215d6
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1f096
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
jmp 0x20eac
movq %rax, %rbx
pushq $0x28
jmp 0x20ea8
jmp 0x20ea3
jmp 0x20ea3
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44d0
nop