// File: /workspace/github_cpp_makefile_star2_O3/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 0x23cd9
cmpl $0x6, %ebp
ja 0x23daf
movl %ebp, %eax
leaq 0x36b0e(%rip), %rcx     # 0x5a768
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x22676
movl %eax, %r12d
movq %rbx, %rdi
callq 0x22676
testl %r12d, %r12d
je 0x23e8f
testl %eax, %eax
jne 0x23ccb
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 0x23cb3
movq %r15, %rdi
callq 0x2402e
movl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
jmp 0x23ed9
cmpl $0x1, 0x8(%r14)
jne 0x23d17
movq 0x20(%r14), %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x238fa
movl $0x0, 0x8(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movq $0x0, 0x20(%r14)
movq (%r14), %rax
movq %r14, %rdi
jmp 0x23edf
cmpl $0x1, 0x8(%rbx)
jne 0x23c48
movq 0x20(%rbx), %rcx
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x23b00
movl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movq $0x0, 0x20(%rbx)
jmp 0x23ed9
movq %r14, %rdi
callq 0x22676
testl %eax, %eax
jne 0x23e88
movq %r14, %rdi
callq 0x240ba
testl %eax, %eax
jne 0x23d7c
movq %rbx, %rdi
callq 0x22676
testl %eax, %eax
je 0x23f64
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4280
movq %rax, %r12
movsd 0x35277(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x23e84
leaq 0x36afb(%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 0x22676
testl %eax, %eax
jne 0x23e88
movq %rbx, %rdi
callq 0x22676
testl %eax, %eax
jne 0x23e88
movq %r14, %rdi
callq 0x240ba
testl %eax, %eax
je 0x23f15
movq %rbx, %rdi
callq 0x240ba
testl %eax, %eax
je 0x23f15
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4280
movq %rax, %r12
movsd 0x351ec(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x23e84
movq %r14, %rdi
callq 0x22676
testl %eax, %eax
jne 0x23e88
movq %rbx, %rdi
callq 0x22676
testl %eax, %eax
je 0x23eff
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4280
movq %rax, %r12
leaq 0x58541(%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
jne 0x23ecb
jmp 0x23ed4
testl %eax, %eax
je 0x23eea
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)
movl $0x0, 0x8(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movq $0x0, 0x20(%r14)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testq %rbx, %rbx
je 0x23ef6
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 0x240ba
testl %eax, %eax
jne 0x23e93
movl $0x5, 0x8(%r15)
jmp 0x23eee
movq %r14, %rdi
callq 0x240ba
testl %eax, %eax
je 0x23f81
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)
movl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movq $0x0, 0x20(%rbx)
testq %r14, %r14
jne 0x23ccb
jmp 0x23ed9
movq %rbx, %rdi
callq 0x240ba
testl %eax, %eax
jne 0x23e93
movl $0x6, 0x8(%r15)
jmp 0x23eee
movq %rbx, %rdi
callq 0x240ba
testl %eax, %eax
jne 0x23e93
movl $0x4, 0x8(%r15)
jmp 0x23eee
jmp 0x23fa2
jmp 0x23fa2
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44c0