// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
movq %rsi, %r14
movq %rdi, %rbx
callq 0xba80
movl %eax, %r15d
movq %r14, %rdi
callq 0xba80
movl %eax, %ecx
testl %r15d, %r15d
sete %dl
testl %eax, %eax
setne %sil
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %sil, %dl
jne 0xb8a1
testl %r15d, %r15d
setne %dl
testl %ecx, %ecx
sete %cl
testb %cl, %dl
je 0xb877
movl $0x1, %eax
jmp 0xb8a1
movb 0x10(%rbx), %dl
movl %edx, %edi
andb $-0x2, %dil
movb 0x10(%r14), %cl
movl %ecx, %esi
andb $-0x2, %sil
cmpb $0x4, %dil
jne 0xb89b
xorl %eax, %eax
cmpb $0x4, %sil
setne %al
jmp 0xb8a1
cmpb $0x4, %sil
jne 0xb8aa
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, %dl
leaq 0x3a14c(%rip), %r15     # 0x45a00
movq %r15, %rax
cmoveq %rbx, %rax
movq 0x3c9e6(%rip), %rsi     # 0x482a8
cmpq %rsi, (%rax)
je 0xb8cd
movb $0x1, %al
xorl %edx, %edx
jmp 0xb934
addq $0x8, %rbx
cmpb $0x1, %dl
cmovneq %r15, %rbx
movq (%rbx), %rbx
movb 0x10(%rbx), %al
cmpb $0x1, %al
movq %r15, %rdx
cmoveq %rbx, %rdx
movq (%rdx), %rdx
movb 0x10(%rdx), %dl
andb $-0x2, %dl
cmpb $0x4, %dl
jne 0xb930
addq $0x8, %rbx
cmpb $0x1, %al
cmovneq %r15, %rbx
movq (%rbx), %rdi
cmpb $0x1, 0x10(%rdi)
leaq 0x8(%rdi), %rax
setne %r8b
cmovneq %r15, %rax
movq 0x3a0e7(%rip), %rbx     # 0x45a00
cmpq %rbx, (%rax)
sete %al
setne %dl
cmovneq %rdi, %rbx
orb %dl, %r8b
jne 0xb934
movq (%rdi), %rbx
jmp 0xb8c7
movb $0x1, %dl
xorl %eax, %eax
cmpb $0x1, %cl
movq %r15, %rdi
cmoveq %r14, %rdi
cmpq %rsi, (%rdi)
je 0xb95e
testb %al, %al
je 0xb9e2
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xbae9
addq $0x8, %r14
cmpb $0x1, %cl
cmovneq %r15, %r14
movq (%r14), %r12
movb 0x10(%r12), %cl
cmpb $0x1, %cl
movq %r15, %rsi
cmoveq %r12, %rsi
movq (%rsi), %rsi
movb 0x10(%rsi), %sil
andb $-0x2, %sil
cmpb $0x4, %sil
jne 0xb9bc
addq $0x8, %r12
cmpb $0x1, %cl
cmovneq %r15, %r12
movq (%r12), %r12
movb 0x10(%r12), %cl
leaq 0x8(%r12), %rsi
cmpb $0x1, %cl
cmovneq %r15, %rsi
movq 0x3a04d(%rip), %r14     # 0x45a00
cmpq %r14, (%rsi)
je 0xba6e
testb %al, %al
je 0xba07
cmpb $0x1, %cl
cmoveq %r12, %r15
movq (%r15), %rsi
movq %rbx, %rdi
callq 0xbae9
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
sbbl %eax, %eax
orl %ecx, %eax
jmp 0xb8a1
testb %dl, %dl
je 0xba04
cmpb $0x1, 0x10(%rbx)
cmoveq %rbx, %r15
movq (%r15), %rdi
movq %r14, %rsi
callq 0xbae9
cmpl $0x1, %eax
adcl $0x0, %eax
jmp 0xb8a1
movq %r14, %r12
cmpb $0x1, 0x10(%rbx)
jne 0xba5c
leaq 0x10(%rbx), %r14
cmpb $0x1, 0x10(%r12)
jne 0xb870
movq (%rbx), %rdi
movq (%r12), %rsi
callq 0xbae9
testl %eax, %eax
jne 0xb8a1
addq $0x8, %rbx
cmpb $0x1, (%r14)
cmovneq %r15, %rbx
movq (%rbx), %rbx
leaq 0x8(%r12), %rax
cmpb $0x1, 0x10(%r12)
cmovneq %r15, %rax
movq (%rax), %r12
leaq 0x10(%rbx), %r14
cmpb $0x1, 0x10(%rbx)
je 0xba11
xorl %eax, %eax
cmpb $0x1, 0x10(%r12)
sete %al
negl %eax
jmp 0xb8a1
cmpb $0x1, %cl
jne 0xb943
movq (%r12), %r14
jmp 0xb943