// File: /workspace/github_c_makefile_star2_O3/georgeweigt[P]eigenmath/eigenmath.c
// Function: promote_tensor
void
promote_tensor(void)
{
	int i, j, k, ndim1, ndim2, nelem1, nelem2;
	struct atom *p1, *p2, *p3;

	p1 = pop();

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

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

	// check

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

	for (i = 1; i < nelem1; i++) {
		p3 = p1->u.tensor->elem[i];
		if (!compatible_dimensions(p2, p3))
			stopf("tensor dimensions");
	}

	if (!istensor(p2)) {
		push(p1);
		return; // all elements are scalars
	}

	ndim2 = p2->u.tensor->ndim;
	nelem2 = p2->u.tensor->nelem;

	if (ndim1 + ndim2 > MAXDIM)
		stopf("rank exceeds max");

	// alloc

	p3 = alloc_tensor(nelem1 * nelem2);

	// merge dimensions

	k = 0;

	for (i = 0; i < ndim1; i++)
		p3->u.tensor->dim[k++] = p1->u.tensor->dim[i];

	for (i = 0; i < ndim2; i++)
		p3->u.tensor->dim[k++] = p2->u.tensor->dim[i];

	p3->u.tensor->ndim = ndim1 + ndim2;

	// merge elements

	k = 0;

	for (i = 0; i < nelem1; i++) {
		p2 = p1->u.tensor->elem[i];
		for (j = 0; j < nelem2; j++)
			p3->u.tensor->elem[k++] = p2->u.tensor->elem[j];
	}

	push(p3);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x1c27c(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0x231a4
decl %eax
movl %eax, 0x1c264(%rip)     # 0x3f2b8
leaq 0x25565(%rip), %rcx     # 0x485c0
movq (%rcx,%rax,8), %rbx
cmpb $0x7, 0x10(%rbx)
jne 0x23182
movq (%rbx), %r15
movl (%r15), %eax
movq %rax, -0x38(%rbp)
movslq 0x64(%r15), %rax
movq 0x68(%r15), %r14
movq %rax, -0x40(%rbp)
cmpq $0x2, %rax
jl 0x230ad
movq -0x40(%rbp), %rax
leaq -0x1(%rax), %r12
xorl %r13d, %r13d
movq 0x70(%r15,%r13,8), %rsi
movq %r14, %rdi
callq 0xb3fb
testl %eax, %eax
je 0x23198
incq %r13
cmpq %r13, %r12
jne 0x23090
cmpb $0x7, 0x10(%r14)
jne 0x23182
movq (%r14), %rax
movl (%rax), %r15d
movq -0x38(%rbp), %rcx
addl %r15d, %ecx
movl %ecx, -0x2c(%rbp)
cmpl $0x19, %ecx
jge 0x231b0
movl 0x64(%rax), %r12d
movl %r12d, %edi
movq -0x40(%rbp), %r13
imull %r13d, %edi
callq 0x7893
movq -0x38(%rbp), %rdi
testl %edi, %edi
jle 0x23107
xorl %ecx, %ecx
movq (%rbx), %rdx
movl 0x4(%rdx,%rcx,4), %edx
movq (%rax), %rsi
movl %edx, 0x4(%rsi,%rcx,4)
incq %rcx
cmpq %rcx, %rdi
jne 0x230ef
jmp 0x23109
xorl %edi, %edi
testl %r15d, %r15d
jle 0x23130
leaq 0x4(,%rdi,4), %rcx
xorl %edx, %edx
movq (%r14), %rsi
movl 0x4(%rsi,%rdx,4), %esi
movq (%rax), %rdi
addq %rcx, %rdi
movl %esi, (%rdi,%rdx,4)
incq %rdx
cmpq %rdx, %r15
jne 0x23118
movq (%rax), %rcx
movl -0x2c(%rbp), %edx
movl %edx, (%rcx)
testl %r13d, %r13d
jle 0x2317f
xorl %ecx, %ecx
xorl %edx, %edx
testl %r12d, %r12d
jle 0x23177
movq (%rbx), %rsi
movq 0x68(%rsi,%rcx,8), %rsi
movslq %edx, %rdi
leaq 0x68(,%rdi,8), %r8
xorl %edi, %edi
movq (%rsi), %r9
movq 0x68(%r9,%rdi,8), %r9
movq (%rax), %r10
addq %r8, %r10
movq %r9, (%r10,%rdi,8)
incq %rdi
cmpq %rdi, %r12
jne 0x2315b
addl %edi, %edx
incq %rcx
cmpq %r13, %rcx
jne 0x23141
movq %rax, %rbx
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0x7122(%rip), %rdi      # 0x2a2c1
callq 0x780b
leaq 0xbd96(%rip), %rdi      # 0x2ef41
callq 0x780b
leaq 0x7252(%rip), %rdi      # 0x2a409
callq 0x780b