// File: /workspace/github_c_makefile_star2_O1/georgeweigt[P]eigenmath/eigenmath.c
// Function: add_rationals
void
add_rationals(struct atom *p1, struct atom *p2)
{
	int sign;
	uint32_t *a, *ab, *b, *ba, *c;

	if (iszero(p1)) {
		push(p2);
		return;
	}

	if (iszero(p2)) {
		push(p1);
		return;
	}

	if (isinteger(p1) && isinteger(p2)) {
		add_integers(p1, p2);
		return;
	}

	ab = mmul(p1->u.q.a, p2->u.q.b);
	ba = mmul(p1->u.q.b, p2->u.q.a);

	if (p1->sign == p2->sign) {
		a = madd(ab, ba);
		sign = p1->sign;
	} else {
		switch (mcmp(ab, ba)) {
		case 1:
			a = msub(ab, ba);
			sign = p1->sign;
			break;
		case 0:
			push_integer(0);
			mfree(ab);
			mfree(ba);
			return;
		case -1:
			a = msub(ba, ab);
			sign = p2->sign;
			break;
		default:
			// never gets here, fix compiler warning
			return;
		}
	}

	mfree(ab);
	mfree(ba);

	b = mmul(p1->u.q.b, p2->u.q.b);
	c = mgcd(a, b);

	push_bignum(sign, mdiv(a, c), mdiv(b, c));

	mfree(a);
	mfree(b);
	mfree(c);
}

// 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 0xaece
testl %eax, %eax
je 0xb73e
movq %rbx, %rdi
jmp 0xb74d
movq %rbx, %rdi
callq 0xaece
testl %eax, %eax
je 0xb760
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
cmpb $0x4, 0x10(%r14)
jne 0xb7a4
movq 0x8(%r14), %rax
cmpl $0x1, -0x4(%rax)
jne 0xb7a4
cmpl $0x1, (%rax)
jne 0xb7a4
cmpb $0x4, 0x10(%rbx)
jne 0xb7a4
movq 0x8(%rbx), %rax
cmpl $0x1, -0x4(%rax)
jne 0xb7a4
cmpl $0x1, (%rax)
jne 0xb7a4
movq %r14, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xb960
movq (%r14), %rdi
movq 0x8(%rbx), %rsi
callq 0x7b16
movq %rax, %r12
movq 0x8(%r14), %rdi
movq (%rbx), %rsi
callq 0x7b16
movq %rax, %r15
leaq 0x12(%r14), %r13
movb 0x12(%r14), %al
cmpb 0x12(%rbx), %al
jne 0xb7e0
movq %r13, -0x30(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7c0e
jmp 0xb854
movq %r12, %rdi
movq %r15, %rsi
callq 0x866a
cmpl $-0x1, %eax
je 0xb835
testl %eax, %eax
jne 0xb845
movq 0x32945(%rip), %rdi     # 0x3e140
callq 0x8b01
addq $-0x4, %r12
movq %r12, %rdi
callq 0x7040
movl 0x3294a(%rip), %ebx     # 0x3e15c
leal -0x1(%rbx), %eax
movl %eax, 0x32941(%rip)     # 0x3e15c
addq $-0x4, %r15
movq %r15, %rdi
callq 0x7040
addl $-0x2, %ebx
movl %ebx, 0x3292c(%rip)     # 0x3e15c
jmp 0xb925
leaq 0x12(%rbx), %rax
movq %rax, -0x30(%rbp)
movq %r15, %rdi
movq %r12, %rsi
jmp 0xb84f
movq %r13, -0x30(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7f39
movq %rax, %r13
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
addq $-0x4, %r12
movq %r12, %rdi
callq 0x7040
movl 0x328e8(%rip), %r12d    # 0x3e15c
leal -0x1(%r12), %eax
movl %eax, 0x328dd(%rip)     # 0x3e15c
addq $-0x4, %r15
movq %r15, %rdi
callq 0x7040
addl $-0x2, %r12d
movl %r12d, 0x328c6(%rip)    # 0x3e15c
movq 0x8(%r14), %rdi
movq 0x8(%rbx), %rsi
callq 0x7b16
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
callq 0x8713
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x8049
movq %rax, %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8049
movl -0x30(%rbp), %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0xb9f7
addq $-0x4, %r13
movq %r13, %rdi
callq 0x7040
movl 0x3286e(%rip), %r15d    # 0x3e15c
leal -0x1(%r15), %eax
movl %eax, 0x32864(%rip)     # 0x3e15c
addq $-0x4, %r14
movq %r14, %rdi
callq 0x7040
leal -0x2(%r15), %eax
movl %eax, 0x3284e(%rip)     # 0x3e15c
addq $-0x4, %rbx
movq %rbx, %rdi
callq 0x7040
addl $-0x3, %r15d
movl %r15d, 0x32837(%rip)    # 0x3e15c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq