// File: /workspace/github_cpp_makefile_star2_O2/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 0x20f18
cmpl $0x6, %ebp
ja 0x21030
movl %ebp, %eax
leaq 0x319f2(%rip), %rcx     # 0x528f8
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 0x20f63
movq (%r14), %rax
movq (%rax), %rdi
callq 0x1ec74
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20f63
jp 0x20f63
movq (%r14), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x206a4
movq %r14, %rdi
callq 0x1ebc0
pushq $0x10
popq %rsi
movq %r14, %rdi
jmp 0x21162
cmpl $0x1, 0xc(%rbx)
jne 0x20ef4
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x1ec74
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x20ef4
jp 0x20ef4
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x20b5e
jmp 0x21154
movq %r14, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x20fc3
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x21068
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1f122
jmp 0x21154
movq %r14, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x2111e
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x2116f
movl $0x1, 0x8(%r15)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r12
movsd 0x2fff6(%rip), %xmm0    # 0x51010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x2111a
leaq 0x31a26(%rip), %rdi     # 0x52a5d
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x264a8
movq %r14, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x2113d
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x2113d
movq %r14, %rdi
callq 0x1ebc0
pushq $0x10
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4290
movq %rbx, %rdi
callq 0x1ebc0
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x21162
movq %r14, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x21123
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x21123
movq %r14, %rdi
callq 0x1ee18
testl %eax, %eax
je 0x211cb
movl $0x1, 0x8(%r15)
movq %rbx, 0x20(%r15)
jmp 0x20f50
movq %r14, %rdi
callq 0x1ee0e
testl %eax, %eax
jne 0x2111e
movq %rbx, %rdi
callq 0x1ee0e
testl %eax, %eax
je 0x211ae
movl $0x1, 0x8(%r15)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r12
leaq 0x562b3(%rip), %rax     # 0x773b8
movsd (%rax), %xmm0
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
movq %r12, 0x20(%r15)
testq %r14, %r14
je 0x2112b
movq %r14, %rdi
callq 0x1ebc0
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x4290
testq %rbx, %rbx
jne 0x21154
jmp 0x2115c
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1f0d0
movq %rbx, %rdi
callq 0x1ebc0
pushq $0x10
popq %rsi
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4290
movq %rbx, %rdi
callq 0x1ee18
testl %eax, %eax
jne 0x211ba
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x20064
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x6
jmp 0x2126f
movq %rbx, %rdi
callq 0x1ee18
testl %eax, %eax
je 0x211e8
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x21136
movq %rbx, %rdi
callq 0x1ee18
testl %eax, %eax
je 0x21218
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
jmp 0x21154
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x20064
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x5
jmp 0x2126f
cmpl $0x1, 0xc(%r14)
jne 0x21241
cmpl $0x1, 0xc(%rbx)
jne 0x21241
movl $0x1, 0x8(%r15)
movq %r14, 0x20(%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1f17a
jmp 0x21154
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x20064
pushq $0x28
popq %rdi
callq 0x4280
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20064
pushq $0x4
popq %rsi
pushq $0x1
popq %r8
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x215d6
jmp 0x21290
jmp 0x2129d
jmp 0x21290
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r14, %rdi
jmp 0x212af
jmp 0x2129d
movq %rax, %rbx
pushq $0x28
jmp 0x212ab
jmp 0x212a6
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44d0