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

  // Check types of operands
  if (!force && (operand1->NTerms() == 1) && (operand1->Term(0)->Degree() == 0)) {
    Construct(op, operand1->Term(0)->Coefficient(), operand2);
    delete operand1;
  }
  else 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->IsZero() && operand2->IsZero()) {
        delete operand1;
        delete operand2;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Add(*operand2);
        delete operand2;
      }
      break; 

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

    case RN_MULTIPLY_OPERATION: 
      if (operand1->IsZero() || operand2->IsZero()) {
        delete operand1;
        delete operand2;
      }
      else if (operand1->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand2;
        delete operand1;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        delete operand2;
      }
      else if ((operand1->NTerms() == 1) && (operand2->NTerms() == 1)) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Multiply(*operand2);
        delete operand2;
      }
      else {
        Construct(op, new RNAlgebraic(operand1), new RNAlgebraic(operand2), TRUE);
      }
      break; 

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

    case RN_POW_OPERATION:
      if (operand1->IsZero()) {
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsZero()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(1.0, 0);
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        delete operand2;
      }
      else {      
        Construct(op, new RNAlgebraic(operand1), 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
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
testl %r8d, %r8d
je 0x2369b
cmpl $0x6, %ebp
ja 0x237bd
movl %ebp, %eax
leaq 0x370e2(%rip), %rcx     # 0x5a73c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x2129e
testl %eax, %eax
je 0x2367f
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23750
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x215bc
jmp 0x238a2
cmpl $0x1, 0xc(%r14)
jne 0x236e8
movq (%r14), %rax
movq (%rax), %rdi
callq 0x210ca
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x236e8
jp 0x236e8
movq (%r14), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x22d8c
movq %r14, %rdi
callq 0x21014
movl $0x10, %esi
movq %r14, %rdi
jmp 0x238b2
cmpl $0x1, 0xc(%rbx)
jne 0x23648
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x210ca
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x23648
jp 0x23648
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x232a0
jmp 0x238a2
movq %r14, %rdi
callq 0x2129e
testl %eax, %eax
je 0x2388b
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
je 0x2388b
movq %r14, %rdi
callq 0x21014
movl $0x10, %esi
movq %r14, %rdi
callq 0x4280
jmp 0x238a2
movq %r14, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23863
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
je 0x238bf
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4270
movq %rax, %r12
movsd 0x35869(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x2385f
leaq 0x370dd(%rip), %rdi     # 0x5a8a1
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x297ae
movq %r14, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23868
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23868
movq %r14, %rdi
callq 0x212a8
testl %eax, %eax
je 0x23925
movl $0x1, 0x8(%r15)
movq %rbx, 0x20(%r15)
jmp 0x236d3
movq %r14, %rdi
callq 0x2129e
testl %eax, %eax
jne 0x23863
movq %rbx, %rdi
callq 0x2129e
testl %eax, %eax
je 0x23908
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4270
movq %rax, %r12
leaq 0x58b5e(%rip), %rax     # 0x7c3a8
movsd (%rax), %xmm0
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
movq %r12, 0x20(%r15)
testq %r14, %r14
je 0x2387d
movq %r14, %rdi
callq 0x21014
movl $0x10, %esi
movq %r14, %rdi
callq 0x4280
testq %rbx, %rbx
jne 0x238a2
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2160e
movq %rbx, %rdi
callq 0x21014
movl $0x10, %esi
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4280
movq %rbx, %rdi
callq 0x212a8
testl %eax, %eax
jne 0x23914
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x226ce
movl $0x28, %edi
callq 0x4270
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movq %r15, %rdi
movl $0x6, %esi
jmp 0x239dd
movq %rbx, %rdi
callq 0x212a8
testl %eax, %eax
je 0x23942
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x2387d
movq %rbx, %rdi
callq 0x212a8
testl %eax, %eax
je 0x2397c
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x238a2
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x226ce
movl $0x28, %edi
callq 0x4270
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movq %r15, %rdi
movl $0x5, %esi
jmp 0x239dd
cmpl $0x1, 0xc(%r14)
jne 0x239a5
cmpl $0x1, 0xc(%rbx)
jne 0x239a5
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21666
jmp 0x238a2
movl $0x28, %edi
callq 0x4270
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x226ce
movl $0x28, %edi
callq 0x4270
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x226ce
movq %r15, %rdi
movl $0x4, %esi
movq %r12, %rdx
movq %r14, %rcx
movl $0x1, %r8d
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x23dbe
jmp 0x239fc
jmp 0x23a0b
jmp 0x239fc
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x23a22
jmp 0x23a0b
movq %rax, %rbx
movl $0x28, %esi
jmp 0x23a1f
jmp 0x23a17
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x44b0
nop