// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl -0x4(%rbp), %eax
addl -0x8(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
movl -0x8(%rbp), %esi
callq 0x2a10
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x30e4
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x30d7
movl -0x1c(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0x1530
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rsi
imull 0x4(%rsi), %ecx
addl -0x20(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x309c
jmp 0x30d9
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x308d
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x31ab
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2d90
cmpl $0x0, %eax
je 0x3113
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x30eb
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rdx
imull 0x4(%rdx), %eax
addl -0x1c(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
movl $0x1, %eax
cmpl (%rcx,%rdx,4), %eax
je 0x3143
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2f00
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x319d
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
je 0x3190
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rdx
imull 0x4(%rdx), %eax
addl -0x20(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
xorl %eax, %eax
cmpl (%rcx,%rdx,4), %eax
je 0x318e
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x2fa0
jmp 0x3190
jmp 0x3192
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x314a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x30eb
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %esi
callq 0x2a10
movq %rax, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jae 0x3229
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x321c
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x8(%rbp), %ecx
addl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rdx
imull 0x4(%rdx), %ecx
addl -0x20(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rsi
imull 0x4(%rsi), %ecx
addl -0x20(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x31d0
jmp 0x321e
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x31c1
movq -0x18(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)