// File: /workspace/github_c_makefile_star2_O2/georgeweigt[P]eigenmath/eigenmath.c
// Function: adj
void
adj(void)
{
	int col, i, j, k, n, row;
	struct atom *p1, *p2, *p3;

	p1 = pop();

	if (!istensor(p1)) {
		push_integer(1); // adj of scalar is 1 because adj = det inv
		return;
	}

	if (!issquarematrix(p1))
		stopf("adj: square matrix expected");

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

	// p2 is the adjunct matrix

	p2 = alloc_matrix(n, n);

	if (n == 2) {
		p2->u.tensor->elem[0] = p1->u.tensor->elem[3];
		push(p1->u.tensor->elem[1]);
		negate();
		p2->u.tensor->elem[1] = pop();
		push(p1->u.tensor->elem[2]);
		negate();
		p2->u.tensor->elem[2] = pop();
		p2->u.tensor->elem[3] = p1->u.tensor->elem[0];
		push(p2);
		return;
	}

	// p3 is for computing cofactors

	p3 = alloc_matrix(n - 1, n - 1);

	for (row = 0; row < n; row++) {
		for (col = 0; col < n; col++) {
			k = 0;
			for (i = 0; i < n; i++)
				for (j = 0; j < n; j++)
					if (i != row && j != col)
						p3->u.tensor->elem[k++] = p1->u.tensor->elem[n * i + j];
			push(p3);
			det();
			if ((row + col) % 2)
				negate();
			p2->u.tensor->elem[n * col + row] = pop(); // transpose
		}
	}

	push(p2);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
callq 0x868d
cmpb $0x7, 0x10(%rax)
jne 0xb175
movq %rax, %rbx
movq %rax, %rdi
callq 0xb291
testl %eax, %eax
je 0xb285
movq (%rbx), %rax
movslq 0x4(%rax), %r15
movl %r15d, %edi
movl %r15d, %esi
callq 0x782d
movq %rax, %r14
cmpq $0x2, %r15
jne 0xb18b
movq (%rbx), %rax
movq 0x80(%rax), %rax
movq (%r14), %rcx
movq %rax, 0x68(%rcx)
movq (%rbx), %rax
movq 0x70(%rax), %rdi
callq 0x861b
callq 0x975f
callq 0x868d
movq (%r14), %rcx
movq %rax, 0x70(%rcx)
movq (%rbx), %rax
movq 0x78(%rax), %rdi
callq 0x861b
callq 0x975f
callq 0x868d
movq (%r14), %rcx
movq %rax, 0x78(%rcx)
movq (%rbx), %rax
movq 0x68(%rax), %rax
movq (%r14), %rcx
movq %rax, 0x80(%rcx)
movq %r14, %rdi
jmp 0xb272
pushq $0x1
popq %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa426
movq %r14, -0x30(%rbp)
movl %r15d, %r13d
leal -0x1(%r13), %edi
movl %edi, %esi
callq 0x782d
movq %rax, %r12
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movq %rax, -0x38(%rbp)
movq %r15, -0x48(%rbp)
leaq (,%r15,8), %r10
movq %r10, -0x40(%rbp)
cmpq -0x38(%rbp), %r14
je 0xb26e
xorl %r15d, %r15d
cmpq %r13, %r15
je 0xb266
xorl %eax, %eax
pushq $0x68
popq %rcx
xorl %edx, %edx
cmpq %r13, %rax
je 0xb224
xorl %esi, %esi
cmpq %rsi, %r13
je 0xb21c
cmpq %r14, %rax
sete %dil
cmpq %rsi, %r15
sete %r8b
orb %dil, %r8b
jne 0xb217
movq (%rbx), %rdi
addq %rcx, %rdi
movq (%rdi,%rsi,8), %rdi
movq (%r12), %r8
movslq %edx, %r9
incl %edx
movq %rdi, 0x68(%r8,%r9,8)
incq %rsi
jmp 0xb1e7
incq %rax
addq %r10, %rcx
jmp 0xb1e0
movq %r12, %rdi
callq 0x861b
callq 0xb2b2
leal (%r15,%r14), %eax
testb $0x1, %al
je 0xb23e
callq 0x975f
callq 0x868d
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq %r15, %rdx
imulq -0x48(%rbp), %rdx
addq %r14, %rdx
movq %rax, 0x68(%rcx,%rdx,8)
incq %r15
movq -0x40(%rbp), %r10
jmp 0xb1d0
incq %r14
jmp 0xb1c3
movq -0x30(%rbp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x861b
leaq 0x17fee(%rip), %rdi     # 0x2327a
callq 0x7756