// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: contract
void
contract(void)
{
	int h, i, j, k, m, n, ncol, ndim, nelem, nrow;
	int index[MAXDIM];
	struct atom *p1, *p2, *p3;

	p3 = pop();
	p2 = pop();
	p1 = pop();

	if (!istensor(p1)) {
		push(p1);
		return;
	}

	ndim = p1->u.tensor->ndim;

	push(p2);
	n = pop_integer();

	push(p3);
	m = pop_integer();

	if (n < 1 || n > ndim || m < 1 || m > ndim || n == m)
		stopf("contract: index error");

	n--; // make zero based
	m--;

	ncol = p1->u.tensor->dim[n];
	nrow = p1->u.tensor->dim[m];

	if (ncol != nrow)
		stopf("contract: unequal tensor dimensions");

	// nelem is the number of elements in result

	nelem = p1->u.tensor->nelem / ncol / nrow;

	p2 = alloc_tensor(nelem);

	for (i = 0; i < ndim; i++)
		index[i] = 0;

	for (i = 0; i < nelem; i++) {

		for (j = 0; j < ncol; j++) {
			index[n] = j;
			index[m] = j;
			k = index[0];
			for (h = 1; h < ndim; h++)
				k = k * p1->u.tensor->dim[h] + index[h];
			push(p1->u.tensor->elem[k]);
		}

		add_terms(ncol);

		p2->u.tensor->elem[i] = pop();

		// increment index

		for (j = ndim - 1; j >= 0; j--) {
			if (j == n || j == m)
				continue;
			if (++index[j] < p1->u.tensor->dim[j])
				break;
			index[j] = 0;
		}
	}

	if (nelem == 1) {
		push(p2->u.tensor->elem[0]);
		return;
	}

	// add dim info

	p2->u.tensor->ndim = ndim - 2;

	k = 0;

	for (i = 0; i < ndim; i++)
		if (i != n && i != m)
			p2->u.tensor->dim[k++] = p1->u.tensor->dim[i];

	push(p2);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
callq 0x868d
movq %rax, %r14
callq 0x868d
movq %rax, %r15
callq 0x868d
movq %rax, %rbx
cmpb $0x7, 0x10(%rax)
jne 0xdeb6
movq (%rbx), %rax
movl (%rax), %r12d
movq %r15, %rdi
callq 0x861b
callq 0xd9d9
movl %eax, %r15d
movq %r14, %rdi
callq 0x861b
callq 0xd9d9
testl %r15d, %r15d
jle 0xe039
cmpl %r12d, %r15d
jg 0xe039
movl %eax, %r13d
testl %eax, %eax
jle 0xe039
cmpl %r12d, %r13d
jg 0xe039
cmpl %r13d, %r15d
je 0xe039
leal -0x1(%r15), %ecx
leal -0x1(%r13), %edx
movq (%rbx), %rax
movq %rcx, -0x58(%rbp)
movl 0x4(%rax,%rcx,4), %r14d
movq %rdx, -0x50(%rbp)
cmpl 0x4(%rax,%rdx,4), %r14d
jne 0xe045
movl 0x64(%rax), %eax
cltd
idivl %r14d
cltd
idivl %r14d
movl %eax, -0x34(%rbp)
movl %eax, %edi
callq 0x77da
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq %rax, %r12
je 0xdebe
andl $0x0, -0xd0(%rbp,%rax,4)
incq %rax
jmp 0xdea4
movq %rbx, %rdi
jmp 0xe023
xorl %edx, %edx
testl %r14d, %r14d
movq %r13, %rcx
movl $0x0, %r13d
movl %r14d, -0x38(%rbp)
cmovgl %r14d, %r13d
movl %r15d, %esi
movq %r12, %rax
movl %ecx, %r12d
movl -0x34(%rbp), %ecx
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
movq %rdi, -0x68(%rbp)
movq %rax, -0x40(%rbp)
leaq -0x1(%rax), %r14
movq %rsi, -0x60(%rbp)
cmpq -0x68(%rbp), %rdx
je 0xdfca
movq %rdx, -0x48(%rbp)
movq %rbx, %r15
xorl %ebx, %ebx
cmpl %r13d, %ebx
je 0xdf5a
movq -0x58(%rbp), %rax
movl %ebx, -0xd0(%rbp,%rax,4)
movq -0x50(%rbp), %rax
movl %ebx, -0xd0(%rbp,%rax,4)
movl -0xd0(%rbp), %ecx
movq (%r15), %rax
xorl %edx, %edx
cmpq %rdx, %r14
je 0xdf49
imull 0x8(%rax,%rdx,4), %ecx
addl -0xcc(%rbp,%rdx,4), %ecx
incq %rdx
jmp 0xdf33
movslq %ecx, %rcx
movq 0x68(%rax,%rcx,8), %rdi
callq 0x861b
incl %ebx
jmp 0xdf0d
movl -0x38(%rbp), %edi
callq 0xa0fd
callq 0x868d
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x48(%rbp), %rdx
movq %rax, 0x68(%rcx,%rdx,8)
movq -0x40(%rbp), %rax
movq %r15, %rbx
movq -0x60(%rbp), %rsi
testl %eax, %eax
jle 0xdfbe
cmpq %rax, %rsi
sete %cl
cmpq %rax, %r12
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
je 0xdfb9
movl -0xd4(%rbp,%rax,4), %ecx
incl %ecx
movl %ecx, -0xd4(%rbp,%rax,4)
movq (%rbx), %rdx
cmpl (%rdx,%rax,4), %ecx
jl 0xdfbe
andl $0x0, -0xd4(%rbp,%rax,4)
decq %rax
jmp 0xdf82
movq -0x48(%rbp), %rdx
incq %rdx
jmp 0xdefa
cmpl $0x1, -0x34(%rbp)
jne 0xdfdd
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x68(%rax), %rdi
jmp 0xe023
movq -0x40(%rbp), %r8
leal -0x2(%r8), %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, %r8
je 0xe01f
cmpq %rax, -0x58(%rbp)
je 0xe01a
cmpq %rax, -0x50(%rbp)
je 0xe01a
movq (%rbx), %rdx
movl 0x4(%rdx,%rax,4), %edx
movq -0x30(%rbp), %rsi
movq (%rsi), %rsi
movslq %ecx, %rdi
incl %ecx
movl %edx, 0x4(%rsi,%rdi,4)
incq %rax
jmp 0xdff2
movq -0x30(%rbp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x861b
leaq 0x15291(%rip), %rdi     # 0x232d1
callq 0x7756
leaq 0x1529b(%rip), %rdi     # 0x232e7
callq 0x7756