// File: /workspace/github_c_makefile_star2_O3/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 0xb477
testl %eax, %eax
je 0xbcad
movq %rbx, %rdi
jmp 0xbcbc
movq %rbx, %rdi
callq 0xb477
testl %eax, %eax
je 0xbccf
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
cmpb $0x4, 0x10(%r14)
jne 0xbd13
movq 0x8(%r14), %rax
cmpl $0x1, -0x4(%rax)
jne 0xbd13
cmpl $0x1, (%rax)
jne 0xbd13
cmpb $0x4, 0x10(%rbx)
jne 0xbd13
movq 0x8(%rbx), %rax
cmpl $0x1, -0x4(%rax)
jne 0xbd13
cmpl $0x1, (%rax)
jne 0xbd13
movq %r14, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbea7
movq (%r14), %rdi
movq 0x8(%rbx), %rsi
callq 0x7b60
movq %rax, %r12
movq 0x8(%r14), %rdi
movq (%rbx), %rsi
callq 0x7b60
movq %rax, %r15
leaq 0x12(%r14), %r13
movb 0x12(%r14), %al
cmpb 0x12(%rbx), %al
jne 0xbd4f
movq %r13, -0x30(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7c62
jmp 0xbdba
movq %r12, %rdi
movq %r15, %rsi
callq 0x8704
cmpl $-0x1, %eax
je 0xbd9b
testl %eax, %eax
jne 0xbdab
movq 0x373d6(%rip), %rdi     # 0x43140
callq 0x8ba5
addq $-0x4, %r12
movq %r12, %rdi
callq 0x7040
movl 0x373db(%rip), %ebx     # 0x4315c
addq $-0x4, %r15
movq %r15, %rdi
callq 0x7040
addl $-0x2, %ebx
movl %ebx, 0x373c6(%rip)     # 0x4315c
jmp 0xbe6c
leaq 0x12(%rbx), %rax
movq %rax, -0x30(%rbp)
movq %r15, %rdi
movq %r12, %rsi
jmp 0xbdb5
movq %r13, -0x30(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7f9c
movq %rax, %r13
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
addq $-0x4, %r12
movq %r12, %rdi
callq 0x7040
movl 0x37382(%rip), %r12d    # 0x4315c
addq $-0x4, %r15
movq %r15, %rdi
callq 0x7040
addl $-0x2, %r12d
movl %r12d, 0x3736b(%rip)    # 0x4315c
movq 0x8(%r14), %rdi
movq 0x8(%rbx), %rsi
callq 0x7b60
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x87b4
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
callq 0x80ac
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x80ac
movl -0x30(%rbp), %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0xbf3e
addq $-0x4, %r13
movq %r13, %rdi
callq 0x7040
movl 0x37313(%rip), %r15d    # 0x4315c
addq $-0x4, %rbx
movq %rbx, %rdi
callq 0x7040
addq $-0x4, %r14
movq %r14, %rdi
callq 0x7040
addl $-0x3, %r15d
movl %r15d, 0x372f0(%rip)    # 0x4315c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq