// File: /workspace/github_cpp_makefile_star2_O1/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 0x22def
cmpl $0x1, 0xc(%rbx)
jne 0x22def
movq (%rbx), %rax
movq (%rax), %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x210ca
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x22def
jp 0x22def
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm1
movq %r14, %rdi
movl %ebp, %esi
movapd %xmm2, %xmm0
callq 0x22b24
jmp 0x2305f
cmpl $0x6, %ebp
ja 0x22f00
movl %ebp, %eax
leaq 0x37903(%rip), %rcx     # 0x5a704
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22e32
jp 0x22e32
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x2129e
movsd 0x8(%rsp), %xmm2
testl %eax, %eax
jne 0x2305f
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movapd %xmm2, %xmm0
jmp 0x22e87
xorpd %xmm0, %xmm0
movsd %xmm2, 0x8(%rsp)
ucomisd %xmm0, %xmm2
jne 0x22e69
jp 0x22e69
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x2305f
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
callq 0x2151a
movq 0x20(%r14), %rdi
movsd 0x8(%rsp), %xmm0
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
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22eb7
jnp 0x2305a
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
je 0x22fdc
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movsd 0x36126(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x23056
leaq 0x3799a(%rip), %rdi     # 0x5a8a1
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x297ae
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22f2c
jnp 0x2305a
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x2129e
testl %eax, %eax
jne 0x2305a
movq %rbx, %rdi
callq 0x212a8
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x230d3
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
movq %r15, 0x20(%r14)
jmp 0x2305f
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22f97
jnp 0x2305a
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
je 0x2301a
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
leaq 0x593e3(%rip), %rax     # 0x7c3a8
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x23056
movq %rbx, %rdi
callq 0x212a8
testl %eax, %eax
je 0x2308d
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x23056
movq %rbx, %rdi
callq 0x212a8
testl %eax, %eax
je 0x230f1
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
movq %r15, 0x20(%r14)
testq %rbx, %rbx
je 0x23080
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
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2290a
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movq %r14, %rdi
movl $0x6, %esi
jmp 0x23135
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 0x21548
movl $0x28, %edi
callq 0x4270
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2290a
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movq %r14, %rdi
movl $0x5, %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
jmp 0x23154
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x23180
jmp 0x23163
movq %rax, %rbx
movl $0x28, %esi
jmp 0x2317d
jmp 0x23175
jmp 0x23175
jmp 0x23175
jmp 0x23175
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x44b0
nop