// File: /workspace/github_c_makefile_star2_O3/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
movl 0x2009e(%rip), %r15d    # 0x3f2b8
movl $0xfffe795f, %eax       # imm = 0xFFFE795F
leal (%r15,%rax), %ecx
cmpl %eax, %ecx
jbe 0x1f419
leal -0x1(%r15), %eax
movl %eax, 0x20083(%rip)     # 0x3f2b8
cmpl $0x1, %r15d
jbe 0x1f419
movl %eax, %eax
leaq 0x29378(%rip), %r12     # 0x485c0
movq (%r12,%rax,8), %rsi
leal -0x2(%r15), %ebx
movl %ebx, 0x20062(%rip)     # 0x3f2b8
movq (%r12,%rbx,8), %rdi
callq 0x1b4c5
movl 0x20052(%rip), %r13d    # 0x3f2b8
movl %r13d, %eax
movl %r13d, %ecx
subl %ebx, %eax
jle 0x1f2d9
movl %ebx, %ecx
movl %eax, %edx
shlq $0x3, %rcx
xorl %esi, %esi
leaq (%rcx,%rsi,8), %rdi
movq (%r12,%rdi), %rdi
cmpb $0x4, 0x10(%rdi)
jne 0x1f40d
incq %rsi
cmpq %rsi, %rdx
jne 0x1f27a
movl %r13d, %ecx
cmpl $0x1, %eax
je 0x1f2d9
movl %r13d, %r14d
subl %r15d, %r14d
addl $0x3, %r14d
decl %r14d
movl %ebx, %edi
movl %r14d, %esi
callq 0x1f425
testl %eax, %eax
je 0x1f2d3
movslq 0x1fffa(%rip), %rax     # 0x3f2b8
movq -0x8(%r12,%rax,8), %rdx
movl %ebx, %edi
movl %r14d, %esi
callq 0x1f756
cmpl $0x2, %r14d
jg 0x1f2a6
movl 0x1ffdf(%rip), %ecx     # 0x3f2b8
movl %ecx, %r14d
subl %r13d, %r14d
je 0x1f39b
movq %rbx, -0x38(%rbp)
movslq %r13d, %rax
movq %rax, -0x30(%rbp)
movslq %ecx, %rax
leaq (%r12,%rax,8), %rdi
movslq %r14d, %r15
leaq (,%r15,8), %rax
subq %rax, %rdi
leaq -0x163d7(%rip), %rcx    # 0x8f35
movl $0x8, %edx
movq %r15, %rsi
callq 0x70a0
cmpl $0x2, %r15d
jl 0x1f381
leal -0x1(%r14), %ebx
xorl %r15d, %r15d
leal (%r15,%r13), %eax
cltq
movq (%r12,%rax,8), %rdi
movq 0x8(%r12,%rax,8), %rsi
callq 0x8d48
testl %eax, %eax
je 0x1f343
movl %r14d, %ebx
jmp 0x1f372
leal 0x1(%r15), %eax
cmpl %ebx, %eax
jge 0x1f36f
cltq
addq -0x30(%rbp), %rax
leaq (%r12,%rax,8), %rax
movl %ebx, %ecx
subl %r15d, %ecx
movl $0x1, %edx
movq (%rax,%rdx,8), %rsi
movq %rsi, -0x8(%rax,%rdx,8)
incq %rdx
cmpl %edx, %ecx
jne 0x1f35f
decl %r15d
movl %ebx, %r14d
incl %r15d
leal -0x1(%r14), %ebx
cmpl %ebx, %r15d
jl 0x1f326
cmpl $0x1, %r14d
jne 0x1f3aa
movq -0x30(%rbp), %rax
movq (%r12,%rax,8), %rdi
movq -0x38(%rbp), %rax
movl %eax, 0x1ff1f(%rip)     # 0x3f2b8
jmp 0x1f3fa
movl %ebx, 0x1ff17(%rip)     # 0x3f2b8
movq 0x26658(%rip), %rdi     # 0x45a00
jmp 0x1f3fa
movl %r14d, %edi
callq 0x7893
movq (%rax), %rcx
movl $0x1, (%rcx)
movq (%rax), %rcx
movl %r14d, 0x4(%rcx)
testl %r14d, %r14d
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r9
jle 0x1f3f0
movl %r14d, %ecx
shlq $0x3, %r9
xorl %edx, %edx
leaq (%r9,%rdx,8), %rsi
movq (%r12,%rsi), %rsi
movq (%rax), %rdi
movq %rsi, 0x68(%rdi,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x1f3d8
movl %r8d, 0x1fec1(%rip)     # 0x3f2b8
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0xf4b0(%rip), %rdi      # 0x2e8c4
callq 0x780b
leaq 0xfb21(%rip), %rdi      # 0x2ef41
callq 0x780b