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

	p1 = pop();

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

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

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

	p2 = copy_tensor(p1);

	// interchange indices n and m

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

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

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

		k = 0;

		for (j = 0; j < ndim; j++) {
			if (j == n)
				k = k * p1->u.tensor->dim[m] + index[m];
			else if (j == m)
				k = k * p1->u.tensor->dim[n] + index[n];
			else
				k = k * p1->u.tensor->dim[j] + index[j];
		}

		p2->u.tensor->elem[k] = p1->u.tensor->elem[i];

		// increment index

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

	push(p2);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl 0x1bab1(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x239c6
movl %edi, %r14d
decl %eax
movl %eax, 0x1ba96(%rip)     # 0x3f2b8
testl %edi, %edi
jle 0x239ba
movl %esi, %ebx
leaq 0x24d8d(%rip), %rcx     # 0x485c0
movq (%rcx,%rax,8), %r15
movq (%r15), %rax
movl (%rax), %edx
movq %rdx, -0x30(%rbp)
cmpl %esi, %edx
jl 0x239ba
testl %ebx, %ebx
jle 0x239ba
cmpl %r14d, -0x30(%rbp)
jl 0x239ba
movl 0x64(%rax), %r12d
decl %r14d
decl %ebx
movq %r15, %rdi
callq 0x9638
movq (%r15), %rcx
movl 0x4(%rcx,%rbx,4), %edx
movq (%rax), %rcx
movl %edx, 0x4(%rcx,%r14,4)
movq (%r15), %rcx
movq %r14, -0x40(%rbp)
movl 0x4(%rcx,%r14,4), %edx
movq %rax, -0x38(%rbp)
movq (%rax), %rcx
movl %edx, 0x4(%rcx,%rbx,4)
movq -0x30(%rbp), %rax
leaq (,%rax,4), %r13
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x7130
movq %r12, -0x48(%rbp)
testl %r12d, %r12d
jle 0x239a0
movq -0x40(%rbp), %rdx
leaq -0xb0(,%rdx,4), %rax
addq %rbp, %rax
leaq -0xb0(,%rbx,4), %rcx
addq %rbp, %rcx
leaq (,%rdx,4), %rdx
leaq (,%rbx,4), %rsi
incq -0x30(%rbp)
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rdx
jne 0x23906
movq (%r15), %r10
leaq (%r10,%rbx,4), %r12
addq $0x4, %r12
movq %rcx, %r11
jmp 0x23932
movq (%r15), %r10
cmpq %r8, %rsi
jne 0x2391f
leaq 0x4(%r10), %r11
movq -0x40(%rbp), %r14
leaq (%r11,%r14,4), %r12
movq %rax, %r11
jmp 0x23932
leaq (%r10,%r8), %r12
addq $0x4, %r12
leaq (%r8,%rbp), %r11
addq $-0xb0, %r11
movslq (%r12), %r12
movslq %r9d, %r14
imulq %r12, %r14
movslq (%r11), %r9
addq %r14, %r9
addq $0x4, %r8
cmpq %r8, %r13
jne 0x238f1
movq 0x68(%r10,%rdi,8), %r8
movq -0x38(%rbp), %r10
movq (%r10), %r10
movq %r8, 0x68(%r10,%r9,8)
movq (%r15), %r8
movq -0x30(%rbp), %r9
movl -0xb8(%rbp,%r9,4), %r10d
incl %r10d
movl %r10d, -0xb8(%rbp,%r9,4)
cmpl -0x4(%r8,%r9,4), %r10d
jl 0x23993
movl $0x0, -0xb8(%rbp,%r9,4)
decq %r9
cmpq $0x1, %r9
jg 0x23964
incq %rdi
cmpq -0x48(%rbp), %rdi
jne 0x238eb
movq -0x38(%rbp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0xb1db(%rip), %rdi      # 0x2eb9c
callq 0x780b
leaq 0xb574(%rip), %rdi      # 0x2ef41
callq 0x780b