// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
callq 0x8b78
movq %rax, %r14
callq 0x8b78
movl 0x1d4c6(%rip), %r12d    # 0x3a2b8
movq %rax, %rdi
movq %r14, %rsi
callq 0x1957f
movslq 0x1d4b4(%rip), %r15     # 0x3a2b8
movl %r15d, %r14d
subl %r12d, %r14d
jle 0x1ce39
movslq %r12d, %rax
movl %r14d, %ecx
shlq $0x3, %rax
xorl %edx, %edx
leaq 0x267a1(%rip), %rsi     # 0x435c0
leaq (%rax,%rdx,8), %rdi
movq (%rsi,%rdi), %rdi
cmpb $0x4, 0x10(%rdi)
jne 0x1cfa6
incq %rdx
cmpq %rdx, %rcx
jne 0x1ce1f
cmpl $0x2, %r14d
jl 0x1ce79
leaq 0x2677a(%rip), %rbx     # 0x435c0
movl %r12d, %edi
movl %r14d, %esi
callq 0x1cfb2
testl %eax, %eax
je 0x1ce79
movslq 0x1d45c(%rip), %rax     # 0x3a2b8
movq -0x8(%rbx,%rax,8), %rdx
movl %r12d, %edi
movl %r14d, %esi
callq 0x1d210
leal -0x1(%r14), %eax
cmpl $0x2, %r14d
movl %eax, %r14d
jg 0x1ce46
movslq 0x1d438(%rip), %rax     # 0x3a2b8
movq %rax, %r14
subq %r15, %r14
testl %r14d, %r14d
je 0x1cf39
movl %r12d, -0x2c(%rbp)
leaq 0x26726(%rip), %r12     # 0x435c0
leaq (%r12,%rax,8), %rdi
leaq (,%r14,8), %rax
subq %rax, %rdi
leaq -0x14031(%rip), %rcx    # 0x8e7f
movl $0x8, %edx
movq %r14, %rsi
callq 0x70a0
cmpl $0x2, %r14d
jl 0x1cf24
leal -0x1(%r14), %ebx
xorl %r13d, %r13d
leal (%r15,%r13), %eax
cltq
movq (%r12,%rax,8), %rdi
movq 0x8(%r12,%rax,8), %rsi
callq 0x8c79
testl %eax, %eax
je 0x1cee7
movl %r14d, %ebx
jmp 0x1cf15
leal 0x1(%r13), %eax
cmpl %ebx, %eax
jge 0x1cf12
cltq
addq %r15, %rax
leaq (%r12,%rax,8), %rax
movl %ebx, %ecx
subl %r13d, %ecx
movl $0x1, %edx
movq (%rax,%rdx,8), %rsi
movq %rsi, -0x8(%rax,%rdx,8)
incq %rdx
cmpl %edx, %ecx
jne 0x1cf02
decl %r13d
movl %ebx, %r14d
incl %r13d
leal -0x1(%r14), %ebx
cmpl %ebx, %r13d
jl 0x1ceca
cmpl $0x1, %r14d
jne 0x1cf49
movq (%r12,%r15,8), %rdi
movl -0x2c(%rbp), %eax
movl %eax, 0x1d381(%rip)     # 0x3a2b8
jmp 0x1cf93
movl %r12d, 0x1d378(%rip)    # 0x3a2b8
movq 0x23ab9(%rip), %rdi     # 0x40a00
jmp 0x1cf93
movl %r14d, %edi
callq 0x7850
movq (%rax), %rcx
movl $0x1, (%rcx)
movq (%rax), %rcx
movl %r14d, 0x4(%rcx)
testl %r14d, %r14d
jle 0x1cf87
movl %r14d, %ecx
shlq $0x3, %r15
xorl %edx, %edx
leaq (%r15,%rdx,8), %rsi
movq (%r12,%rsi), %rsi
movq (%rax), %rdi
movq %rsi, 0x68(%rdi,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x1cf6f
movl -0x2c(%rbp), %ecx
movl %ecx, 0x1d328(%rip)     # 0x3a2b8
movq %rax, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
leaq 0xd8f7(%rip), %rdi      # 0x2a8a4
callq 0x77c8