// File: /workspace/github_c_makefile_star2_O3/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
movl 0x1dda3(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x2196f
decl %eax
movl %eax, 0x1dd8b(%rip)     # 0x3f2b8
leaq 0x2708c(%rip), %r13     # 0x485c0
movq (%r13,%rax,8), %rbx
movb 0x10(%rbx), %r14b
movq %rbx, %rdi
callq 0x8ba5
cmpb $0x1, %r14b
jne 0x218eb
callq 0x15d88
movl 0x1dd5e(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %r14d      # imm = 0xFFFE795F
leal (%rax,%r14), %ecx
cmpl %r14d, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1dd43(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r15
movq %rbx, %rdi
callq 0x8ba5
callq 0xe5b8
movl 0x1dd2b(%rip), %eax     # 0x3f2b8
leal (%rax,%r14), %ecx
cmpl %r14d, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1dd16(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2197b
testl %eax, %eax
cmoveq %r15, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2197b
testl %eax, %eax
cmoveq %rbx, %r14
movb 0x10(%r14), %bl
movq %r14, %rdi
callq 0x8ba5
cmpb $0x1, %bl
jne 0x218eb
callq 0x146a2
callq 0x15d88
movl 0x1dcc9(%rip), %r15d    # 0x3f2b8
movl $0xfffe795f, %eax       # imm = 0xFFFE795F
leal (%r15,%rax), %ecx
cmpl %eax, %ecx
jbe 0x2196f
decl %r15d
movl %r15d, 0x1dcae(%rip)    # 0x3f2b8
movq (%r13,%r15,8), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2197b
testl %eax, %eax
cmoveq %r14, %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x21959
movq (%rbx), %rdi
callq 0x8ba5
cmpb $0x1, 0x10(%rbx)
leaq 0x8(%rbx), %rax
leaq 0x243bf(%rip), %r14     # 0x45a00
cmovneq %r14, %rax
movq (%rax), %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x21665
movq (%rbx), %rdi
callq 0x8ba5
callq 0x214fe
leaq 0x8(%rbx), %rax
cmpb $0x1, 0x10(%rbx)
jmp 0x21641
movl 0x1dc4d(%rip), %edi     # 0x3f2b8
subl %r15d, %edi
callq 0x8b7d
callq 0x96e2
movl 0x1dc3a(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x2196f
decl %eax
movl %eax, 0x1dc22(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r15
movb 0x10(%r15), %bl
movq %r15, %rdi
callq 0x8ba5
cmpb $0x1, %bl
jne 0x218eb
callq 0xe915
movl 0x1dbfd(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x2196f
movq %r15, -0x48(%rbp)
leal -0x1(%rax), %ecx
movl %ecx, 0x1dbe0(%rip)     # 0x3f2b8
cmpl $0x1, %eax
jbe 0x2196f
movq (%r13,%rcx,8), %r15
addl $-0x2, %eax
movl %eax, 0x1dbc9(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %rdi
callq 0x8ba5
callq 0xe915
movl 0x1dbb4(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ebx       # imm = 0xFFFE795F
leal (%rax,%rbx), %ecx
cmpl %ebx, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1db9c(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %rax
movq %rax, -0x30(%rbp)
movq %r15, %rdi
callq 0x8ba5
movl $0x2, %edi
callq 0xacef
movl 0x1db7b(%rip), %eax     # 0x3f2b8
leal (%rax,%rbx), %ecx
cmpl %ebx, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1db68(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r15
cmpb $0x1, 0x10(%r15)
movq %r14, %rax
cmoveq %r15, %rax
movq (%rax), %rax
cmpq 0x26b35(%rip), %rax     # 0x482a0
jne 0x21897
movq -0x30(%rbp), %rsi
movb 0x10(%rsi), %cl
cmpb $0x1, %cl
movq %r14, %rdx
cmoveq %rsi, %rdx
cmpq %rax, (%rdx)
jne 0x21897
movq -0x30(%rbp), %rax
addq $0x8, %rax
cmpb $0x1, %cl
cmovneq %r14, %rax
movq (%rax), %rbx
cmpb $0x1, 0x10(%rbx)
jne 0x21897
leaq 0x10(%rbx), %rcx
leaq 0x8(%r15), %rax
movq %rax, -0x40(%rbp)
movl $0xfffe795f, %r12d      # imm = 0xFFFE795F
movq %r15, -0x38(%rbp)
movq %rcx, -0x50(%rbp)
cmpb $0x1, 0x10(%r15)
movq %r14, %rax
cmoveq -0x40(%rbp), %rax
movq (%rax), %rax
cmpb $0x1, 0x10(%rax)
cmovneq %r14, %rax
movq (%rax), %rdi
callq 0x8ba5
cmpb $0x1, 0x10(%rbx)
movq %r14, %rax
cmoveq %rbx, %rax
movq (%rax), %rdi
callq 0x8ba5
callq 0xe218
movl 0x1dab9(%rip), %eax     # 0x3f2b8
leal (%rax,%r12), %ecx
cmpl %r12d, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1daa4(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r12
movq %r15, %rdi
callq 0x8ba5
movq %r12, %rdi
callq 0x8ba5
movq -0x30(%rbp), %rdi
callq 0x8ba5
movl $0x2, %edi
callq 0xacef
callq 0xbfb4
movl 0x1da71(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %edx       # imm = 0xFFFE795F
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
jbe 0x2196f
decl %eax
movl %eax, 0x1da59(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %rdi
callq 0xb477
testl %eax, %eax
jne 0x218e6
addq $0x8, %rbx
movq -0x50(%rbp), %rax
cmpb $0x1, (%rax)
cmovneq %r14, %rbx
movq (%rbx), %rbx
leaq 0x10(%rbx), %rcx
cmpb $0x1, 0x10(%rbx)
movq -0x38(%rbp), %r15
movl $0xfffe795f, %r12d      # imm = 0xFFFE795F
je 0x217bd
movq %r15, %r14
movq %r15, %rdi
callq 0x8ba5
movq -0x30(%rbp), %rdi
callq 0x8ba5
callq 0xe218
movl 0x1da02(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x2196f
decl %eax
movl %eax, 0x1d9ea(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r12
movq %r12, %rdi
movq -0x48(%rbp), %rbx
movq %rbx, %rsi
callq 0x2197b
testl %eax, %eax
je 0x218fa
movq %r12, %rdi
jmp 0x2195c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x8ba5
movq %r14, %rdi
callq 0x8ba5
callq 0xe218
callq 0x1f007
movq 0xf4ad4(%rip), %rdi     # 0x1163f0
callq 0x8ba5
callq 0xa573
movl 0x1d98c(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x2196f
decl %eax
movl %eax, 0x1d978(%rip)     # 0x3f2b8
movq (%r13,%rax,8), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2197b
testl %eax, %eax
je 0x21959
movq %r14, %rdi
jmp 0x2195c
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0xd5cb(%rip), %rdi      # 0x2ef41
callq 0x780b