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

  // Check type of operand2
  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 == 0) && (operand2->IsZero())) {
        delete operand2;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand2;
        this->polynomial->Add(operand1);
      }
      break; 

    case RN_SUBTRACT_OPERATION: 
      if ((operand1 == 0) && (operand2->IsZero())) {
        delete operand2;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand2;
        this->polynomial->Negate();
        this->polynomial->Add(operand1);
      }
      break; 

    case RN_MULTIPLY_OPERATION: 
      if ((operand1 == 0) || (operand2->IsZero())) {
        delete operand2;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(operand1, 0);
        delete operand2;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand2;
        this->polynomial->Multiply(operand1);
      }
      break; 

    case RN_DIVIDE_OPERATION:
      if (operand1 == 0) {
        delete operand2;
      }
      else if (operand2->IsZero()) {
        operation = RN_POLYNOMIAL_OPERATION;
        polynomial = new RNPolynomial(RN_INFINITY, 0);
        delete operand2;
      }  
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(operand1, 0);
        delete operand2;
      }
      else {      
        Construct(op, new RNAlgebraic(operand1, 0), new RNAlgebraic(operand2), TRUE);
      }
      break;

    case RN_POW_OPERATION:
      if (operand1 == 0) {
        delete operand2;
      }
      else if (operand2->IsZero()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(1.0, 0);
        delete operand2;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = new RNPolynomial(operand1, 0);
        delete operand2;
      }
      else {      
        Construct(op, new RNAlgebraic(operand1, 0), 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
subq $0x10, %rsp
movq %rdx, %rbx
movapd %xmm0, %xmm2
movl %esi, %ebp
movq %rdi, %r14
testl %ecx, %ecx
jne 0x22ca3
cmpl $0x1, 0xc(%rbx)
jne 0x22ca3
movq (%rbx), %rax
movq (%rax), %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x20fda
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x22ca3
jp 0x22ca3
movq (%rbx), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm1
movq %r14, %rdi
movl %ebp, %esi
movapd %xmm2, %xmm0
callq 0x229d8
jmp 0x22f23
cmpl $0x6, %ebp
ja 0x22d69
movl %ebp, %eax
leaq 0x37a5f(%rip), %rcx     # 0x5a714
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm0, %xmm0
movsd %xmm2, 0x8(%rsp)
ucomisd %xmm0, %xmm2
jne 0x22ced
jp 0x22ced
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
jne 0x22f23
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
callq 0x21414
movq 0x20(%r14), %rdi
movsd 0x8(%rsp), %xmm0
jmp 0x22dc0
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22d20
jnp 0x22f1e
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
je 0x22ea0
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movsd 0x362bd(%rip), %xmm0    # 0x59010
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x22f1a
leaq 0x37b41(%rip), %rdi     # 0x5a8b1
movl %ebp, %esi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x29356
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22dad
jp 0x22dad
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x211b4
movsd 0x8(%rsp), %xmm2
testl %eax, %eax
jne 0x22f23
movl $0x1, 0x8(%r14)
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movapd %xmm2, %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x20d48
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22df0
jnp 0x22f1e
movq %rbx, %rdi
movsd %xmm2, 0x8(%rsp)
callq 0x211b4
testl %eax, %eax
jne 0x22f1e
movq %rbx, %rdi
callq 0x211be
movl $0x1, 0x8(%r14)
testl %eax, %eax
je 0x22f8a
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
movq %r15, 0x20(%r14)
jmp 0x22f23
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x22e5b
jnp 0x22f1e
movsd %xmm2, 0x8(%rsp)
movq %rbx, %rdi
callq 0x211b4
testl %eax, %eax
je 0x22ede
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
leaq 0x59527(%rip), %rax     # 0x7c3b0
movsd (%rax), %xmm0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x22f1a
movq %rbx, %rdi
callq 0x211be
testl %eax, %eax
je 0x22f44
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
jmp 0x22f1a
movq %rbx, %rdi
callq 0x211be
testl %eax, %eax
je 0x22fa8
movl $0x1, 0x8(%r14)
movl $0x10, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20ce4
movq %r15, 0x20(%r14)
testq %rbx, %rbx
je 0x22f2b
movq %rbx, %rdi
callq 0x20f24
movl $0x10, %esi
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4290
movl $0x28, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x227be
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
movq %r14, %rdi
movl $0x6, %esi
jmp 0x22fec
movq %rbx, 0x20(%r14)
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x21442
movl $0x28, %edi
callq 0x4280
movq %rax, %r15
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x227be
movl $0x28, %edi
callq 0x4280
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22582
movq %r14, %rdi
movl $0x5, %esi
movq %r15, %rdx
movq %r12, %rcx
movl $0x1, %r8d
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x23c2c
jmp 0x2300b
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x23037
jmp 0x2301a
movq %rax, %rbx
movl $0x28, %esi
jmp 0x23034
jmp 0x2302c
jmp 0x2302c
jmp 0x2302c
jmp 0x2302c
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x4290
movq %rbx, %rdi
callq 0x44c0