// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movl %esi, 0xfc(%rsp)
movsd %xmm0, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movl %ecx, 0xe4(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpl $0x0, 0xe4(%rsp)
jne 0x406bf
movq 0xe8(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x406bf
movq 0xe8(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3cc40
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x406bf
jp 0x406bf
movl 0xfc(%rsp), %eax
movl %eax, 0xb4(%rsp)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0xe8(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3f6a0
movq 0xc8(%rsp), %rdi
movl 0xb4(%rsp), %esi
movaps %xmm0, %xmm1
movsd 0xb8(%rsp), %xmm0
xorl %edx, %edx
callq 0x40080
movq 0xe8(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
je 0x406ba
movq 0xc0(%rsp), %rdi
callq 0x3cac0
movq 0xc0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40ed7
movl 0xfc(%rsp), %eax
movq %rax, 0xa8(%rsp)
subq $0x6, %rax
ja 0x40ec0
movq 0xa8(%rsp), %rax
leaq 0x6c179(%rip), %rcx     # 0xac860
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x40ed5
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x40752
jp 0x40752
movq 0xe8(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x40752
movq 0xe8(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, %rax
je 0x40750
movq 0xa0(%rsp), %rdi
callq 0x3cac0
movq 0xa0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x4077f
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xe8(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xf0(%rsp), %xmm0
callq 0xb6c0
jmp 0x40ed5
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x407e1
jp 0x407e1
movq 0xe8(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x407e1
movq 0xe8(%rsp), %rax
movq %rax, 0x98(%rsp)
cmpq $0x0, %rax
je 0x407df
movq 0x98(%rsp), %rdi
callq 0x3cac0
movq 0x98(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x4081f
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xe8(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
callq 0x3d470
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rdi
movsd 0xf0(%rsp), %xmm0
callq 0xb6c0
jmp 0x40ed5
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4083a
jp 0x4083a
jmp 0x4084c
movq 0xe8(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x40886
movq 0xe8(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, %rax
je 0x40881
movq 0x90(%rsp), %rdi
callq 0x3cac0
movq 0x90(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40984
movq 0xe8(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x40955
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x88(%rsp)
movsd 0xf0(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x408df
movq 0xc8(%rsp), %rax
movq 0x88(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, %rax
je 0x40928
movq 0x80(%rsp), %rdi
callq 0x3cac0
movq 0x80(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40982
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40edf
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0xe8(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movsd 0xf0(%rsp), %xmm0
callq 0x3d4e0
jmp 0x40984
jmp 0x40ed5
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x409ce
jp 0x409ce
movq 0xe8(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x409c9
movq 0x78(%rsp), %rdi
callq 0x3cac0
movq 0x78(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40c27
movq 0xe8(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x40a90
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
leaq 0x898d9(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40a26
movq 0xc8(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x40a63
movq 0x68(%rsp), %rdi
callq 0x3cac0
movq 0x68(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40c25
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40edf
movq 0xe8(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x40b50
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movsd 0xf0(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40ae6
movq 0xc8(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x40b23
movq 0x58(%rsp), %rdi
callq 0x3cac0
movq 0x58(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40c23
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40edf
movl 0xfc(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movsd 0xf0(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3fcb0
jmp 0x40b8c
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq 0xe8(%rsp), %rsi
callq 0x3f8b0
jmp 0x40bb0
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl 0x4c(%rsp), %esi
movq 0xc8(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x40c23
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x40edf
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x40edf
jmp 0x40c25
jmp 0x40c27
jmp 0x40ed5
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x40c71
jp 0x40c71
movq 0xe8(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x40c6c
movq 0x38(%rsp), %rdi
callq 0x3cac0
movq 0x38(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40ebe
movq 0xe8(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x40d30
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
xorl %r8d, %r8d
movl %r8d, %ecx
movsd 0x6a367(%rip), %xmm0    # 0xab020
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40cc6
movq 0xc8(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x40d03
movq 0x28(%rsp), %rdi
callq 0x3cac0
movq 0x28(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40ebc
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40edf
movq 0xe8(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x40df0
movq 0xc8(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movsd 0xf0(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x40d86
movq 0xc8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x40dc3
movq 0x18(%rsp), %rdi
callq 0x3cac0
movq 0x18(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x40eba
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x40edf
movl 0xfc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movsd 0xf0(%rsp), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3fcb0
jmp 0x40e2c
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movq 0xe8(%rsp), %rsi
callq 0x3f8b0
jmp 0x40e4f
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
movl 0xc(%rsp), %esi
movq 0xc8(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x40eba
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x40edf
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x40edf
jmp 0x40ebc
jmp 0x40ebe
jmp 0x40ed5
movl 0xfc(%rsp), %esi
leaq 0x6bb2f(%rip), %rdi     # 0xac9fd
movb $0x0, %al
callq 0x4c6e0
jmp 0x40ed7
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xd8(%rsp), %rdi
callq 0x44b0
nopl (%rax)