// File: /workspace/github_c_makefile_star2_O1/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 0x8b78
movq %rax, %r14
callq 0x8b78
movq %rax, %r15
callq 0x8b78
movq %rax, %rbx
cmpb $0x7, 0x10(%rax)
jne 0xf1f7
movq (%rbx), %rax
movl (%rax), %r12d
movq %r15, %rdi
callq 0x8b01
callq 0xeb5b
movl %eax, %r13d
movq %r14, %rdi
callq 0x8b01
callq 0xeb5b
testl %r13d, %r13d
jle 0xf25e
cmpl %r12d, %r13d
jg 0xf25e
testl %eax, %eax
jle 0xf25e
cmpl %r12d, %eax
jg 0xf25e
cmpl %eax, %r13d
je 0xf25e
movslq %r13d, %rcx
movl %eax, -0x40(%rbp)
movslq %eax, %rdx
movq (%rbx), %rax
movq %rcx, -0x50(%rbp)
movl (%rax,%rcx,4), %r15d
movq %rdx, -0x48(%rbp)
movl (%rax,%rdx,4), %ecx
cmpl %ecx, %r15d
jne 0xf26a
movq (%rbx), %rax
movl 0x64(%rax), %eax
cltd
idivl %r15d
cltd
idivl %ecx
movl %eax, %r14d
movl %eax, %edi
callq 0x7850
movq %rax, -0x30(%rbp)
testl %r12d, %r12d
jle 0xf0c1
leaq (,%r12,4), %rdx
leaq -0xd0(%rbp), %rdi
xorl %esi, %esi
callq 0x7130
decq -0x50(%rbp)
decq -0x48(%rbp)
movl %r14d, -0x34(%rbp)
testl %r14d, %r14d
jle 0xf1e4
movl -0x40(%rbp), %esi
movl %r13d, %edi
movl -0x34(%rbp), %eax
movq %rax, -0x60(%rbp)
leaq -0x1(%r12), %r13
leaq 0x1(%r12), %rax
movq %rax, -0x58(%rbp)
negq %rdi
negq %rsi
xorl %r8d, %r8d
movq %rsi, -0x70(%rbp)
movq %rdi, -0x68(%rbp)
movq %r8, -0x40(%rbp)
movq %r12, %r14
testl %r15d, %r15d
jle 0xf165
xorl %r12d, %r12d
movq -0x50(%rbp), %rax
movl %r12d, -0xd0(%rbp,%rax,4)
movq -0x48(%rbp), %rax
movl %r12d, -0xd0(%rbp,%rax,4)
movl -0xd0(%rbp), %eax
cmpl $0x2, %r14d
jl 0xf14e
movq (%rbx), %rcx
xorl %edx, %edx
imull 0x8(%rcx,%rdx,4), %eax
addl -0xcc(%rbp,%rdx,4), %eax
incq %rdx
cmpq %rdx, %r13
jne 0xf13a
movq (%rbx), %rcx
cltq
movq 0x68(%rcx,%rax,8), %rdi
callq 0x8b01
incl %r12d
cmpl %r15d, %r12d
jne 0xf111
movl %r15d, %edi
callq 0xa975
callq 0x8b78
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x40(%rbp), %r8
movq %rax, 0x68(%rcx,%r8,8)
movq %r14, %r12
testl %r12d, %r12d
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdi
jle 0xf1d7
movq -0x58(%rbp), %rax
leaq (%rdi,%rax), %rcx
cmpq $0x1, %rcx
je 0xf1ce
leaq (%rsi,%rax), %rcx
cmpq $0x1, %rcx
je 0xf1ce
movl -0xd8(%rbp,%rax,4), %ecx
incl %ecx
movl %ecx, -0xd8(%rbp,%rax,4)
movq (%rbx), %rdx
cmpl -0x4(%rdx,%rax,4), %ecx
jl 0xf1d7
movl $0x0, -0xd8(%rbp,%rax,4)
decq %rax
cmpq $0x1, %rax
jg 0xf196
incq %r8
cmpq -0x60(%rbp), %r8
jne 0xf102
cmpl $0x1, -0x34(%rbp)
jne 0xf1fc
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x68(%rax), %rdi
jmp 0xf248
movq %rbx, %rdi
jmp 0xf248
movl %r12d, %eax
addl $-0x2, %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx)
testl %r12d, %r12d
jle 0xf244
movl -0x48(%rbp), %eax
movl -0x50(%rbp), %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rcx
je 0xf23c
cmpq %rdx, %rax
je 0xf23c
movq (%rbx), %rdi
movl 0x4(%rdi,%rdx,4), %edi
movq -0x30(%rbp), %r8
movq (%r8), %r8
movslq %esi, %r9
incl %esi
movl %edi, 0x4(%r8,%r9,4)
incq %rdx
cmpq %rdx, %r12
jne 0xf21a
movq -0x30(%rbp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
leaq 0x17014(%rip), %rdi     # 0x26279
callq 0x77c8
leaq 0x1701e(%rip), %rdi     # 0x2628f
callq 0x77c8