// File: /workspace/github_cpp_makefile_star2_O3/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 0x23510
cmpl $0x6, %ebp
ja 0x2362c
movl %ebp, %eax
leaq 0x3724e(%rip), %rcx     # 0x5a74c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, 0xc(%r14)
jne 0x2355d
movq (%r14), %rax
movq (%rax), %rdi
callq 0x20fda
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2355d
jp 0x2355d
movq (%r14), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x22c40
movq %r14, %rdi
callq 0x20f24
movl $0x10, %esi
movq %r14, %rdi
jmp 0x23754
cmpl $0x1, 0xc(%rbx)
jne 0x234ec
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x20fda
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x234ec
jp 0x234ec
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x23146
jmp 0x23744
movq %r14, %rdi
callq 0x211b4
testl %eax, %eax
je 0x235bd
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x23664
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21508
jmp 0x23744
movq %r14, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x2370c
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
je 0x23761
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4280
movq %rax, %r12
movsd 0x359fa(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x23708
leaq 0x3727e(%rip), %rdi     # 0x5a8b1
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x29356
movq %r14, %rdi
callq 0x211b4
testl %eax, %eax
je 0x2372d
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
je 0x2372d
movq %r14, %rdi
callq 0x20f24
movl $0x10, %esi
movq %r14, %rdi
callq 0x4290
jmp 0x23744
movq %r14, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x23711
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x23711
movq %r14, %rdi
callq 0x211be
testl %eax, %eax
je 0x237c7
movl $0x1, 0x8(%r15)
movq %rbx, 0x20(%r15)
jmp 0x23548
movq %r14, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x2370c
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
je 0x237aa
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4280
movq %rax, %r12
leaq 0x58cbd(%rip), %rax     # 0x7c3b0
movsd (%rax), %xmm0
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
movq %r12, 0x20(%r15)
testq %r14, %r14
je 0x23719
movq %r14, %rdi
callq 0x20f24
movl $0x10, %esi
movq %r14, %rdi
callq 0x4290
testq %rbx, %rbx
jne 0x23744
jmp 0x2374c
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x214b6
movq %rbx, %rdi
callq 0x20f24
movl $0x10, %esi
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4290
movq %rbx, %rdi
callq 0x211be
testl %eax, %eax
jne 0x237b6
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x22582
movl $0x28, %edi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
movq %r15, %rdi
movl $0x6, %esi
jmp 0x2387f
movq %rbx, %rdi
callq 0x211be
testl %eax, %eax
je 0x237e4
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x23726
movq %rbx, %rdi
callq 0x211be
testl %eax, %eax
je 0x2381e
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x23744
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x22582
movl $0x28, %edi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
movq %r15, %rdi
movl $0x5, %esi
jmp 0x2387f
cmpl $0x1, 0xc(%r14)
jne 0x23847
cmpl $0x1, 0xc(%rbx)
jne 0x23847
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21560
jmp 0x23744
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x22582
movl $0x28, %edi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
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 0x23c2c
jmp 0x2389e
jmp 0x238ad
jmp 0x2389e
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x238c4
jmp 0x238ad
movq %rax, %rbx
movl $0x28, %esi
jmp 0x238c1
jmp 0x238b9
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44c0
nop