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

  // Check types of operands
  if (!force && (operand1->NTerms() == 1) && (operand1->Term(0)->Degree() == 0)) {
    Construct(op, operand1->Term(0)->Coefficient(), operand2);
    delete operand1;
  }
  else 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->IsZero() && operand2->IsZero()) {
        delete operand1;
        delete operand2;
      }
      else {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Add(*operand2);
        delete operand2;
      }
      break; 

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

    case RN_MULTIPLY_OPERATION: 
      if (operand1->IsZero() || operand2->IsZero()) {
        delete operand1;
        delete operand2;
      }
      else if (operand1->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand2;
        delete operand1;
      }
      else if (operand2->IsOne()) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        delete operand2;
      }
      else if ((operand1->NTerms() == 1) && (operand2->NTerms() == 1)) {
        operation = RN_POLYNOMIAL_OPERATION;
        this->polynomial = operand1;
        this->polynomial->Multiply(*operand2);
        delete operand2;
      }
      else {
        Construct(op, new RNAlgebraic(operand1), new RNAlgebraic(operand2), TRUE);
      }
      break; 

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

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

    default:
      RNAbort("Invalid operation %d\n", op);
      break;
    }
  }
  
  // Just checking
  assert(IsValid());
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movl %esi, 0x16c(%rsp)
movq %rdx, 0x160(%rsp)
movq %rcx, 0x158(%rsp)
movl %r8d, 0x154(%rsp)
movq 0x170(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpl $0x0, 0x154(%rsp)
jne 0x41971
movq 0x160(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x41971
movq 0x160(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3cc40
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x41971
jp 0x41971
movl 0x16c(%rsp), %eax
movl %eax, 0x12c(%rsp)
movq 0x160(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3f6a0
movq 0x138(%rsp), %rdi
movl 0x12c(%rsp), %esi
movq 0x158(%rsp), %rdx
xorl %ecx, %ecx
callq 0x405a0
movq 0x160(%rsp), %rax
movq %rax, 0x130(%rsp)
cmpq $0x0, %rax
je 0x4196c
movq 0x130(%rsp), %rdi
callq 0x3cac0
movq 0x130(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x42482
cmpl $0x0, 0x154(%rsp)
jne 0x41a4c
movq 0x158(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x41a4c
movq 0x158(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3cc40
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x41a4c
jp 0x41a4c
movl 0x16c(%rsp), %eax
movl %eax, 0x114(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x158(%rsp), %rdi
xorl %esi, %esi
callq 0x3f5d0
movq %rax, %rdi
callq 0x3f6a0
movq 0x138(%rsp), %rdi
movl 0x114(%rsp), %esi
movq 0x118(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41100
movq 0x158(%rsp), %rax
movq %rax, 0x120(%rsp)
cmpq $0x0, %rax
je 0x41a47
movq 0x120(%rsp), %rdi
callq 0x3cac0
movq 0x120(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x42480
movl 0x16c(%rsp), %eax
movq %rax, 0x108(%rsp)
subq $0x6, %rax
ja 0x42469
movq 0x108(%rsp), %rax
leaq 0x6ae24(%rip), %rcx     # 0xac898
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x4247e
movq 0x160(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41b12
movq 0x158(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41b12
movq 0x160(%rsp), %rax
movq %rax, 0x100(%rsp)
cmpq $0x0, %rax
je 0x41adb
movq 0x100(%rsp), %rdi
callq 0x3cac0
movq 0x100(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0xf8(%rsp)
cmpq $0x0, %rax
je 0x41b10
movq 0xf8(%rsp), %rdi
callq 0x3cac0
movq 0xf8(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41b75
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movq 0x158(%rsp), %rsi
callq 0x3d5e0
movq 0x158(%rsp), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, %rax
je 0x41b73
movq 0xf0(%rsp), %rdi
callq 0x3cac0
movq 0xf0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41b75
jmp 0x4247e
movq 0x160(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41c0a
movq 0x158(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41c0a
movq 0x160(%rsp), %rax
movq %rax, 0xe8(%rsp)
cmpq $0x0, %rax
je 0x41bd3
movq 0xe8(%rsp), %rdi
callq 0x3cac0
movq 0xe8(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0xe0(%rsp)
cmpq $0x0, %rax
je 0x41c08
movq 0xe0(%rsp), %rdi
callq 0x3cac0
movq 0xe0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41c6d
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movq 0x158(%rsp), %rsi
callq 0x3d6a0
movq 0x158(%rsp), %rax
movq %rax, 0xd8(%rsp)
cmpq $0x0, %rax
je 0x41c6b
movq 0xd8(%rsp), %rdi
callq 0x3cac0
movq 0xd8(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41c6d
jmp 0x4247e
movq 0x160(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
jne 0x41c96
movq 0x158(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41d05
movq 0x160(%rsp), %rax
movq %rax, 0xd0(%rsp)
cmpq $0x0, %rax
je 0x41ccb
movq 0xd0(%rsp), %rdi
callq 0x3cac0
movq 0xd0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, %rax
je 0x41d00
movq 0xc8(%rsp), %rdi
callq 0x3cac0
movq 0xc8(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41f41
movq 0x160(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x41d6c
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x158(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x160(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
je 0x41d67
movq 0xc0(%rsp), %rdi
callq 0x3cac0
movq 0xc0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41f3f
movq 0x158(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x41dd3
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x158(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, %rax
je 0x41dce
movq 0xb8(%rsp), %rdi
callq 0x3cac0
movq 0xb8(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41f3d
movq 0x160(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x41e5d
movq 0x158(%rsp), %rdi
callq 0x3f5c0
cmpl $0x1, %eax
jne 0x41e5d
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x20(%rax), %rdi
movq 0x158(%rsp), %rsi
callq 0x3d780
movq 0x158(%rsp), %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, %rax
je 0x41e58
movq 0xb0(%rsp), %rdi
callq 0x3cac0
movq 0xb0(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x41f3b
movl 0x16c(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa8(%rsp)
movq 0x160(%rsp), %rsi
callq 0x3f8b0
jmp 0x41e92
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x98(%rsp)
movq 0x158(%rsp), %rsi
callq 0x3f8b0
jmp 0x41eb9
movq 0x98(%rsp), %rcx
movq 0xa8(%rsp), %rdx
movl 0xa4(%rsp), %esi
movq 0x138(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x41f3b
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
jmp 0x41f3d
jmp 0x41f3f
jmp 0x41f41
jmp 0x4247e
movq 0x160(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x41fc7
movq 0x160(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, %rax
je 0x41f8d
movq 0x90(%rsp), %rdi
callq 0x3cac0
movq 0x90(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, %rax
je 0x41fc2
movq 0x88(%rsp), %rdi
callq 0x3cac0
movq 0x88(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x421e6
movq 0x158(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x420be
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x80(%rsp)
leaq 0x882dd(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
xorl %r8d, %r8d
movl %r8d, %ecx
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x42022
movq 0x138(%rsp), %rax
movq 0x80(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x160(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x42062
movq 0x78(%rsp), %rdi
callq 0x3cac0
movq 0x78(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
je 0x4208e
movq 0x70(%rsp), %rdi
callq 0x3cac0
movq 0x70(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x421e4
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x4248a
movq 0x158(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x4211c
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x158(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpq $0x0, %rax
je 0x42117
movq 0x68(%rsp), %rdi
callq 0x3cac0
movq 0x68(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x421e2
movl 0x16c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movq 0x160(%rsp), %rsi
callq 0x3f8b0
jmp 0x4214b
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq 0x158(%rsp), %rsi
callq 0x3f8b0
jmp 0x4216f
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
movl 0x5c(%rsp), %esi
movq 0x138(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x421e2
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
jmp 0x421e4
jmp 0x421e6
jmp 0x4247e
movq 0x160(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x4225a
movq 0x160(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x42229
movq 0x48(%rsp), %rdi
callq 0x3cac0
movq 0x48(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x42255
movq 0x40(%rsp), %rdi
callq 0x3cac0
movq 0x40(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x42467
movq 0x158(%rsp), %rdi
callq 0x3cf90
cmpl $0x0, %eax
je 0x42345
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movl $0x10, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
xorl %r8d, %r8d
movl %r8d, %ecx
movsd 0x68d7e(%rip), %xmm0    # 0xab020
movl %r8d, %esi
movq %rcx, %rdx
callq 0x3c6e0
jmp 0x422af
movq 0x138(%rsp), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x160(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x422ec
movq 0x30(%rsp), %rdi
callq 0x3cac0
movq 0x30(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
movq 0x158(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x42318
movq 0x28(%rsp), %rdi
callq 0x3cac0
movq 0x28(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x42465
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x10, %esi
callq 0x4290
jmp 0x4248a
movq 0x158(%rsp), %rdi
callq 0x3cfd0
cmpl $0x0, %eax
je 0x423a3
movq 0x138(%rsp), %rax
movl $0x1, 0x8(%rax)
movq 0x160(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x158(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x4239e
movq 0x20(%rsp), %rdi
callq 0x3cac0
movq 0x20(%rsp), %rdi
movl $0x10, %esi
callq 0x4290
jmp 0x42463
movl 0x16c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq 0x160(%rsp), %rsi
callq 0x3f8b0
jmp 0x423d2
movl $0x28, %edi
callq 0x4280
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movq 0x158(%rsp), %rsi
callq 0x3f8b0
jmp 0x423f6
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl 0x14(%rsp), %esi
movq 0x138(%rsp), %rdi
movl $0x1, %r8d
callq 0x42b30
jmp 0x42463
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x28, %esi
callq 0x4290
jmp 0x4248a
jmp 0x42465
jmp 0x42467
jmp 0x4247e
movl 0x16c(%rsp), %esi
leaq 0x6a586(%rip), %rdi     # 0xac9fd
movb $0x0, %al
callq 0x4c6e0
jmp 0x42480
jmp 0x42482
addq $0x178, %rsp            # imm = 0x178
retq
movq 0x148(%rsp), %rdi
callq 0x44b0
nopw (%rax,%rax)