// File: /workspace/github_c_makefile_star2_O1/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 0x23f7
movq %rax, %r15
testl %ebp, %ebp
jle 0x2807
xorl %r12d, %r12d
testl %ebx, %ebx
je 0x27ff
xorl %r13d, %r13d
movl %r12d, %edi
movl %r13d, %esi
callq 0x151a
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 0x27da
incl %r12d
cmpl %ebp, %r12d
jne 0x27d3
testl %ebx, %ebx
je 0x28ae
movl %ebx, %ebp
xorl %r12d, %r12d
movl 0x4(%r15), %eax
testq %rax, %rax
je 0x28a2
movq 0x8(%r15), %rcx
movl %eax, %edx
imull %r12d, %edx
xorl %esi, %esi
leal (%rdx,%rsi), %edi
xorl %r8d, %r8d
cmpq %rsi, %r12
sete %r8b
cmpl %r8d, (%rcx,%rdi,4)
jne 0x284a
incq %rsi
cmpq %rsi, %rax
jne 0x282d
jmp 0x28a2
movq 0x8(%r15), %rcx
incl %eax
movl %r12d, %edx
imull %eax, %edx
cmpl $0x1, (%rcx,%rdx,4)
je 0x2867
movq %r15, %rdi
movl %r12d, %esi
callq 0x26cd
movl 0x4(%r15), %eax
testl %eax, %eax
je 0x28a2
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x2896
movq 0x8(%r15), %rcx
imull %r12d, %eax
addl %r13d, %eax
cmpl $0x0, (%rcx,%rax,4)
je 0x2896
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x2739
movl 0x4(%r15), %eax
incq %r13
cmpl %eax, %r13d
jb 0x2872
incq %r12
cmpq %rbp, %r12
jne 0x2814
movl %r14d, %edi
movl %ebx, %esi
callq 0x23f7
movq %rax, %r12
testl %r14d, %r14d
je 0x2901
xorl %eax, %eax
movl %ebx, %ecx
testl %ebx, %ebx
je 0x28f8
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 0x28d3
incl %eax
incl %ecx
cmpl %r14d, %eax
jne 0x28c4
movq %r15, %rdi
callq 0x1030
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq