// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: cosfunc
void
cosfunc(void)
{
	int i, n;
	double d;
	struct atom *p1, *p2, *X, *Y;

	p1 = pop();

	if (istensor(p1)) {
		p1 = copy_tensor(p1);
		n = p1->u.tensor->nelem;
		for (i = 0; i < n; i++) {
			push(p1->u.tensor->elem[i]);
			cosfunc();
			p1->u.tensor->elem[i] = pop();
		}
		push(p1);
		return;
	}

	if (isdouble(p1)) {
		push(p1);
		d = pop_double();
		d = cos(d);
		push_double(d);
		return;
	}

	// cos(z) = 1/2 exp(i z) + 1/2 exp(-i z)

	if (isdoublez(p1)) {
		push_double(0.5);
		push(imaginaryunit);
		push(p1);
		multiply();
		expfunc();
		push(imaginaryunit);
		negate();
		push(p1);
		multiply();
		expfunc();
		add();
		multiply();
		return;
	}

	// cos(-x) = cos(x)

	if (isnegativeterm(p1)) {
		push(p1);
		negate();
		cosfunc();
		return;
	}

	if (car(p1) == symbol(ADD)) {
		cosfunc_sum(p1);
		return;
	}

	// cos(arctan(y,x)) = x (x^2 + y^2)^(-1/2)

	if (car(p1) == symbol(ARCTAN)) {
		X = caddr(p1);
		Y = cadr(p1);
		push(X);
		push(X);
		push(X);
		multiply();
		push(Y);
		push(Y);
		multiply();
		add();
		push_rational(-1, 2);
		power();
		multiply();
		return;
	}

	// cos(arcsin(x)) = sqrt(1 - x^2)

	if (car(p1) == symbol(ARCSIN)) {
		push_integer(1);
		push(cadr(p1));
		push_integer(2);
		power();
		subtract();
		push_rational(1, 2);
		power();
		return;
	}

	// n pi ?

	push(p1);
	push_symbol(PI);
	divide();
	p2 = pop();

	if (!isnum(p2)) {
		push_symbol(COS);
		push(p1);
		list(2);
		return;
	}

	if (isdouble(p2)) {
		push(p2);
		d = pop_double();
		d = cos(d * M_PI);
		push_double(d);
		return;
	}

	push(p2); // nonnegative by cos(-x) = cos(x) above
	push_integer(180);
	multiply();
	p2 = pop();

	if (!isinteger(p2)) {
		push_symbol(COS);
		push(p1);
		list(2);
		return;
	}

	push(p2);
	push_integer(360);
	modfunc();
	n = pop_integer();

	switch (n) {
	case 90:
	case 270:
		push_integer(0);
		break;
	case 60:
	case 300:
		push_rational(1, 2);
		break;
	case 120:
	case 240:
		push_rational(-1, 2);
		break;
	case 45:
	case 315:
		push_rational(1, 2);
		push_integer(2);
		push_rational(1, 2);
		power();
		multiply();
		break;
	case 135:
	case 225:
		push_rational(-1, 2);
		push_integer(2);
		push_rational(1, 2);
		power();
		multiply();
		break;
	case 30:
	case 330:
		push_rational(1, 2);
		push_integer(3);
		push_rational(1, 2);
		power();
		multiply();
		break;
	case 150:
	case 210:
		push_rational(-1, 2);
		push_integer(3);
		push_rational(1, 2);
		power();
		multiply();
		break;
	case 0:
		push_integer(1);
		break;
	case 180:
		push_integer(-1);
		break;
	default:
		push_symbol(COS);
		push(p1);
		list(2);
		break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
callq 0x868d
movq %rax, %rbx
movzbl 0x10(%rax), %r14d
cmpl $0x5, %r14d
je 0xe133
cmpl $0x7, %r14d
je 0xe0dc
movq %rbx, %rdi
callq 0xb876
testl %eax, %eax
jne 0xe154
movq %rbx, %rdi
callq 0x9fd6
testl %eax, %eax
je 0xe1b6
movq %rbx, %rdi
callq 0x861b
callq 0x975f
jmp 0xe090
movq %rbx, %rdi
callq 0x8f1b
movq %rax, %rbx
movq (%rax), %rax
movl 0x64(%rax), %r14d
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
cmpq %r15, %r14
je 0xe121
movq (%rbx), %rax
movq 0x68(%rax,%r15,8), %rdi
callq 0x861b
callq 0xe086
callq 0x868d
movq (%rbx), %rcx
movq %rax, 0x68(%rcx,%r15,8)
incq %r15
jmp 0xe0f8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x861b
movq %rbx, %rdi
callq 0x861b
callq 0x8ae1
callq 0x7170
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xaf59
movsd 0x15004(%rip), %xmm0    # 0x23160
callq 0xaf59
movq 0xf6968(%rip), %rdi     # 0x104ad0
callq 0x861b
movq %rbx, %rdi
callq 0x861b
callq 0x9fae
callq 0xe4ce
movq 0xf694a(%rip), %rdi     # 0x104ad0
callq 0x861b
callq 0x975f
movq %rbx, %rdi
callq 0x861b
callq 0x9fae
callq 0xe4ce
callq 0xa1f6
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9fae
cmpb $0x1, %r14b
leaq 0x3082f(%rip), %r15     # 0x3e9f0
movq %r15, %rax
cmoveq %rbx, %rax
movq (%rax), %rax
cmpq 0x330be(%rip), %rax     # 0x41290
je 0xe23f
cmpq 0x2dfad(%rip), %rax     # 0x3c188
je 0xe251
cmpq 0x2df94(%rip), %rax     # 0x3c178
je 0xe27e
movq %rbx, %rdi
callq 0x861b
movq 0x30e47(%rip), %rdi     # 0x3f040
callq 0x861b
callq 0xca0b
callq 0x868d
movb 0x10(%rax), %r14b
movl %r14d, %ecx
andb $-0x2, %cl
cmpb $0x4, %cl
jne 0xe410
movq %rax, %rdi
callq 0x861b
cmpb $0x5, %r14b
jne 0xe33b
callq 0x8ae1
mulsd 0x14efe(%rip), %xmm0    # 0x23138
jmp 0xe140
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xe4e9
addq $0x8, %rbx
cmpb $0x1, %r14b
cmovneq %r15, %rbx
movq (%rbx), %rax
cmpb $0x1, 0x10(%rax)
jne 0xe2d9
movq 0x8(%rax), %rcx
cmpb $0x1, 0x10(%rcx)
leaq 0x3077b(%rip), %rbx     # 0x3e9f0
cmoveq %rcx, %rbx
movq %rax, %r15
jmp 0xe2e9
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0xa426
movq %rbx, %rax
addq $0x8, %rax
cmpb $0x1, 0x10(%rbx)
cmovneq %r15, %rax
movq (%rax), %rax
cmpb $0x1, 0x10(%rax)
cmoveq %rax, %r15
movq (%r15), %rdi
callq 0x861b
pushq $0x2
popq %rbx
movl %ebx, %edi
callq 0xa426
callq 0x9a87
callq 0xb07f
movl %r14d, %edi
movl %ebx, %esi
callq 0x9a47
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9a87
movq 0x30710(%rip), %rbx     # 0x3e9f0
cmpb $0x1, 0x10(%rbx)
movq %rbx, %r14
jne 0xe2ef
movq (%rbx), %r14
movq (%r15), %rbx
movq %r14, %rdi
callq 0x861b
movq %r14, %rdi
callq 0x861b
movq %r14, %rdi
callq 0x861b
callq 0x9fae
movq %rbx, %rdi
callq 0x861b
movq %rbx, %rdi
callq 0x861b
callq 0x9fae
callq 0xa1f6
pushq $-0x1
popq %rdi
pushq $0x2
popq %rsi
callq 0x9a47
callq 0x9a87
jmp 0xe1a7
movl $0xb4, %edi
callq 0xa426
callq 0x9fae
callq 0x868d
movq %rax, %r14
movq %rax, %rdi
callq 0x8b5d
testl %eax, %eax
je 0xe410
movq %r14, %rdi
callq 0x861b
movl $0x168, %edi            # imm = 0x168
callq 0xa426
callq 0xe617
callq 0xd9d9
testl %eax, %eax
je 0xe4bc
cmpl $0x1e, %eax
je 0xe436
cmpl $0x2d, %eax
je 0xe465
cmpl $0x3c, %eax
je 0xe44c
cmpl $0x5a, %eax
je 0xe409
cmpl $0x78, %eax
je 0xe450
cmpl $0x87, %eax
je 0xe49a
cmpl $0x96, %eax
je 0xe488
cmpl $0xb4, %eax
je 0xe4b8
cmpl $0xd2, %eax
je 0xe488
cmpl $0xe1, %eax
je 0xe49a
cmpl $0xf0, %eax
je 0xe450
cmpl $0x14a, %eax            # imm = 0x14A
je 0xe436
cmpl $0x12c, %eax            # imm = 0x12C
je 0xe44c
cmpl $0x13b, %eax            # imm = 0x13B
je 0xe465
cmpl $0x10e, %eax            # imm = 0x10E
jne 0xe410
xorl %edi, %edi
jmp 0xe4bf
movq 0x2e3c9(%rip), %rdi     # 0x3c7e0
callq 0x861b
movq %rbx, %rdi
callq 0x861b
pushq $0x2
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x85eb
pushq $0x1
popq %rbx
pushq $0x2
popq %r14
movl %ebx, %edi
movl %r14d, %esi
callq 0x9a47
pushq $0x3
popq %rdi
jmp 0xe479
pushq $0x1
jmp 0xe452
pushq $-0x1
popq %rdi
pushq $0x2
popq %rsi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x9a47
pushq $0x1
popq %rbx
pushq $0x2
popq %r14
movl %ebx, %edi
movl %r14d, %esi
callq 0x9a47
movl %r14d, %edi
callq 0xa426
movl %ebx, %edi
movl %r14d, %esi
jmp 0xe32c
pushq $-0x1
popq %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
callq 0x9a47
pushq $0x3
popq %rdi
jmp 0xe4a9
pushq $-0x1
popq %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
callq 0x9a47
movl %ebx, %edi
callq 0xa426
pushq $0x1
popq %rdi
movl %ebx, %esi
jmp 0xe32c
pushq $-0x1
jmp 0xe4be
pushq $0x1
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xa426