// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: cmp_terms
int
cmp_terms(struct atom *p1, struct atom *p2)
{
	int a, b, c;

	// 1st level: imaginary terms on the right

	a = isimaginaryterm(p1);
	b = isimaginaryterm(p2);

	if (a == 0 && b == 1)
		return -1; // ok

	if (a == 1 && b == 0)
		return 1; // out of order

	// 2nd level: numericals on the right

	if (isnum(p1) && isnum(p2))
		return 0; // don't care about order, save time, don't compare

	if (isnum(p1))
		return 1; // out of order

	if (isnum(p2))
		return -1; // ok

	// 3rd level: sort by factors

	a = 0;
	b = 0;

	if (car(p1) == symbol(MULTIPLY)) {
		p1 = cdr(p1);
		a = 1; // p1 is a list of factors
		if (isnum(car(p1))) {
			// skip over coeff
			p1 = cdr(p1);
			if (cdr(p1) == symbol(NIL)) {
				p1 = car(p1);
				a = 0;
			}
		}
	}

	if (car(p2) == symbol(MULTIPLY)) {
		p2 = cdr(p2);
		b = 1; // p2 is a list of factors
		if (isnum(car(p2))) {
			// skip over coeff
			p2 = cdr(p2);
			if (cdr(p2) == symbol(NIL)) {
				p2 = car(p2);
				b = 0;
			}
		}
	}

	if (a == 0 && b == 0)
		return cmp_factors(p1, p2);

	if (a == 0 && b == 1) {
		c = cmp_factors(p1, car(p2));
		if (c == 0)
			c = -1; // lengthf(p1) < lengthf(p2)
		return c;
	}

	if (a == 1 && b == 0) {
		c = cmp_factors(car(p1), p2);
		if (c == 0)
			c = 1; // lengthf(p1) > lengthf(p2)
		return c;
	}

	while (iscons(p1) && iscons(p2)) {
		c = cmp_factors(car(p1), car(p2));
		if (c)
			return c;
		p1 = cdr(p1);
		p2 = cdr(p2);
	}

	if (iscons(p1))
		return 1; // lengthf(p1) > lengthf(p2)

	if (iscons(p2))
		return -1; // lengthf(p1) < lengthf(p2)

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
callq 0xabcf
movl %eax, %r15d
movq %r14, %rdi
callq 0xabcf
movl %eax, %ecx
testl %r15d, %r15d
sete %dl
testl %eax, %eax
setne %sil
pushq $-0x1
popq %rax
testb %sil, %dl
jne 0xa9f5
testl %r15d, %r15d
setne %dl
testl %ecx, %ecx
sete %cl
pushq $0x1
popq %r15
testb %cl, %dl
je 0xa9cb
movl %r15d, %eax
jmp 0xa9f5
movb 0x10(%rbx), %dl
movl %edx, %edi
andb $-0x2, %dil
movb 0x10(%r14), %cl
movl %ecx, %esi
andb $-0x2, %sil
cmpb $0x4, %dil
jne 0xa9ef
xorl %eax, %eax
cmpb $0x4, %sil
setne %al
jmp 0xa9f5
cmpb $0x4, %sil
jne 0xaa04
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, %dl
leaq 0x33fe2(%rip), %r12     # 0x3e9f0
movq %r12, %rax
cmoveq %rbx, %rax
movq 0x3687c(%rip), %rsi     # 0x41298
cmpq %rsi, (%rax)
je 0xaa27
movb $0x1, %al
xorl %edx, %edx
jmp 0xaa8e
addq $0x8, %rbx
cmpb $0x1, %dl
cmovneq %r12, %rbx
movq (%rbx), %rbx
movb 0x10(%rbx), %al
cmpb $0x1, %al
movq %r12, %rdx
cmoveq %rbx, %rdx
movq (%rdx), %rdx
movb 0x10(%rdx), %dl
andb $-0x2, %dl
cmpb $0x4, %dl
jne 0xaa8a
addq $0x8, %rbx
cmpb $0x1, %al
cmovneq %r12, %rbx
movq (%rbx), %rdi
cmpb $0x1, 0x10(%rdi)
leaq 0x8(%rdi), %rax
setne %r8b
cmovneq %r12, %rax
movq 0x33f7d(%rip), %rbx     # 0x3e9f0
cmpq %rbx, (%rax)
sete %al
setne %dl
cmovneq %rdi, %rbx
orb %dl, %r8b
jne 0xaa8e
movq (%rdi), %rbx
jmp 0xaa21
movb $0x1, %dl
xorl %eax, %eax
cmpb $0x1, %cl
movq %r12, %rdi
cmoveq %r14, %rdi
cmpq %rsi, (%rdi)
je 0xaabe
testb %al, %al
je 0xab40
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xac3d
addq $0x8, %r14
cmpb $0x1, %cl
cmovneq %r12, %r14
movq (%r14), %r13
movb 0x10(%r13), %cl
cmpb $0x1, %cl
movq %r12, %rsi
cmoveq %r13, %rsi
movq (%rsi), %rsi
movb 0x10(%rsi), %sil
andb $-0x2, %sil
cmpb $0x4, %sil
jne 0xab19
addq $0x8, %r13
cmpb $0x1, %cl
cmovneq %r12, %r13
movq (%r13), %r13
movb 0x10(%r13), %cl
leaq 0x8(%r13), %rsi
cmpb $0x1, %cl
cmovneq %r12, %rsi
movq 0x33ee0(%rip), %r14     # 0x3e9f0
cmpq %r14, (%rsi)
je 0xabbd
testb %al, %al
je 0xab66
cmpb $0x1, %cl
cmoveq %r13, %r12
movq (%r12), %rsi
movq %rbx, %rdi
callq 0xac3d
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
sbbl %eax, %eax
orl %ecx, %eax
jmp 0xa9f5
testb %dl, %dl
je 0xab63
cmpb $0x1, 0x10(%rbx)
cmoveq %rbx, %r12
movq (%r12), %rdi
movq %r14, %rsi
callq 0xac3d
cmpl $0x1, %eax
adcl $0x0, %eax
jmp 0xa9f5
movq %r14, %r13
cmpb $0x1, 0x10(%rbx)
movb 0x10(%r13), %cl
jne 0xabae
cmpb $0x1, %cl
jne 0xa9c6
movq (%rbx), %rdi
movq (%r13), %rsi
callq 0xac3d
testl %eax, %eax
jne 0xa9f5
leaq 0x8(%rbx), %rax
cmpb $0x1, 0x10(%rbx)
cmovneq %r12, %rax
movq (%rax), %rbx
cmpb $0x1, 0x10(%r13)
leaq 0x8(%r13), %rax
cmovneq %r12, %rax
movq (%rax), %r13
jmp 0xab66
xorl %eax, %eax
cmpb $0x1, %cl
sete %al
negl %eax
jmp 0xa9f5
cmpb $0x1, %cl
jne 0xaa9d
movq (%r13), %r14
jmp 0xaa9d