// File: /workspace/github_c_makefile_star2_O1/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 0x2b9a
movq %rdi, %rbx
leal (%r14,%r14), %ebp
movl %r14d, %edi
movl %ebp, %esi
callq 0x23f7
movq %rax, %r15
testl %r14d, %r14d
jle 0x2983
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 0x295b
incl %edx
cmpl %r14d, %edx
jne 0x2959
testl %r14d, %r14d
jle 0x29c1
xorl %esi, %esi
cmpl %ebp, %r14d
jge 0x29ba
movq 0x8(%r15), %rdi
movl %r14d, %r8d
movl %r14d, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r14d
xorl %eax, %eax
cmpl %edx, %esi
sete %al
movl 0x4(%r15), %edx
imull %esi, %edx
addl %ecx, %edx
movl %eax, (%rdi,%rdx,4)
incl %ecx
decl %r8d
jne 0x2999
incl %esi
cmpl %r14d, %esi
jne 0x298a
movq %rbx, 0x10(%rsp)
testl %r14d, %r14d
jle 0x2b42
cmpl $0x2, %ebp
movl $0x1, %r12d
cmovgel %ebp, %r12d
xorl %r13d, %r13d
movl $0x1, %r11d
movq %r14, 0x20(%rsp)
leal 0x1(%r13), %ecx
movl %r13d, %eax
movl %ecx, 0xc(%rsp)
cmpl %r14d, %ecx
jge 0x2a39
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 0x2a2c
movl %r8d, %eax
incq %rdi
addl %edx, %esi
movl %eax, %r8d
cmpq %rdi, %r14
jne 0x2a11
movq %r11, 0x18(%rsp)
cmpl %r13d, %eax
je 0x2a78
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, %r12d
jne 0x2a49
xorl %ebx, %ebx
cmpl %r13d, %ebx
jne 0x2ac6
movq 0x8(%r15), %rax
movl 0x4(%r15), %ecx
imull %ebx, %ecx
addl %r13d, %ecx
movl (%rax,%rcx,4), %ebp
xorl %r14d, %r14d
movq 0x8(%r15), %rax
movl 0x4(%r15), %ecx
imull %ebx, %ecx
addl %r14d, %ecx
movl (%rax,%rcx,4), %edi
movl %ebp, %esi
callq 0x14dc
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %ebx, %edx
addl %r14d, %edx
movl %eax, (%rcx,%rdx,4)
incl %r14d
cmpl %r14d, %r12d
jne 0x2a93
jmp 0x2b1a
movq 0x8(%r15), %rax
movl 0x4(%r15), %ecx
imull %ebx, %ecx
addl %r13d, %ecx
movl (%rax,%rcx,4), %ebp
xorl %r14d, %r14d
movq 0x8(%r15), %rax
movl 0x4(%r15), %ecx
imull %r13d, %ecx
leal (%r14,%rcx), %edx
movl (%rax,%rdx,4), %edi
addl %r13d, %ecx
movl (%rax,%rcx,4), %esi
callq 0x14dc
movl %eax, %edi
movl %ebp, %esi
callq 0x149e
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %ebx, %edx
addl %r14d, %edx
xorl %eax, (%rcx,%rdx,4)
incl %r14d
cmpl %r14d, %r12d
jne 0x2ada
incl %ebx
movq 0x20(%rsp), %r14
cmpl %r14d, %ebx
jne 0x2a7a
movq 0x18(%rsp), %r11
incq %r11
movl 0xc(%rsp), %eax
movl %eax, %r13d
cmpl %r14d, %eax
jne 0x29ea
testl %r14d, %r14d
movq 0x10(%rsp), %r9
jle 0x2b84
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 0x2b58
incl %edx
cmpl %r14d, %edx
jne 0x2b56
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2448
leaq 0x7bb(%rip), %rdi       # 0x335c
leaq 0x788(%rip), %rsi       # 0x3330
leaq 0x7c8(%rip), %rcx       # 0x3377
movl $0xc8, %edx
callq 0x1090