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

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

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

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

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

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

    case RN_POW_OPERATION:
      if (operand1 == 0) {
        delete operand2;
      }
      else if (operand2->IsZero()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(1.0, 0);
        delete operand2;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(operand1, 0);
        delete operand2;
      }
      else {      
        Construct(op, new RNAlgebraic(operand1, 0), new RNAlgebraic(operand2), 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
movq %rdx, %rbx
movapd %xmm0, %xmm2
movl %esi, %ebp
movq %rdi, %r14
testl %ecx, %ecx
jne 0x20707
cmpl $0x1, 0xc(%rbx)
jne 0x20707
movq (%rbx), %rax
movq (%rax), %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x1ec74
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20707
jp 0x20707
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm1
movq %r14, %rdi
movl %ebp, %esi
movapd %xmm2, %xmm0
callq 0x20456
jmp 0x2096e
cmpl $0x6, %ebp
ja 0x207cb
movl %ebp, %eax
leaq 0x321a7(%rip), %rcx     # 0x528c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm0, %xmm0
movsd %xmm2, 0x8(%rsp)
ucomisd %xmm0, %xmm2
jne 0x20751
jp 0x20751
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x2096e
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
callq 0x1f02e
movq 0x20(%r14), %rdi
movsd 0x8(%rsp), %xmm0
jmp 0x20822
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x20784
jnp 0x20969
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x208ef
movl $0x1, 0x8(%r14)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r15
movsd 0x3085b(%rip), %xmm0    # 0x51010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x20965
leaq 0x3228b(%rip), %rdi     # 0x52a5d
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x264a8
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x2080f
jp 0x2080f
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x1ee0e
movsd 0x8(%rsp), %xmm2
testl %eax, %eax
jne 0x2096e
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movapd %xmm2, %xmm0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7a02
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x20843
jnp 0x20969
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x1ee0e
testl %eax, %eax
jne 0x20969
movq %rbx, %rdi
callq 0x1ee18
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x209c9
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)
jmp 0x2096e
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x208ac
jnp 0x20969
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x2092b
movl $0x1, 0x8(%r14)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r15
leaq 0x56ae0(%rip), %rax     # 0x773b8
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x20965
movq %rbx, %rdi
callq 0x1ee18
testl %eax, %eax
je 0x2098d
movl $0x1, 0x8(%r14)
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
jmp 0x20965
movq %rbx, %rdi
callq 0x1ee18
testl %eax, %eax
je 0x209e7
movl $0x1, 0x8(%r14)
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 0x20976
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
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20266
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x6
jmp 0x20a21
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 0x1f05a
pushq $0x28
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 0x20266
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x5
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
jmp 0x20a42
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
jmp 0x20a67
jmp 0x20a4f
movq %rax, %rbx
pushq $0x28
jmp 0x20a63
jmp 0x20a5e
jmp 0x20a5e
jmp 0x20a5e
jmp 0x20a5e
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44d0