// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: roots
void
roots(void)
{
	int h, i, j, k, n;
	struct atom *A, *P, *X;

	X = pop();
	P = pop();

	h = tos;

	coeffs(P, X); // put coeffs on stack

	k = tos;

	n = k - h; // number of coeffs on stack

	// check coeffs

	for (i = 0; i < n; i++)
		if (!isrational(stack[h + i]))
			stopf("roots: coeffs");

	// find roots

	while (n > 1) {

		if (findroot(h, n) == 0)
			break; // no root found

		// A is the root

		A = stack[tos - 1];

		// divide p(x) by X - A

		reduce(h, n, A);

		n--;
	}

	n = tos - k; // number of roots on stack

	if (n == 0) {
		tos = h; // pop all
		push_symbol(NIL); // no roots
		return;
	}

	sort(n); // sort roots

	// eliminate repeated roots

	for (i = 0; i < n - 1; i++)
		if (equal(stack[k + i], stack[k + i + 1])) {
			for (j = i + 1; j < n - 1; j++)
				stack[k + j] = stack[k + j + 1];
			i--;
			n--;
		}

	if (n == 1) {
		A = stack[k];
		tos = h; // pop all
		push(A); // one root
		return;
	}

	A = alloc_vector(n);

	for (i = 0; i < n; i++)
		A->u.tensor->elem[i] = stack[k + i];

	tos = h; // pop all

	push(A);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x868d
movq %rax, %r14
callq 0x868d
movslq 0x1e252(%rip), %rbx     # 0x382a8
movq %rax, %rdi
movq %r14, %rsi
callq 0x16fdc
movslq 0x1e240(%rip), %r12     # 0x382a8
movl %r12d, %r15d
subl %ebx, %r15d
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movq %rbx, %rdx
shlq $0x3, %rdx
leaq 0x27526(%rip), %r13     # 0x415b0
cmpq %rax, %rcx
je 0x1a0d7
leaq (%rdx,%rax,8), %rsi
movq (%r13,%rsi), %rsi
incq %rax
cmpb $0x4, 0x10(%rsi)
je 0x1a08a
leaq 0xd854(%rip), %rdi      # 0x278fc
callq 0x7756
movl %ebx, %edi
movl %r15d, %esi
callq 0x1a1d7
movl 0x1e1ea(%rip), %r14d    # 0x382a8
testl %eax, %eax
je 0x1a0e4
movslq %r14d, %rax
movq -0x8(%r13,%rax,8), %rdx
movl %ebx, %edi
movl %r15d, %esi
callq 0x1a40f
decl %r15d
cmpl $0x1, %r15d
jg 0x1a0ad
movl 0x1e1c4(%rip), %r14d    # 0x382a8
subl %r12d, %r14d
je 0x1a174
movq %rbx, -0x38(%rbp)
movl %r14d, %edi
callq 0x88d6
movq %r12, -0x30(%rbp)
leaq 0x8(,%r12,8), %r15
addq %r13, %r15
xorl %r12d, %r12d
leal -0x1(%r14), %ebx
cmpl %ebx, %r12d
jge 0x1a159
movq -0x30(%rbp), %rax
addl %r12d, %eax
cltq
movq (%r13,%rax,8), %rdi
movq 0x8(%r13,%rax,8), %rsi
callq 0x8760
testl %eax, %eax
je 0x1a135
movl %r14d, %ebx
jmp 0x1a151
movslq %r12d, %rax
movslq %ebx, %rcx
incq %rax
cmpq %rcx, %rax
jge 0x1a14e
movq (%r15,%rax,8), %rdx
movq %rdx, -0x8(%r15,%rax,8)
jmp 0x1a13b
decl %r12d
incl %r12d
movl %ebx, %r14d
jmp 0x1a10b
cmpl $0x1, %r14d
jne 0x1a183
movq -0x30(%rbp), %rax
movq (%r13,%rax,8), %rdi
movq -0x38(%rbp), %rax
movl %eax, 0x1e136(%rip)     # 0x382a8
jmp 0x1a1c4
movl %ebx, 0x1e12e(%rip)     # 0x382a8
movq 0x2486f(%rip), %rdi     # 0x3e9f0
jmp 0x1a1c4
movl %r14d, %edi
callq 0x77b7
movq %rax, %rdi
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
movq -0x30(%rbp), %r8
shlq $0x3, %r8
movq -0x38(%rbp), %rsi
cmpq %rax, %r14
je 0x1a1be
leaq (%r8,%rax,8), %rcx
movq (%r13,%rcx), %rcx
movq (%rdi), %rdx
movq %rcx, 0x68(%rdx,%rax,8)
incq %rax
jmp 0x1a1a3
movl %esi, 0x1e0e4(%rip)     # 0x382a8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x861b