// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: nfindroot
void
nfindroot(double cr[], double ci[], int n, double *par, double *pai)
{
	int i, j;
	double d;
	double ar, br, dfr, dxr, far, fbr, xr, yr;
	double ai, bi, dfi, dxi, fai, fbi, xi, yi;

	// if const term is zero then root is zero

	// note: use exact zero, not "close to zero"

	// term will be exactly zero from coeffs(), no need for arbitrary cutoff

	if (cr[0] == 0.0 && ci[0] == 0.0) {
		*par = 0.0;
		*pai = 0.0;
		return;
	}

	// secant method

	for (i = 0; i < 100; i++) {

		ar = urandom();
		ai = urandom();

		fata(cr, ci, n, ar, ai, &far, &fai);

		br = ar;
		bi = ai;

		fbr = far;
		fbi = fai;

		ar = urandom();
		ai = urandom();

		for (j = 0; j < 1000; j++) {

			fata(cr, ci, n, ar, ai, &far, &fai);

			if (zabs(far, fai) < EPSILON) {
				*par = ar;
				*pai = ai;
				return;
			}

			if (zabs(far, fai) < zabs(fbr, fbi)) {

				xr = ar;
				xi = ai;

				ar = br;
				ai = bi;

				br = xr;
				bi = xi;

				xr = far;
				xi = fai;

				far = fbr;
				fai = fbi;

				fbr = xr;
				fbi = xi;
			}

			// dx = b - a

			dxr = br - ar;
			dxi = bi - ai;

			// df = fb - fa

			dfr = fbr - far;
			dfi = fbi - fai;

			// y = dx / df

			d = dfr * dfr + dfi * dfi;

			if (d == 0.0)
				break;

			yr = (dxr * dfr + dxi * dfi) / d;
			yi = (dxi * dfr - dxr * dfi) / d;

			// a = b - y * fb

			ar = br - (yr * fbr - yi * fbi);
			ai = bi - (yr * fbi + yi * fbr);
		}
	}

	stopf("nroots: convergence error");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r13
movsd (%rdi), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x170b1
jp 0x170b1
movsd (%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x170b1
jp 0x170b1
xorpd %xmm0, %xmm0
jmp 0x172dc
movq %rcx, -0x78(%rbp)
movq %r8, -0x80(%rbp)
xorl %eax, %eax
leaq -0x58(%rbp), %r14
movl %eax, -0x5c(%rbp)
cmpl $0x64, %eax
je 0x172f4
callq 0x173ac
movsd %xmm0, -0x38(%rbp)
callq 0x173ac
movapd %xmm0, %xmm1
movq %r13, %rdi
movq %r12, %rsi
movl %r15d, %edx
movsd -0x38(%rbp), %xmm0
movsd %xmm1, -0x50(%rbp)
leaq -0x40(%rbp), %rcx
movq %r14, %r8
callq 0x173d3
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
callq 0x173ac
movsd %xmm0, -0x30(%rbp)
movl $0x3e8, %ebx            # imm = 0x3E8
callq 0x173ac
movsd -0x30(%rbp), %xmm5
movsd -0x38(%rbp), %xmm2
movapd %xmm0, %xmm9
subl $0x1, %ebx
jb 0x172c0
movsd %xmm2, -0x38(%rbp)
movq %r13, %rdi
movq %r12, %rsi
movl %r15d, %edx
movsd %xmm5, -0x30(%rbp)
movapd %xmm5, %xmm0
movsd %xmm9, -0x48(%rbp)
movapd %xmm9, %xmm1
leaq -0x40(%rbp), %rcx
movq %r14, %r8
callq 0x173d3
movsd -0x40(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
sqrtsd %xmm3, %xmm2
movsd 0xbffb(%rip), %xmm3     # 0x23190
ucomisd %xmm2, %xmm3
ja 0x172ca
movsd -0x68(%rbp), %xmm7
movapd %xmm7, %xmm3
mulsd %xmm7, %xmm3
movsd -0x70(%rbp), %xmm5
movapd %xmm5, %xmm4
mulsd %xmm5, %xmm4
addsd %xmm3, %xmm4
sqrtsd %xmm4, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x171f5
movsd %xmm5, -0x40(%rbp)
movsd %xmm7, -0x58(%rbp)
movapd %xmm7, %xmm4
movapd %xmm5, %xmm6
movsd -0x50(%rbp), %xmm3
movsd -0x48(%rbp), %xmm2
movsd %xmm2, -0x50(%rbp)
movsd -0x30(%rbp), %xmm2
movsd -0x38(%rbp), %xmm8
jmp 0x17215
movapd %xmm1, %xmm4
movapd %xmm0, %xmm6
movapd %xmm5, %xmm0
movsd -0x48(%rbp), %xmm3
movsd -0x38(%rbp), %xmm2
movsd -0x30(%rbp), %xmm8
movapd %xmm7, %xmm1
movapd %xmm0, %xmm5
subsd %xmm6, %xmm5
movapd %xmm1, %xmm6
subsd %xmm4, %xmm6
movapd %xmm6, %xmm4
mulsd %xmm6, %xmm4
movapd %xmm5, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm4, %xmm7
ucomisd 0xbf2f(%rip), %xmm7     # 0x23170
jne 0x17245
jnp 0x172c0
movsd -0x50(%rbp), %xmm9
movapd %xmm9, %xmm4
subsd %xmm3, %xmm4
movapd %xmm2, %xmm3
subsd %xmm8, %xmm3
movapd %xmm4, %xmm8
mulsd %xmm6, %xmm8
mulsd %xmm3, %xmm6
mulsd %xmm5, %xmm3
addsd %xmm8, %xmm3
divsd %xmm7, %xmm3
mulsd %xmm5, %xmm4
subsd %xmm6, %xmm4
divsd %xmm7, %xmm4
movapd %xmm1, %xmm5
mulsd %xmm4, %xmm5
movapd %xmm3, %xmm6
mulsd %xmm0, %xmm6
subsd %xmm5, %xmm6
movapd %xmm2, %xmm5
subsd %xmm6, %xmm5
mulsd %xmm0, %xmm4
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
subsd %xmm3, %xmm9
movsd %xmm0, -0x70(%rbp)
movsd %xmm1, -0x68(%rbp)
jmp 0x17134
movl -0x5c(%rbp), %eax
incl %eax
jmp 0x170bf
movsd -0x30(%rbp), %xmm1
movq -0x80(%rbp), %r8
movq -0x78(%rbp), %rcx
movsd -0x48(%rbp), %xmm0
movsd %xmm1, (%rcx)
movsd %xmm0, (%r8)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1060f(%rip), %rdi     # 0x2790a
callq 0x7756