// File: /workspace/github_c_makefile_star2_O3/vrancurel[P]ec/mat.c
// Function: mat_vandermonde_correct
t_mat *mat_vandermonde_correct(u_int n_rows, u_int n_cols)
{
  t_mat *mat, *tmp;
  int i, j, dim;
  
  dim = n_rows + n_cols;
  tmp = mat_xcalloc(dim, n_cols);
  for (i = 0;i < dim;i++) {
    for (j = 0;j < n_cols;j++) {
      MAT_ITEM(tmp, i, j) = gpow(i, j); 
    }
  }

  /* perform transformations to get the identity matrix on the top rows */
  i = 0;
  while (i < n_cols) {
    
    if (mat_check_row_is_identity(tmp, i)) {
      i++;
      continue ;
    }
    
    //this case is mentionned in the paper but cannot happen
    /* if (0 == MAT_ITEM(tmp, i, i)) {
       for (j = i + 1;j < tmp->n_cols;j++) {
       if (0 != MAT_ITEM(tmp, i, j)) {
       mat_swap_cols(tmp, i, j);
       continue ;
       }
       } */
    
    //check if f_i_i == 1
    if (1 != MAT_ITEM(tmp, i, i)) {
      //check for inverse since f_i_i != 0
      mat_transform1(tmp, i);
    }
    
    //now f_i_i == 1
    for (j = 0;j < tmp->n_cols;j++) {
      if (i != j) {
        if (0 != MAT_ITEM(tmp, i, j)) {
          mat_transform2(tmp, i, j);
        }
      }
    }
    
    i++;
  }

  mat = mat_xcalloc(n_rows, n_cols);

  //copy last n_rows rows of tmp into mat
  for (i = 0;i < n_rows;i++) {
    for (j = 0;j < n_cols;j++) {
      MAT_ITEM(mat, i, j) = MAT_ITEM(tmp, n_cols + i, j);
    }
  }

  free(tmp);
  return mat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movl %edi, %r14d
leal (%rbx,%r14), %ebp
movl %ebp, %edi
callq 0x2407
movq %rax, %r15
testl %ebp, %ebp
jle 0x2813
xorl %r12d, %r12d
testl %ebx, %ebx
je 0x280b
xorl %r13d, %r13d
movl %r12d, %edi
movl %r13d, %esi
callq 0x14ea
movq 0x8(%r15), %rcx
movl 0x4(%r15), %edx
imull %r12d, %edx
addl %r13d, %edx
movl %eax, (%rcx,%rdx,4)
incl %r13d
cmpl %r13d, %ebx
jne 0x27e6
incl %r12d
cmpl %ebp, %r12d
jne 0x27df
testl %ebx, %ebx
je 0x28cb
movl %ebx, %ebp
movl 0x4(%r15), %eax
xorl %r12d, %r12d
movl %eax, %ecx
movl %eax, %edx
testl %edx, %edx
je 0x28bd
movl %edx, %edx
movq 0x8(%r15), %rsi
movl %edx, %edi
imull %r12d, %edi
xorl %r8d, %r8d
leal (%rdi,%r8), %r9d
xorl %r10d, %r10d
cmpq %r8, %r12
sete %r10b
cmpl %r10d, (%rsi,%r9,4)
jne 0x285d
incq %r8
cmpq %r8, %rdx
jne 0x283f
jmp 0x28bf
incl %edx
imull %r12d, %edx
cmpl $0x1, (%rsi,%rdx,4)
je 0x287a
movq %r15, %rdi
movl %r12d, %esi
callq 0x26dd
movl 0x4(%r15), %eax
movl %eax, %ecx
testl %ecx, %ecx
je 0x28bb
xorl %r13d, %r13d
movl %ecx, %esi
cmpq %r13, %r12
je 0x28ab
movq 0x8(%r15), %rcx
imull %r12d, %esi
addl %r13d, %esi
cmpl $0x0, (%rcx,%rsi,4)
je 0x28ab
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x2749
movl 0x4(%r15), %eax
incq %r13
movl %eax, %ecx
movl %eax, %edx
movl %eax, %esi
cmpl %eax, %r13d
jb 0x2883
jmp 0x28bf
xorl %ecx, %ecx
xorl %edx, %edx
incq %r12
cmpq %rbp, %r12
jne 0x2828
movl %r14d, %edi
movl %ebx, %esi
callq 0x2407
movq %rax, %r12
testl %r14d, %r14d
je 0x291e
xorl %eax, %eax
movl %ebx, %ecx
testl %ebx, %ebx
je 0x2915
movq 0x8(%r15), %rdx
movq 0x8(%r12), %rsi
xorl %edi, %edi
movl 0x4(%r15), %r8d
imull %ecx, %r8d
addl %edi, %r8d
movl (%rdx,%r8,4), %r8d
movl 0x4(%r12), %r9d
imull %eax, %r9d
addl %edi, %r9d
movl %r8d, (%rsi,%r9,4)
incl %edi
cmpl %edi, %ebx
jne 0x28f0
incl %eax
incl %ecx
cmpl %r14d, %eax
jne 0x28e1
movq %r15, %rdi
callq 0x1030
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq