// File: /workspace/github_c_makefile_star2_O3/vrancurel[P]ec/mat.c
// Function: mat_inv
void mat_inv(t_mat *mat)
{
  t_mat *aug;
  int dim, i, j, k, tpos, tval, r;

  assert(mat->n_rows == mat->n_cols);
  dim = mat->n_rows;
  aug = mat_xcalloc(dim, dim * 2);

  for (i = 0;i < dim;i++) {
    for (j = 0;j < dim;j++) {
      MAT_ITEM(aug, i, j) = MAT_ITEM(mat, i, j);
    }
  }

  for (i = 0;i < dim;i++) {
    for (j = dim;j < 2*dim;j++) {
      if (i == (j % dim))
        MAT_ITEM(aug, i, j) = 1;
      else
        MAT_ITEM(aug, i, j) = 0;
    }
  }

  /* using gauss-jordan elimination */
  for (j = 0;j < dim;j++) {
    tpos = j;
    
    /* finding maximum jth column element in last (dimension-j) rows */
    for (i = j+1;i < dim;i++) {
      if (MAT_ITEM(aug, i, j) > MAT_ITEM(aug, tpos, j))
        tpos = i;
    }
    
    /* swapping row which has maximum jth column element */
    if (tpos != j) {
      for (k=0;k < 2*dim;k++) {
        tval = MAT_ITEM(aug, j, k);
        MAT_ITEM(aug, j, k) = MAT_ITEM(aug, tpos, k);
        MAT_ITEM(aug, tpos, k) = tval;
      }
    }
    
    /* performing row operations to form required identity matrix out of the input matrix */
    for (i = 0;i < dim;i++) {
      if (i != j) {
        r = MAT_ITEM(aug, i, j);
        for (k = 0;k < 2*dim;k++) {
          MAT_ITEM(aug, i, k) ^= gmul(gdiv(MAT_ITEM(aug, j, k), MAT_ITEM(aug, j, j)), r);
        }
      } else {
        r = MAT_ITEM(aug, i, j);
        for (k = 0;k < 2*dim;k++) {
          MAT_ITEM(aug, i, k) = gdiv(MAT_ITEM(aug, i, k), r);
        }
      }
    }
  }

  for (i = 0;i < dim;i++) {
    for (j = 0;j < dim;j++) {
      MAT_ITEM(mat, i, j) = MAT_ITEM(aug, i, dim + j);
    }
  }

  mat_free(aug);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl (%rdi), %r14d
cmpl 0x4(%rdi), %r14d
jne 0x2b9f
movq %rdi, %rbx
leal (%r14,%r14), %ebp
movl %r14d, %edi
movl %ebp, %esi
callq 0x2407
movq %rax, %r15
testl %r14d, %r14d
jle 0x2b89
movq 0x8(%rbx), %rax
movq 0x8(%r15), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0x4(%rbx), %edi
imull %edx, %edi
addl %esi, %edi
movl (%rax,%rdi,4), %edi
movl 0x4(%r15), %r8d
imull %edx, %r8d
addl %esi, %r8d
movl %edi, (%rcx,%r8,4)
incl %esi
cmpl %esi, %r14d
jne 0x297c
incl %edx
cmpl %r14d, %edx
jne 0x297a
testl %r14d, %r14d
jle 0x2b89
movq 0x8(%r15), %rsi
xorl %edi, %edi
movl %r14d, %r8d
movl %r14d, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r14d
xorl %eax, %eax
cmpl %edx, %edi
sete %al
movl 0x4(%r15), %edx
imull %edi, %edx
addl %ecx, %edx
movl %eax, (%rsi,%rdx,4)
incl %ecx
decl %r8d
jne 0x29b9
incl %edi
cmpl %r14d, %edi
jne 0x29b3
testl %r14d, %r14d
jle 0x2b89
movq %rbx, 0x10(%rsp)
xorl %r13d, %r13d
movl $0x1, %r11d
movq %r14, 0x20(%rsp)
leal 0x1(%r13), %eax
movl %eax, 0xc(%rsp)
cmpl %r14d, %eax
jge 0x2a82
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
movl %edx, %esi
imull %r11d, %esi
addl %r13d, %esi
movq %r11, %rdi
movl %r13d, %r8d
movl %esi, %eax
movl (%rcx,%rax,4), %r9d
movl %edx, %r10d
imull %r8d, %r10d
addl %r13d, %r10d
movl %edi, %eax
cmpl (%rcx,%r10,4), %r9d
jg 0x2a3c
movl %r8d, %eax
incq %rdi
addl %edx, %esi
movl %eax, %r8d
cmpq %rdi, %r14
jne 0x2a21
cmpl %r13d, %eax
je 0x2a82
movq 0x8(%r15), %rcx
xorl %edx, %edx
movl 0x4(%r15), %esi
movl %r13d, %edi
imull %esi, %edi
addl %edx, %edi
movl (%rcx,%rdi,4), %r8d
imull %eax, %esi
addl %edx, %esi
movl (%rcx,%rsi,4), %esi
movl %esi, (%rcx,%rdi,4)
movl 0x4(%r15), %esi
imull %eax, %esi
addl %edx, %esi
movl %r8d, (%rcx,%rsi,4)
incl %edx
cmpl %edx, %ebp
jne 0x2a54
movq %r11, 0x18(%rsp)
xorl %ebx, %ebx
movq 0x8(%r15), %rcx
movl 0x4(%r15), %eax
cmpl %r13d, %ebx
jne 0x2ad5
incl %eax
imull %r13d, %eax
movl (%rcx,%rax,4), %r12d
xorl %r14d, %r14d
movl 0x4(%r15), %eax
imull %r13d, %eax
addl %r14d, %eax
movl (%rcx,%rax,4), %edi
movl %r12d, %esi
callq 0x14ac
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %r13d, %edx
addl %r14d, %edx
movl %eax, (%rcx,%rdx,4)
incl %r14d
cmpl %r14d, %ebp
jne 0x2aa3
jmp 0x2b1f
imull %ebx, %eax
addl %r13d, %eax
movl (%rcx,%rax,4), %r12d
xorl %r14d, %r14d
movl 0x4(%r15), %eax
imull %r13d, %eax
leal (%r14,%rax), %edx
movl (%rcx,%rdx,4), %edi
addl %r13d, %eax
movl (%rcx,%rax,4), %esi
callq 0x14ac
movl %eax, %edi
movl %r12d, %esi
callq 0x146e
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %ebx, %edx
addl %r14d, %edx
xorl %eax, (%rcx,%rdx,4)
incl %r14d
cmpl %r14d, %ebp
jne 0x2ae2
incl %ebx
movq 0x20(%rsp), %r14
cmpl %r14d, %ebx
jne 0x2a89
movq 0x18(%rsp), %r11
incq %r11
movl 0xc(%rsp), %eax
movl %eax, %r13d
cmpl %r14d, %eax
jne 0x29fd
testl %r14d, %r14d
movq 0x10(%rsp), %r9
jle 0x2b89
movq 0x8(%r15), %rax
movq 0x8(%r9), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0x4(%r15), %edi
imull %edx, %edi
addl %r14d, %edi
addl %esi, %edi
movl (%rax,%rdi,4), %edi
movl 0x4(%r9), %r8d
imull %edx, %r8d
addl %esi, %r8d
movl %edi, (%rcx,%r8,4)
incl %esi
cmpl %esi, %r14d
jne 0x2b5d
incl %edx
cmpl %r14d, %edx
jne 0x2b5b
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2458
leaq 0x7b6(%rip), %rdi       # 0x335c
leaq 0x783(%rip), %rsi       # 0x3330
leaq 0x7c3(%rip), %rcx       # 0x3377
movl $0xc8, %edx
callq 0x1090