// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
callq 0x91d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x7, %eax
je 0xe8bd
movl $0x1, %edi
callq 0xca50
jmp 0xeab8
movq -0x20(%rbp), %rdi
callq 0xeac0
cmpl $0x0, %eax
jne 0xe8d7
leaq 0x36920(%rip), %rdi     # 0x451f2
callq 0x78d0
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x7a40
movq %rax, -0x28(%rbp)
cmpl $0x2, -0x14(%rbp)
jne 0xe98d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x80(%rax), %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x70(%rax), %rdi
callq 0x9110
callq 0xb4b0
callq 0x91d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x70(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x78(%rax), %rdi
callq 0x9110
callq 0xb4b0
callq 0x91d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x68(%rax), %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x80(%rax)
movq -0x28(%rbp), %rdi
callq 0x9110
jmp 0xeab8
movl -0x14(%rbp), %edi
subl $0x1, %edi
movl -0x14(%rbp), %esi
subl $0x1, %esi
callq 0x7a40
movq %rax, -0x30(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xeaaf
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xea9f
movl $0x0, -0x10(%rbp)
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xea4a
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xea3d
movl -0x8(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0xea30
movl -0xc(%rbp), %eax
cmpl -0x4(%rbp), %eax
je 0xea30
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imull -0x8(%rbp), %ecx
addl -0xc(%rbp), %ecx
movslq %ecx, %rcx
movq 0x68(%rax,%rcx,8), %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x10(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x10(%rbp)
movslq %ecx, %rcx
movq %rdx, 0x68(%rax,%rcx,8)
jmp 0xea32
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xe9e5
jmp 0xea3f
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0xe9d6
movq -0x30(%rbp), %rdi
callq 0x9110
callq 0xeb10
movl -0x18(%rbp), %eax
addl -0x4(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xea70
callq 0xb4b0
callq 0x91d0
movq %rax, %rdx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imull -0x4(%rbp), %ecx
addl -0x18(%rbp), %ecx
movslq %ecx, %rcx
movq %rdx, 0x68(%rax,%rcx,8)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xe9bc
jmp 0xeaa1
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xe9a9
movq -0x28(%rbp), %rdi
callq 0x9110
addq $0x30, %rsp
popq %rbp
retq
nop