// File: /workspace/github_cpp_makefile_star2_O1/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 0x23e6b
cmpl $0x6, %ebp
ja 0x23f4a
movl %ebp, %eax
leaq 0x3696c(%rip), %rcx     # 0x5a758
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x227c2
movl %eax, %r12d
movq %rbx, %rdi
callq 0x227c2
testl %r12d, %r12d
je 0x24047
testl %eax, %eax
jne 0x23e5d
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 0x23e45
movq %r15, %rdi
callq 0x241f6
movl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
jmp 0x24036
cmpl $0x1, 0x8(%r14)
jne 0x23eb2
movq 0x20(%r14), %rdx
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x23a58
movl $0x0, 0x8(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movq $0x0, 0x20(%r14)
testq %r14, %r14
je 0x24091
movq (%r14), %rax
movq %r14, %rdi
jmp 0x2403c
cmpl $0x1, 0x8(%rbx)
jne 0x23dda
movq 0x20(%rbx), %rcx
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x23c80
movl $0x0, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movq $0x0, 0x20(%rbx)
jmp 0x24031
movq %r14, %rdi
callq 0x227c2
testl %eax, %eax
jne 0x24023
movq %r14, %rdi
callq 0x2427e
testl %eax, %eax
jne 0x23f17
movq %rbx, %rdi
callq 0x227c2
testl %eax, %eax
je 0x240f6
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4270
movq %rax, %r12
movsd 0x350dc(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x2401f
leaq 0x36950(%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 0x227c2
testl %eax, %eax
jne 0x24023
movq %rbx, %rdi
callq 0x227c2
testl %eax, %eax
jne 0x24023
movq %r14, %rdi
callq 0x2427e
testl %eax, %eax
je 0x240b0
movq %rbx, %rdi
callq 0x2427e
testl %eax, %eax
je 0x240b0
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4270
movq %rax, %r12
movsd 0x35051(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dd4
jmp 0x2401f
movq %r14, %rdi
callq 0x227c2
testl %eax, %eax
jne 0x24023
movq %rbx, %rdi
callq 0x227c2
testl %eax, %eax
je 0x2409a
movl $0x1, 0x8(%r15)
movl $0x10, %edi
callq 0x4270
movq %rax, %r12
leaq 0x5839e(%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 0x24031
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testq %rbx, %rbx
je 0x24091
movq (%rbx), %rax
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmpq *0x8(%rax)
testl %eax, %eax
je 0x24085
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)
jmp 0x24028
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 0x2427e
testl %eax, %eax
jne 0x2411b
movl $0x5, 0x8(%r15)
jmp 0x24089
movq %r14, %rdi
callq 0x2427e
testl %eax, %eax
je 0x2410f
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)
jmp 0x24023
movq %rbx, %rdi
callq 0x2427e
testl %eax, %eax
jne 0x2411b
movl $0x6, 0x8(%r15)
jmp 0x24089
movq %rbx, %rdi
callq 0x2427e
testl %eax, %eax
je 0x24158
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)
jmp 0x24023
movl $0x4, 0x8(%r15)
jmp 0x24089
jmp 0x24169
jmp 0x24169
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x44b0
nop