// File: /workspace/github_c_makefile_star2_O0/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
subq $0xb0, %rsp
callq 0x91d0
movq %rax, -0xa8(%rbp)
callq 0x91d0
movq %rax, -0xa0(%rbp)
callq 0x91d0
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x7, %eax
je 0x13e50
movq -0x98(%rbp), %rdi
callq 0x9110
jmp 0x14139
movq -0x98(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x9110
callq 0x13520
movl %eax, -0x18(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x9110
callq 0x13520
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x18(%rbp)
jl 0x13eab
movl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0x13eab
cmpl $0x1, -0x14(%rbp)
jl 0x13eab
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0x13eab
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x13eb7
leaq 0x31397(%rip), %rdi     # 0x45249
callq 0x78d0
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movslq -0x18(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x28(%rbp), %eax
je 0x13f07
leaq 0x3135d(%rip), %rdi     # 0x4525f
callq 0x78d0
movq -0x98(%rbp), %rax
movq (%rax), %rax
movl 0x64(%rax), %eax
cltd
idivl -0x1c(%rbp)
cltd
idivl -0x28(%rbp)
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
callq 0x79b0
movq %rax, -0xa0(%rbp)
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x13f57
movslq -0x8(%rbp), %rax
movl $0x0, -0x90(%rbp,%rax,4)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x13f35
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1409f
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x14006
movl -0xc(%rbp), %ecx
movslq -0x18(%rbp), %rax
movl %ecx, -0x90(%rbp,%rax,4)
movl -0xc(%rbp), %ecx
movslq -0x14(%rbp), %rax
movl %ecx, -0x90(%rbp,%rax,4)
movl -0x90(%rbp), %eax
movl %eax, -0x10(%rbp)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x13fe0
movl -0x10(%rbp), %eax
movq -0x98(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x4(%rbp), %rdx
imull 0x4(%rcx,%rdx,4), %eax
movslq -0x4(%rbp), %rcx
addl -0x90(%rbp,%rcx,4), %eax
movl %eax, -0x10(%rbp)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x13fa9
movq -0x98(%rbp), %rax
movq (%rax), %rax
movslq -0x10(%rbp), %rcx
movq 0x68(%rax,%rcx,8), %rdi
callq 0x9110
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x13f71
movl -0x1c(%rbp), %edi
callq 0xc520
callq 0x91d0
movq %rax, %rdx
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movslq -0x8(%rbp), %rcx
movq %rdx, 0x68(%rax,%rcx,8)
movl -0x20(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jl 0x1408f
movl -0xc(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x14048
movl -0xc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x1404a
jmp 0x14084
movslq -0xc(%rbp), %rcx
movl -0x90(%rbp,%rcx,4), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp,%rcx,4)
movq -0x98(%rbp), %rcx
movq (%rcx), %rcx
movslq -0xc(%rbp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jge 0x14075
jmp 0x1408f
movslq -0xc(%rbp), %rax
movl $0x0, -0x90(%rbp,%rax,4)
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x14032
jmp 0x14091
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x13f5e
cmpl $0x1, -0x24(%rbp)
jne 0x140ba
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq 0x68(%rax), %rdi
callq 0x9110
jmp 0x14139
movl -0x20(%rbp), %ecx
subl $0x2, %ecx
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movl %ecx, (%rax)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x1412d
movl -0x8(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x14120
movl -0x8(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x14120
movq -0x98(%rbp), %rax
movq (%rax), %rax
movslq -0x8(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %edx
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x10(%rbp)
movslq %ecx, %rcx
movl %edx, 0x4(%rax,%rcx,4)
jmp 0x14122
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x140da
movq -0xa0(%rbp), %rdi
callq 0x9110
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)