// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
callq 0x8b78
cmpb $0x7, 0x10(%rax)
jne 0xbbbf
movq %rax, %rbx
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0xbcd6
movl 0x4(%rax), %ecx
cmpl 0x8(%rax), %ecx
jne 0xbcd6
movq (%rbx), %rax
movl 0x4(%rax), %r12d
movl %r12d, %edi
imull %r12d, %edi
callq 0x7850
movq %rax, %r14
movq (%rax), %rax
movl $0x2, (%rax)
movq (%r14), %rax
movl %r12d, 0x4(%rax)
movq (%r14), %rax
movl %r12d, 0x8(%rax)
cmpq $0x2, %r12
jne 0xbbcb
movq (%rbx), %rax
movq 0x80(%rax), %rax
movq (%r14), %rcx
movq %rax, 0x68(%rcx)
movq (%rbx), %rax
movq 0x70(%rax), %rdi
callq 0x8b01
movq 0x105893(%rip), %rdi    # 0x1113f0
callq 0x8b01
movl $0x2, %edi
callq 0xa8bb
callq 0x8b78
movq (%r14), %rcx
movq %rax, 0x70(%rcx)
movq (%rbx), %rax
movq 0x78(%rax), %rdi
callq 0x8b01
movq 0x105865(%rip), %rdi    # 0x1113f0
callq 0x8b01
movl $0x2, %edi
callq 0xa8bb
callq 0x8b78
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 0xbcc3
movq 0xfaf12(%rip), %rdi     # 0x106ad8
jmp 0xbcc3
movq %r14, -0x30(%rbp)
leal -0x1(%r12), %r14d
movl %r14d, %edi
imull %r14d, %edi
callq 0x7850
movq %rax, %r15
movq (%rax), %rax
movl $0x2, (%rax)
movq (%r15), %rax
movl %r14d, 0x4(%rax)
movq (%r15), %rax
movl %r14d, 0x8(%rax)
testl %r12d, %r12d
jle 0xbcbf
leaq (,%r12,8), %r10
xorl %r14d, %r14d
movq %r10, -0x38(%rbp)
xorl %r13d, %r13d
xorl %eax, %eax
movl $0x68, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %r14, %rax
sete %dil
cmpq %rsi, %r13
sete %r8b
orb %dil, %r8b
jne 0xbc4a
movq (%rbx), %rdi
addq %rcx, %rdi
movq (%rdi,%rsi,8), %rdi
movq (%r15), %r8
movslq %edx, %r9
incl %edx
movq %rdi, 0x68(%r8,%r9,8)
incq %rsi
cmpq %rsi, %r12
jne 0xbc20
incq %rax
addq %r10, %rcx
cmpq %r12, %rax
jne 0xbc1e
movq %r15, %rdi
callq 0x8b01
callq 0xbd03
leal (%r14,%r13), %eax
testb $0x1, %al
je 0xbc88
movq 0x105777(%rip), %rdi    # 0x1113f0
callq 0x8b01
movl $0x2, %edi
callq 0xa8bb
callq 0x8b78
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq %r13, %rdx
imulq %r12, %rdx
addq %r14, %rdx
movq %rax, 0x68(%rcx,%rdx,8)
incq %r13
cmpq %r12, %r13
movq -0x38(%rbp), %r10
jne 0xbc15
incq %r14
cmpq %r12, %r14
jne 0xbc12
movq -0x30(%rbp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
leaq 0x1a545(%rip), %rdi     # 0x26222
callq 0x77c8