// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
callq 0x8b78
movq %rax, %rbx
movb 0x10(%rax), %r14b
movq %rax, %rdi
callq 0x8b01
cmpb $0x1, %r14b
jne 0x1ee84
callq 0x145aa
callq 0x8b78
movq %rax, %r15
movq %rbx, %rdi
callq 0x8b01
callq 0xdc9f
callq 0x8b78
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x1ee93
testl %eax, %eax
cmoveq %r15, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1ee93
testl %eax, %eax
cmoveq %rbx, %r14
movb 0x10(%r14), %bl
movq %r14, %rdi
callq 0x8b01
cmpb $0x1, %bl
jne 0x1ee84
callq 0x1319a
callq 0x145aa
callq 0x8b78
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x1ee93
testl %eax, %eax
cmoveq %r14, %r12
cmpb $0x1, 0x10(%r12)
jne 0x1ee6e
movl 0x1b687(%rip), %ebx     # 0x3a2b8
movq (%r12), %rdi
callq 0x8b01
cmpb $0x1, 0x10(%r12)
leaq 0x8(%r12), %rax
leaq 0x21db4(%rip), %r13     # 0x40a00
cmovneq %r13, %rax
movq (%rax), %r14
cmpb $0x1, 0x10(%r14)
jne 0x1ec72
movq (%r14), %rdi
callq 0x8b01
callq 0x1eb74
leaq 0x8(%r14), %rax
cmpb $0x1, 0x10(%r14)
jmp 0x1ec4c
movl 0x1b640(%rip), %edi     # 0x3a2b8
subl %ebx, %edi
callq 0x8ad9
callq 0x95e2
callq 0x8b78
movq %rax, %r14
movb 0x10(%rax), %bl
movq %rax, %rdi
callq 0x8b01
cmpb $0x1, %bl
jne 0x1ee84
movq %r14, -0x40(%rbp)
callq 0xdee7
callq 0x8b78
movq %rax, %rbx
callq 0x8b78
movq %rax, %rdi
callq 0x8b01
callq 0xdee7
callq 0x8b78
movq %rax, %r12
movq %rbx, %rdi
callq 0x8b01
movl $0x2, %edi
callq 0xa8bb
callq 0x8b78
movq %rax, %r15
cmpb $0x1, 0x10(%rax)
movq %r13, %rax
cmoveq %r15, %rax
movq 0x245a9(%rip), %rcx     # 0x432a0
cmpq %rcx, (%rax)
jne 0x1edf4
movb 0x10(%r12), %al
cmpb $0x1, %al
movq %r13, %rdx
cmoveq %r12, %rdx
cmpq %rcx, (%rdx)
jne 0x1edf4
movq %r12, %rcx
addq $0x8, %rcx
cmpb $0x1, %al
cmovneq %r13, %rcx
movq (%rcx), %r14
cmpb $0x1, 0x10(%r14)
jne 0x1edf4
leaq 0x10(%r14), %rcx
movq %r15, %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq %r15, -0x30(%rbp)
movq %rcx, -0x48(%rbp)
cmpb $0x1, 0x10(%r15)
movq %r13, %rax
cmoveq -0x38(%rbp), %rax
movq (%rax), %rax
cmpb $0x1, 0x10(%rax)
cmovneq %r13, %rax
movq (%rax), %rdi
callq 0x8b01
cmpb $0x1, 0x10(%r14)
movq %r13, %rax
cmoveq %r14, %rax
movq (%rax), %rdi
callq 0x8b01
callq 0xd9a4
callq 0x8b78
movq %r12, %rbx
movq %rax, %r12
movq %r15, %rdi
callq 0x8b01
movq %r12, %rdi
callq 0x8b01
movq %rbx, %r15
movq %rbx, %rdi
callq 0x8b01
movl $0x2, %edi
callq 0xa8bb
callq 0xba6d
callq 0x8b78
movq %rax, %rdi
callq 0xaece
testl %eax, %eax
jne 0x1ee6e
addq $0x8, %r14
movq -0x48(%rbp), %rax
cmpb $0x1, (%rax)
cmovneq %r13, %r14
movq (%r14), %r14
leaq 0x10(%r14), %rcx
cmpb $0x1, 0x10(%r14)
movq %r15, %r12
movq -0x30(%rbp), %r15
je 0x1ed45
movq %r15, %rdi
callq 0x8b01
movq %r12, %rdi
callq 0x8b01
callq 0xd9a4
callq 0x8b78
movq %r12, %rbx
movq %rax, %r12
movq %rax, %rdi
movq -0x40(%rbp), %r14
movq %r14, %rsi
callq 0x1ee93
testl %eax, %eax
jne 0x1ee6e
movq %rbx, %rdi
callq 0x8b01
movq %r15, %rdi
callq 0x8b01
callq 0xd9a4
callq 0x1cc2b
movq 0xf25a8(%rip), %rdi     # 0x1113f0
callq 0x8b01
callq 0xa234
callq 0x8b78
movq %r14, %r12
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x1ee93
testl %eax, %eax
cmovneq %r14, %r12
movq %r12, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq