// File: /workspace/github_c_makefile_star2_O3/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
movl 0x2f4a5(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x100bc
leal -0x1(%rax), %ecx
movl %ecx, 0x2f48c(%rip)     # 0x3f2b8
cmpl $0x1, %eax
jbe 0x100bc
leaq 0x38784(%rip), %rdx     # 0x485c0
movq (%rdx,%rcx,8), %r14
leal -0x2(%rax), %ecx
movl %ecx, 0x2f46f(%rip)     # 0x3f2b8
cmpl $0x2, %eax
je 0x100bc
movq (%rdx,%rcx,8), %rdi
addl $-0x3, %eax
movl %eax, 0x2f459(%rip)     # 0x3f2b8
movq (%rdx,%rax,8), %r13
cmpb $0x7, 0x10(%r13)
jne 0x10061
movq (%r13), %rax
movl (%rax), %r12d
callq 0x8ba5
callq 0xf8e6
movl %eax, %r15d
movq %r14, %rdi
callq 0x8ba5
callq 0xf8e6
testl %r15d, %r15d
jle 0x100c8
cmpl %r12d, %r15d
jg 0x100c8
testl %eax, %eax
jle 0x100c8
cmpl %r12d, %eax
jg 0x100c8
cmpl %eax, %r15d
je 0x100c8
leal -0x1(%r15), %ecx
movq %rax, -0x40(%rbp)
leal -0x1(%rax), %edx
movq (%r13), %rax
movq %rcx, -0x58(%rbp)
movl 0x4(%rax,%rcx,4), %ecx
movq %rdx, -0x50(%rbp)
cmpl 0x4(%rax,%rdx,4), %ecx
jne 0x100d4
movl 0x64(%rax), %eax
cltd
idivl %ecx
cltd
movl %ecx, -0x34(%rbp)
idivl %ecx
movl %eax, %r14d
movl %eax, %edi
callq 0x7893
movq %rax, -0x30(%rbp)
leaq (,%r12,4), %rdx
leaq -0xd0(%rbp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x7130
testl %r14d, %r14d
jle 0x10066
movl %r14d, %eax
movl -0x40(%rbp), %esi
movl %r15d, %r14d
movl %eax, -0x44(%rbp)
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq %r12, %rax
decq %r12
movq %rax, %rbx
incq %rax
movq %rax, -0x60(%rbp)
negq %r14
negq %rsi
xorl %edi, %edi
movq %rsi, -0x70(%rbp)
movq %rdi, -0x40(%rbp)
movq %r13, %r15
movl -0x34(%rbp), %edi
testl %edi, %edi
jle 0xffae
xorl %r13d, %r13d
movq -0x58(%rbp), %rax
movl %r13d, -0xd0(%rbp,%rax,4)
movq -0x50(%rbp), %rax
movl %r13d, -0xd0(%rbp,%rax,4)
movl -0xd0(%rbp), %ecx
movq (%r15), %rax
cmpl $0x2, %ebx
jb 0xff96
xorl %edx, %edx
imull 0x8(%rax,%rdx,4), %ecx
addl -0xcc(%rbp,%rdx,4), %ecx
incq %rdx
cmpq %rdx, %r12
jne 0xff82
movslq %ecx, %rcx
movq 0x68(%rax,%rcx,8), %rdi
callq 0x8ba5
incl %r13d
movl -0x34(%rbp), %edi
cmpl %edi, %r13d
jne 0xff5a
callq 0xadb3
movl 0x2f2ff(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %edx       # imm = 0xFFFE795F
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
jbe 0x100bc
decl %eax
movl %eax, 0x2f2e7(%rip)     # 0x3f2b8
leaq 0x385e8(%rip), %rcx     # 0x485c0
movq (%rcx,%rax,8), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x40(%rbp), %rdi
movq %rax, 0x68(%rcx,%rdi,8)
movq -0x60(%rbp), %rax
movq %r15, %r13
movq -0x70(%rbp), %rsi
leaq (%r14,%rax), %rcx
cmpq $0x1, %rcx
je 0x10030
leaq (%rsi,%rax), %rcx
cmpq $0x1, %rcx
je 0x10030
movl -0xd8(%rbp,%rax,4), %ecx
incl %ecx
movl %ecx, -0xd8(%rbp,%rax,4)
movq (%r13), %rdx
cmpl -0x4(%rdx,%rax,4), %ecx
jl 0x10039
movl $0x0, -0xd8(%rbp,%rax,4)
decq %rax
cmpq $0x1, %rax
jg 0xfff7
incq %rdi
cmpq -0x68(%rbp), %rdi
jne 0xff49
cmpl $0x1, -0x44(%rbp)
movq %rbx, %r12
movl $0x0, %ebx
jne 0x10066
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq 0x68(%rax), %rdi
jmp 0x100a6
movq %r13, %rdi
jmp 0x100a6
leal -0x2(%r12), %eax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx)
xorl %eax, %eax
cmpq %rax, -0x58(%rbp)
je 0x1009a
cmpq %rax, -0x50(%rbp)
je 0x1009a
movq (%r13), %rcx
movl 0x4(%rcx,%rax,4), %ecx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movslq %ebx, %rsi
incl %ebx
movl %ecx, 0x4(%rdx,%rsi,4)
incq %rax
cmpq %rax, %r12
jne 0x10076
movq -0x30(%rbp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0x1ee7e(%rip), %rdi     # 0x2ef41
callq 0x780b
leaq 0x1a1ca(%rip), %rdi     # 0x2a299
callq 0x780b
leaq 0x1a1d4(%rip), %rdi     # 0x2a2af
callq 0x780b