// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: simplify_nib
void
simplify_nib(void)
{
	int h;
	struct atom *p1, *p2, *p3, *NUM, *DEN, *R;

	p1 = pop();

	if (!iscons(p1)) {
		push(p1);
		return;
	}

	push(p1);
	rect();
	p2 = pop();
	push(p1);
	polar();
	p3 = pop();
	if (simpler(p3, p2))
		p2 = p3;
	if (simpler(p2, p1))
		p1 = p2;

	if (!iscons(p1)) {
		push(p1);
		return;
	}

	push(p1);
	expform();
	rect();
	p2 = pop();
	if (simpler(p2, p1))
		p1 = p2;

	if (!iscons(p1)) {
		push(p1);
		return;
	}

	// depth first

	h = tos;
	push(car(p1)); // function name
	p1 = cdr(p1);
	while (iscons(p1)) {
		push(car(p1));
		simplify_nib();
		p1 = cdr(p1);
	}
	list(tos - h);
	evalf(); // normalize
	p1 = pop();

	if (!iscons(p1)) {
		push(p1);
		return;
	}

	push(p1);
	numden();
	NUM = pop();
	DEN = pop();

	// NUM / DEN = A / (B / C) = A C / B

	// for example, 1 / (x + y^2 / x) -> x / (x^2 + y^2)

	push(DEN);
	numden();
	DEN = pop();
	push(NUM);
	multiply();
	NUM = pop();

	// search for R such that NUM = R DEN

	if (car(NUM) == symbol(ADD) && car(DEN) == symbol(ADD)) {

		p2 = cdr(DEN);

		while (iscons(p2)) {

			// provisional ratio

			push(cadr(NUM)); // 1st term of numerator
			push(car(p2));
			divide();
			R = pop();

			// check

			push(NUM);
			push(R);
			push(DEN);
			multiply();
			subtract();
			p3 = pop();

			if (iszero(p3)) {
				push(R);
				return;
			}

			p2 = cdr(p2);
		}
	}

	push(NUM);
	push(DEN);
	divide();
	p2 = pop();
	if (simpler(p2, p1)) {
		push(p2);
		return;
	}

	push(DEN);
	push(NUM);
	divide();
	rationalize();
	reciprocate();
	p2 = pop();
	if (simpler(p2, p1)) {
		push(p2);
		return;
	}

	push(p1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x868d
movq %rax, %rbx
movb 0x10(%rax), %r14b
movq %rax, %rdi
callq 0x861b
cmpb $0x1, %r14b
jne 0x1bc42
callq 0x128dd
callq 0x868d
movq %rax, %r15
movq %rbx, %rdi
callq 0x861b
callq 0xcc9e
callq 0x868d
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x1bd50
testl %eax, %eax
cmoveq %r15, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bd50
testl %eax, %eax
cmoveq %rbx, %r14
movb 0x10(%r14), %bl
movq %r14, %rdi
callq 0x861b
cmpb $0x1, %bl
jne 0x1bc42
callq 0x11798
callq 0x128dd
callq 0x868d
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1bd50
testl %eax, %eax
cmoveq %r14, %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x1bc8c
movl 0x1c768(%rip), %r14d    # 0x382a8
movq (%rbx), %rdi
callq 0x861b
leaq 0x22ea1(%rip), %r13     # 0x3e9f0
leaq 0x8(%rbx), %rax
cmpb $0x1, 0x10(%rbx)
cmovneq %r13, %rax
movq (%rax), %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x1bb73
movq (%rbx), %rdi
callq 0x861b
callq 0x1ba84
jmp 0x1bb4f
movl 0x1c72f(%rip), %edi     # 0x382a8
subl %r14d, %edi
callq 0x85eb
callq 0x8fbb
callq 0x868d
movb 0x10(%rax), %bl
movq %rax, -0x38(%rbp)
movq %rax, %rdi
callq 0x861b
cmpb $0x1, %bl
jne 0x1bc42
callq 0xcea2
callq 0x868d
movq %rax, %r15
callq 0x868d
movq %rax, %rdi
callq 0x861b
callq 0xcea2
callq 0x868d
movq %rax, %r14
movq %r15, %rdi
callq 0x861b
callq 0x9fae
callq 0x868d
movq %rax, %r15
cmpb $0x1, 0x10(%rax)
movq %r13, %rax
cmoveq %r15, %rax
movq (%rax), %rax
cmpq 0x2569c(%rip), %rax     # 0x41290
jne 0x1bc0d
movb 0x10(%r14), %cl
cmpb $0x1, %cl
movq %r13, %rdx
cmoveq %r14, %rdx
cmpq %rax, (%rdx)
je 0x1bca2
movq %r15, %rdi
callq 0x861b
movq %r14, %rdi
callq 0x861b
callq 0xca0b
callq 0x868d
movq %rax, %r12
movq %rax, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x1bd50
testl %eax, %eax
je 0x1bc51
movq %r12, %rdi
jmp 0x1bc8f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x861b
movq %r15, %rdi
callq 0x861b
callq 0xca0b
callq 0x19eb0
callq 0xa03a
callq 0x868d
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1bd50
testl %eax, %eax
je 0x1bc8c
movq %r14, %rdi
jmp 0x1bc8f
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x861b
movq %r14, %rax
addq $0x8, %rax
cmpb $0x1, %cl
cmovneq %r13, %rax
movq %r15, %rcx
addq $0x8, %rcx
movq %rcx, -0x30(%rbp)
movq (%rax), %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x1bc0d
cmpb $0x1, 0x10(%r15)
movq %r13, %rax
cmoveq -0x30(%rbp), %rax
movq (%rax), %rax
cmpb $0x1, 0x10(%rax)
cmovneq %r13, %rax
movq (%rax), %rdi
callq 0x861b
cmpb $0x1, 0x10(%rbx)
movq %r13, %rax
cmoveq %rbx, %rax
movq (%rax), %rdi
callq 0x861b
callq 0xca0b
callq 0x868d
movq %rax, %r12
movq %r15, %rdi
callq 0x861b
movq %r12, %rdi
callq 0x861b
movq %r14, %rdi
callq 0x861b
callq 0x9fae
callq 0xb07f
callq 0x868d
movq %rax, %rdi
callq 0xa641
testl %eax, %eax
jne 0x1bc3d
leaq 0x8(%rbx), %rax
cmpb $0x1, 0x10(%rbx)
cmovneq %r13, %rax
jmp 0x1bcbb