// File: /workspace/github_cpp_makefile_star2_O3/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 0x231a3
cmpl $0x1, 0xc(%rbx)
jne 0x231a3
movq (%rbx), %rax
movq (%rax), %rdi
movapd %xmm1, (%rsp)
callq 0x20fda
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd (%rsp), %xmm1
jne 0x231a3
jp 0x231a3
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r14, %rdi
movl %ebp, %esi
callq 0x229d8
jmp 0x233dc
cmpl $0x6, %ebp
ja 0x23267
movl %ebp, %eax
leaq 0x3757b(%rip), %rcx     # 0x5a730
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
movapd %xmm1, (%rsp)
callq 0x211b4
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x231f1
jp 0x231f1
testl %eax, %eax
jne 0x233dc
movl $0x1, 0x8(%r14)
xorpd 0x35e5f(%rip), %xmm0    # 0x59060
jmp 0x232b1
movapd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x233d7
xorpd %xmm0, %xmm0
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2337b
jp 0x2337b
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movsd 0x35dbf(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x233d3
leaq 0x37643(%rip), %rdi     # 0x5a8b1
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x29356
movq %rbx, %rdi
movapd %xmm1, (%rsp)
callq 0x211b4
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x232a9
jp 0x232a9
testl %eax, %eax
jne 0x233dc
movl $0x1, 0x8(%r14)
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 0x20d48
movq %rbx, %rdi
movapd %xmm1, (%rsp)
callq 0x211b4
movapd (%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x232fa
jnp 0x233dc
testl %eax, %eax
jne 0x233dc
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 0x21442
movapd %xmm1, (%rsp)
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x233d7
xorpd %xmm0, %xmm0
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x23398
jp 0x23398
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
leaq 0x5904c(%rip), %rax     # 0x7c3b0
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x233d3
ucomisd 0x35c8d(%rip), %xmm1    # 0x59010
jne 0x233fd
jp 0x233fd
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
jmp 0x231be
movq %rbx, %rdi
callq 0x211be
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x2345d
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movapd (%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
movq %r15, 0x20(%r14)
testq %rbx, %rbx
je 0x233e4
movq %rbx, %rdi
callq 0x20f24
movl $0x10, %esi
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4290
movl $0x28, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movapd (%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x227be
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 0x23c2c
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movaps (%rsp), %xmm0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2147c
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x2349f
movq %rax, %rbx
movl $0x28, %esi
jmp 0x2349c
jmp 0x23494
jmp 0x23494
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44c0