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

  // Check types of operands
  if (!force && (operand1->operation == RN_POLYNOMIAL_OPERATION)) {
    Construct(op, operand1->polynomial, operand2);
    operand1->operation = RN_ZERO_OPERATION;
    operand1->operands[0] = NULL;
    operand1->operands[1] = NULL;
    operand1->polynomial = NULL;
    delete operand1;
  }
  else if (!force && (operand2->operation == RN_POLYNOMIAL_OPERATION)) {
    Construct(op, operand1, operand2->polynomial);
    operand2->operation = RN_ZERO_OPERATION;
    operand2->operands[0] = NULL;
    operand2->operands[1] = NULL;
    operand2->polynomial = NULL;
    delete operand2;
  }
  else {
    switch(op) {
    case RN_ZERO_OPERATION:
      delete operand1;
      delete operand2;
      break;

    case RN_ADD_OPERATION:
    case RN_SUBTRACT_OPERATION:
      if (operand1->IsZero()) {
        if (operand2->IsZero()) {
          // Both operands are zero -- delete both 
          delete operand1;
          delete operand2;
        }
        else {
          // Operand1 is zero - copy stuff from operand2
          operation = operand2->operation;
          polynomial = operand2->polynomial;
          operands[0] = operand2->operands[0];
          operands[1] = operand2->operands[1];
          if (op == RN_SUBTRACT_OPERATION) Negate();
        
          // Delete operands
          operand2->operation = RN_ZERO_OPERATION;
          operand2->operands[0] = NULL;
          operand2->operands[1] = NULL;
          operand2->polynomial = NULL;
          delete operand1;
          delete operand2;
        }
      }
      else if (operand2->IsZero()) {
        // Operand2 is zero - copy stuff from operand1
        operation = operand1->operation;
        polynomial = operand1->polynomial;
        operands[0] = operand1->operands[0];
        operands[1] = operand1->operands[1];
      
        // Delete operands
        operand1->operation = RN_ZERO_OPERATION;
        operand1->operands[0] = NULL;
        operand1->operands[1] = NULL;
        operand1->polynomial = NULL;
        delete operand1;
        delete operand2;
      }
      else {
        // Initialize binary operation
        operation = op;
        operands[0] = operand1;
        operands[1] = operand2;
      }
      break;

    case RN_MULTIPLY_OPERATION:
      if ((operand1->IsZero()) || (operand2->IsZero())) {
        delete operand1;
        delete operand2;
      }
      else if ((operand1->IsOne()) && (operand2->IsOne())) {
        operation = RN_POLYNOMIAL_OPERATION;
        polynomial = new RNPolynomial(1.0, 0);
        delete operand1;
        delete operand2;
      }
      else if (operand1->IsOne()) {
        // Operand1 is one - copy stuff from operand2
        operation = operand2->operation;
        polynomial = operand2->polynomial;
        operands[0] = operand2->operands[0];
        operands[1] = operand2->operands[1];
        
        // Delete operands
        operand2->operation = RN_ZERO_OPERATION;
        operand2->operands[0] = NULL;
        operand2->operands[1] = NULL;
        operand2->polynomial = NULL;
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsOne()) {
        // Operand2 is one - copy stuff from operand1
        operation = operand1->operation;
        polynomial = operand1->polynomial;
        operands[0] = operand1->operands[0];
        operands[1] = operand1->operands[1];
        
        // Delete operands
        operand1->operation = RN_ZERO_OPERATION;
        operand1->operands[0] = NULL;
        operand1->operands[1] = NULL;
        operand1->polynomial = NULL;
        delete operand1;
        delete operand2;
      }
      else {
        // Initialize binary operation
        operation = op;
        operands[0] = operand1;
        operands[1] = operand2;
      }
      break;

    case RN_DIVIDE_OPERATION:
      if (operand1->IsZero()) {
        // Numerator is zero
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsZero()) {
        // Denominator is zero
        operation = RN_POLYNOMIAL_OPERATION;
        polynomial = new RNPolynomial(RN_INFINITY, 0);
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsOne()) {
        // Operand2 is one - copy stuff from operand1
        operation = operand1->operation;
        polynomial = operand1->polynomial;
        operands[0] = operand1->operands[0];
        operands[1] = operand1->operands[1];
        
        // Delete operands
        operand1->operation = RN_ZERO_OPERATION;
        operand1->operands[0] = NULL;
        operand1->operands[1] = NULL;
        operand1->polynomial = NULL;
        delete operand1;
        delete operand2;
      }
      else {
        // Initialize binary operation
        operation = op;
        operands[0] = operand1;
        operands[1] = operand2;
      }
      break;

    case RN_POW_OPERATION:
      if (operand1->IsZero()) {
        // Base is zero
        delete operand1;
        delete operand2;
      }
      else if ((operand1->IsOne()) || (operand2->IsZero())) {
        operation = RN_POLYNOMIAL_OPERATION;
        polynomial = new RNPolynomial(1.0, 0);
        delete operand1;
        delete operand2;
      }
      else if (operand2->IsOne()) {
        // Operand2 is one - copy stuff from operand1
        operation = operand1->operation;
        polynomial = operand1->polynomial;
        operands[0] = operand1->operands[0];
        operands[1] = operand1->operands[1];
        
        // Delete operands
        operand1->operation = RN_ZERO_OPERATION;
        operand1->operands[0] = NULL;
        operand1->operands[1] = NULL;
        operand1->polynomial = NULL;
        delete operand1;
        delete operand2;
      }
      else {
        // Initialize binary operation
        operation = op;
        operands[0] = operand1;
        operands[1] = operand2;
      }
      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 0x2167d
cmpl $0x6, %ebp
ja 0x21745
movl %ebp, %eax
leaq 0x31310(%rip), %rcx     # 0x52914
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x20144
movl %eax, %r12d
movq %rbx, %rdi
callq 0x20144
testl %r12d, %r12d
je 0x21821
testl %eax, %eax
jne 0x2166f
movl 0x8(%rbx), %eax
movl %eax, 0x8(%r15)
movq 0x20(%rbx), %rax
movq %rax, 0x20(%r15)
leaq 0x10(%rbx), %r12
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
cmpl $0x3, %ebp
jne 0x2165d
movq %r15, %rdi
callq 0x219ae
andl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
jmp 0x21865
cmpl $0x1, 0x8(%r14)
jne 0x216b5
movq 0x20(%r14), %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x212de
andl $0x0, 0x8(%r14)
andq $0x0, 0x20(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movq (%r14), %rax
movq %r14, %rdi
jmp 0x2186b
cmpl $0x1, 0x8(%rbx)
jne 0x215f2
movq 0x20(%rbx), %rcx
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x214be
andl $0x0, 0x8(%rbx)
andq $0x0, 0x20(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
jmp 0x21865
movq %r14, %rdi
callq 0x20144
testl %eax, %eax
jne 0x2181a
movq %r14, %rdi
callq 0x21a52
testl %eax, %eax
jne 0x21714
movq %rbx, %rdi
callq 0x20144
testl %eax, %eax
je 0x218ea
movl $0x1, 0x8(%r15)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r12
movsd 0x2f8e1(%rip), %xmm0    # 0x51010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x21816
leaq 0x31311(%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 0x20144
testl %eax, %eax
jne 0x2181a
movq %rbx, %rdi
callq 0x20144
testl %eax, %eax
jne 0x2181a
movq %r14, %rdi
callq 0x21a52
testl %eax, %eax
je 0x218a1
movq %rbx, %rdi
callq 0x21a52
testl %eax, %eax
je 0x218a1
movl $0x1, 0x8(%r15)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r12
movsd 0x2f858(%rip), %xmm0    # 0x51010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1e996
jmp 0x21816
movq %r14, %rdi
callq 0x20144
testl %eax, %eax
jne 0x2181a
movq %rbx, %rdi
callq 0x20144
testl %eax, %eax
je 0x2188b
movl $0x1, 0x8(%r15)
pushq $0x10
popq %rdi
callq 0x4280
movq %rax, %r12
leaq 0x55bb7(%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
jne 0x21857
jmp 0x21860
testl %eax, %eax
je 0x21876
movl 0x8(%r14), %eax
movl %eax, 0x8(%r15)
movq 0x20(%r14), %rax
movq %rax, 0x20(%r15)
movq 0x10(%r14), %rax
movq %rax, 0x10(%r15)
movq 0x18(%r14), %rax
movq %rax, 0x18(%r15)
andl $0x0, 0x8(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
andq $0x0, 0x20(%r14)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testq %rbx, %rbx
je 0x21882
movq (%rbx), %rax
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmpq *0x8(%rax)
movl %ebp, 0x8(%r15)
movq %r14, 0x10(%r15)
movq %rbx, 0x18(%r15)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x21a52
testl %eax, %eax
jne 0x21825
movl $0x5, 0x8(%r15)
jmp 0x2187a
movq %r14, %rdi
callq 0x21a52
testl %eax, %eax
je 0x21907
movl 0x8(%rbx), %eax
movl %eax, 0x8(%r15)
movq 0x20(%rbx), %rax
movq %rax, 0x20(%r15)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
andl $0x0, 0x8(%rbx)
andq $0x0, 0x20(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
testq %r14, %r14
jne 0x2166f
jmp 0x21865
movq %rbx, %rdi
callq 0x21a52
testl %eax, %eax
jne 0x21825
movl $0x6, 0x8(%r15)
jmp 0x2187a
movq %rbx, %rdi
callq 0x21a52
testl %eax, %eax
jne 0x21825
movl $0x4, 0x8(%r15)
jmp 0x2187a
jmp 0x21928
jmp 0x21928
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44d0