// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movl (%rdi), %r14d
cmpl 0x4(%rdi), %r14d
jne 0x292b
movq %rdi, %rbx
leal (%r14,%r14), %ebp
movl %r14d, %edi
movl %ebp, %esi
callq 0x222f
movq %rax, %r15
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
movl %ecx, (%rsp)
cmpl (%rsp), %eax
je 0x274f
xorl %ecx, %ecx
cmpl %ecx, %r14d
je 0x274b
movq 0x8(%rbx), %rdx
movl 0x4(%rbx), %esi
imull %eax, %esi
addl %ecx, %esi
movl (%rdx,%rsi,4), %edx
movq 0x8(%r15), %rsi
movl 0x4(%r15), %edi
imull %eax, %edi
addl %ecx, %edi
movl %edx, (%rsi,%rdi,4)
incl %ecx
jmp 0x2723
incl %eax
jmp 0x271c
xorl %esi, %esi
movl %r14d, %ecx
cmpl (%rsp), %esi
jne 0x277a
jmp 0x2782
movl %ecx, %eax
cltd
idivl %r14d
xorl %eax, %eax
cmpl %edx, %esi
sete %al
movq 0x8(%r15), %rdx
movl 0x4(%r15), %edi
imull %esi, %edi
addl %ecx, %edi
movl %eax, (%rdx,%rdi,4)
incl %ecx
cmpl %ebp, %ecx
jl 0x275b
incl %esi
jmp 0x2751
movq %rbx, 0x8(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
cmovlel %r13d, %ebp
movq %r14, 0x10(%rsp)
cmpl (%rsp), %r13d
je 0x28d7
leal 0x1(%r13), %ecx
movl %r13d, %eax
movl %ecx, 0x4(%rsp)
cmpl %r14d, %ecx
jge 0x27d9
movq 0x8(%r15), %rsi
movl 0x4(%r15), %edi
movl %edi, %edx
imull %ecx, %edx
addl %r13d, %edx
movl (%rsi,%rdx,4), %r8d
imull %eax, %edi
addl %r13d, %edi
movl %ecx, %edx
cmpl (%rsi,%rdi,4), %r8d
jg 0x27d3
movl %eax, %edx
incl %ecx
movl %edx, %eax
jmp 0x27aa
cmpl %r13d, %eax
jne 0x289d
xorl %ebx, %ebx
cmpl (%rsp), %ebx
je 0x2891
movq 0x8(%r15), %rcx
movl 0x4(%r15), %eax
cmpl %r13d, %ebx
jne 0x2839
incl %eax
imull %r13d, %eax
movl (%rcx,%rax,4), %r12d
xorl %r14d, %r14d
cmpl %r14d, %ebp
je 0x2885
movl 0x4(%r15), %eax
imull %r13d, %eax
addl %r14d, %eax
movl (%rcx,%rax,4), %edi
movl %r12d, %esi
callq 0x13a2
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %r13d, %edx
addl %r14d, %edx
movl %eax, (%rcx,%rdx,4)
incl %r14d
jmp 0x2807
imull %ebx, %eax
addl %r13d, %eax
movl (%rcx,%rax,4), %r12d
xorl %r14d, %r14d
cmpl %r14d, %ebp
je 0x2885
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 0x13a2
movl %eax, %edi
movl %r12d, %esi
callq 0x1364
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %ebx, %edx
addl %r14d, %edx
xorl %eax, (%rcx,%rdx,4)
incl %r14d
jmp 0x2846
incl %ebx
movq 0x10(%rsp), %r14
jmp 0x27e4
movl 0x4(%rsp), %eax
movl %eax, %r13d
jmp 0x2795
xorl %ecx, %ecx
cmpl %ecx, %ebp
je 0x27e2
movq 0x8(%r15), %rdx
movl 0x4(%r15), %esi
movl %r13d, %edi
imull %esi, %edi
addl %ecx, %edi
movl (%rdx,%rdi,4), %r8d
imull %eax, %esi
addl %ecx, %esi
movl (%rdx,%rsi,4), %esi
movl %esi, (%rdx,%rdi,4)
movl 0x4(%r15), %esi
imull %eax, %esi
addl %ecx, %esi
movl %r8d, (%rdx,%rsi,4)
incl %ecx
jmp 0x289f
xorl %eax, %eax
movq 0x8(%rsp), %r8
cmpl (%rsp), %eax
je 0x2915
xorl %ecx, %ecx
cmpl %ecx, %r14d
je 0x2911
movq 0x8(%r15), %rdx
movl 0x4(%r15), %esi
imull %eax, %esi
addl %r14d, %esi
addl %ecx, %esi
movl (%rdx,%rsi,4), %edx
movq 0x8(%r8), %rsi
movl 0x4(%r8), %edi
imull %eax, %edi
addl %ecx, %edi
movl %edx, (%rsi,%rdi,4)
incl %ecx
jmp 0x28e5
incl %eax
jmp 0x28de
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x227e
leaq 0xa2a(%rip), %rdi       # 0x335c
leaq 0x9f7(%rip), %rsi       # 0x3330
leaq 0xa37(%rip), %rcx       # 0x3377
movl $0xc8, %edx
callq 0x1090