// File: /workspace/github_c_makefile_star2_O3/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
movl 0x33293(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ecx       # imm = 0xFFFE795F
leal (%rax,%rcx), %edx
cmpl %ecx, %edx
jbe 0xc28f
decl %eax
movl %eax, 0x3327b(%rip)     # 0x3f2b8
leaq 0x3c57c(%rip), %r14     # 0x485c0
movq (%r14,%rax,8), %r15
cmpb $0x7, 0x10(%r15)
jne 0xc15e
movq (%r15), %rax
cmpl $0x2, (%rax)
jne 0xc29b
movl 0x4(%rax), %r13d
cmpl 0x8(%rax), %r13d
jne 0xc29b
movl %r13d, %edi
imull %r13d, %edi
callq 0x7893
movq %rax, %r12
movq (%rax), %rax
movl $0x2, (%rax)
movq (%r12), %rax
movl %r13d, 0x4(%rax)
movq (%r12), %rax
movl %r13d, 0x8(%rax)
cmpl $0x2, %r13d
jne 0xc16a
movq (%r15), %rax
movq 0x80(%rax), %rax
movq (%r12), %rcx
movq %rax, 0x68(%rcx)
movq (%r15), %rax
movq 0x70(%rax), %rdi
callq 0x8ba5
movq 0x10a32c(%rip), %rdi    # 0x1163f0
callq 0x8ba5
movl $0x2, %edi
callq 0xacef
movl 0x331df(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %ebx       # imm = 0xFFFE795F
leal (%rax,%rbx), %ecx
cmpl %ebx, %ecx
jbe 0xc28f
decl %eax
movl %eax, 0x331c7(%rip)     # 0x3f2b8
movq (%r14,%rax,8), %rax
movq (%r12), %rcx
movq %rax, 0x70(%rcx)
movq (%r15), %rax
movq 0x78(%rax), %rdi
callq 0x8ba5
movq 0x10a2e0(%rip), %rdi    # 0x1163f0
callq 0x8ba5
movl $0x2, %edi
callq 0xacef
movl 0x33193(%rip), %eax     # 0x3f2b8
leal (%rax,%rbx), %ecx
cmpl %ebx, %ecx
jbe 0xc28f
decl %eax
movl %eax, 0x33180(%rip)     # 0x3f2b8
movq (%r14,%rax,8), %rax
movq (%r12), %rcx
movq %rax, 0x78(%rcx)
movq (%r15), %rax
movq 0x68(%rax), %rax
movq (%r12), %rcx
movq %rax, 0x80(%rcx)
movq %r12, %rdi
jmp 0xc27c
movq 0xff973(%rip), %rdi     # 0x10bad8
jmp 0xc27c
movq %r12, -0x30(%rbp)
leal -0x1(%r13), %ebx
movl %ebx, %edi
imull %ebx, %edi
callq 0x7893
movq %rax, %r14
movq (%rax), %rax
movl $0x2, (%rax)
movq (%r14), %rax
movl %ebx, 0x4(%rax)
movq (%r14), %rax
movl %ebx, 0x8(%rax)
testl %r13d, %r13d
jle 0xc278
leaq (,%r13,8), %r10
xorl %ebx, %ebx
movq %r10, -0x38(%rbp)
xorl %r12d, %r12d
xorl %eax, %eax
movl $0x68, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rbx, %rax
sete %dil
cmpq %rsi, %r12
sete %r8b
orb %dil, %r8b
jne 0xc1e3
movq (%r15), %rdi
addq %rcx, %rdi
movq (%rdi,%rsi,8), %rdi
movq (%r14), %r8
movslq %edx, %r9
incl %edx
movq %rdi, 0x68(%r8,%r9,8)
incq %rsi
cmpq %rsi, %r13
jne 0xc1b9
incq %rax
addq %r10, %rcx
cmpq %r13, %rax
jne 0xc1b7
movq %r14, %rdi
callq 0x8ba5
callq 0xc2c8
leal (%r12,%rbx), %eax
testb $0x1, %al
je 0xc221
movq 0x10a1de(%rip), %rdi    # 0x1163f0
callq 0x8ba5
movl $0x2, %edi
callq 0xacef
movl 0x33091(%rip), %eax     # 0x3f2b8
movl $0xfffe795f, %edx       # imm = 0xFFFE795F
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
jbe 0xc28f
decl %eax
movl %eax, 0x3307d(%rip)     # 0x3f2b8
leaq 0x3c37e(%rip), %rcx     # 0x485c0
movq (%rcx,%rax,8), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq %r12, %rdx
imulq %r13, %rdx
addq %rbx, %rdx
movq %rax, 0x68(%rcx,%rdx,8)
incq %r12
cmpq %r13, %r12
movq -0x38(%rbp), %r10
jne 0xc1ae
incq %rbx
cmpq %r13, %rbx
jne 0xc1ab
movq -0x30(%rbp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ba5
leaq 0x22cab(%rip), %rdi     # 0x2ef41
callq 0x780b
leaq 0x1dfa0(%rip), %rdi     # 0x2a242
callq 0x780b