// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rdi, %r14
callq 0xb4e1
movl %eax, %r15d
movq %rbx, %rdi
callq 0xb4e1
movl %eax, %ecx
testl %r15d, %r15d
sete %dl
testl %eax, %eax
setne %sil
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %sil, %dl
jne 0xb2c3
testl %r15d, %r15d
setne %dl
testl %ecx, %ecx
sete %cl
testb %cl, %dl
je 0xb29b
movl $0x1, %eax
jmp 0xb2c3
movb 0x10(%r14), %cl
movl %ecx, %edi
andb $-0x2, %dil
movb 0x10(%rbx), %sil
movl %esi, %edx
andb $-0x2, %dl
cmpb $0x4, %dil
jne 0xb2be
xorl %eax, %eax
cmpb $0x4, %dl
setne %al
jmp 0xb2c3
cmpb $0x4, %dl
jne 0xb2d2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, %cl
leaq 0x35724(%rip), %r15     # 0x40a00
movq %r15, %rdx
cmoveq %r14, %rdx
movq 0x37fbe(%rip), %rdi     # 0x432a8
movb $0x1, %al
cmpq %rdi, (%rdx)
je 0xb2f7
xorl %ecx, %ecx
movb $0x1, %dl
jmp 0xb366
addq $0x8, %r14
cmpb $0x1, %cl
cmovneq %r15, %r14
movq (%r14), %r14
movb 0x10(%r14), %cl
cmpb $0x1, %cl
movq %r15, %rdx
cmoveq %r14, %rdx
movq (%rdx), %rdx
movb 0x10(%rdx), %dl
andb $-0x2, %dl
cmpb $0x4, %dl
jne 0xb362
addq $0x8, %r14
cmpb $0x1, %cl
cmovneq %r15, %r14
movq (%r14), %r8
cmpb $0x1, 0x10(%r8)
leaq 0x8(%r8), %rcx
setne %r9b
cmovneq %r15, %rcx
movq 0x356b9(%rip), %r14     # 0x40a00
cmpq %r14, (%rcx)
sete %dl
setne %cl
cmovneq %r8, %r14
orb %cl, %r9b
jne 0xb366
movq (%r8), %r14
movb $0x1, %dl
xorl %ecx, %ecx
jmp 0xb366
movb $0x1, %cl
xorl %edx, %edx
cmpb $0x1, %sil
movq %r15, %r8
cmoveq %rbx, %r8
cmpq %rdi, (%r8)
je 0xb37a
xorl %esi, %esi
jmp 0xb3e9
addq $0x8, %rbx
cmpb $0x1, %sil
cmovneq %r15, %rbx
movq (%rbx), %rbx
movb 0x10(%rbx), %al
cmpb $0x1, %al
movq %r15, %rsi
cmoveq %rbx, %rsi
movq (%rsi), %rsi
movb 0x10(%rsi), %sil
andb $-0x2, %sil
cmpb $0x4, %sil
jne 0xb3e4
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 0x35636(%rip), %rbx     # 0x40a00
cmpq %rbx, (%rax)
sete %al
setne %sil
cmovneq %rdi, %rbx
orb %sil, %r8b
jne 0xb3e9
movq (%rdi), %rbx
movb $0x1, %al
jmp 0xb376
movb $0x1, %sil
xorl %eax, %eax
movl %edx, %edi
andb %al, %dil
cmpb $0x1, %dil
jne 0xb40d
movq %r14, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb54a
andb %sil, %dl
cmpb $0x1, %dl
jne 0xb438
cmpb $0x1, 0x10(%rbx)
cmoveq %rbx, %r15
movq (%r15), %rsi
movq %r14, %rdi
callq 0xb54a
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
sbbl %eax, %eax
orl %ecx, %eax
jmp 0xb2c3
movb 0x10(%r14), %sil
cmpb $0x1, %sil
sete %dl
testb %al, %cl
je 0xb465
cmpb $0x1, %sil
cmoveq %r14, %r15
movq (%r15), %rdi
movq %rbx, %rsi
callq 0xb54a
cmpl $0x1, %eax
adcl $0x0, %eax
jmp 0xb2c3
cmpb $0x1, %sil
jne 0xb4d1
cmpb $0x1, 0x10(%rbx)
jne 0xb4c4
leaq 0x10(%r14), %r13
leaq 0x10(%rbx), %r12
movq (%r14), %rdi
movq (%rbx), %rsi
callq 0xb54a
testl %eax, %eax
jne 0xb2c3
addq $0x8, %r14
cmpb $0x1, (%r13)
cmovneq %r15, %r14
movq (%r14), %r14
addq $0x8, %rbx
cmpb $0x1, (%r12)
cmovneq %r15, %rbx
movq (%rbx), %rbx
cmpb $0x1, 0x10(%r14)
sete %dl
jne 0xb4d1
leaq 0x10(%r14), %r13
leaq 0x10(%rbx), %r12
cmpb $0x1, 0x10(%rbx)
je 0xb479
movl $0x1, %eax
testb %dl, %dl
jne 0xb2c3
xorl %eax, %eax
cmpb $0x1, 0x10(%rbx)
sete %al
negl %eax
jmp 0xb2c3