// File: /workspace/github_c_makefile_star2_O1/vrancurel[P]ec/ec.c
// Function: repair_data_files
int repair_data_files(char *prefix, t_mat *mat)
{
  int i, j, k;
  FILE *d_files[mat->n_cols];
  FILE *r_files[mat->n_cols];
  FILE *c_files[mat->n_rows];
  char filename[1024];
  struct stat stbuf;
  size_t size = -1, wc;
  t_mat *a_prime = NULL;
  u_int n_data_ok = 0;
  u_int n_coding_ok = 0;
  u_int n_total_ok;
  t_vec *words = NULL;
  t_vec *output = NULL;
  int ret;
  
  for (i = 0;i < mat->n_cols;i++) {
    snprintf(filename, sizeof (filename), "%s.d%d", prefix, i);
    if (-1 == access(filename, F_OK)) {
      if (vflag)
        fprintf(stderr, "%s is missing\n", filename);
      d_files[i] = NULL;
      if (NULL == (r_files[i] = fopen(filename, "w")))
        xerrormsg("error opening", filename);
    } else {
      r_files[i] = NULL;
      if (NULL == (d_files[i] = fopen(filename, "r")))
        xerrormsg("error opening", filename);
      if (-1 == fstat(fileno(d_files[i]), &stbuf))
        xerrormsg("error stating", filename);
      if (-1 == size)
        size = stbuf.st_size;
      else if (size != stbuf.st_size)
        xmsg("bad size", filename);
      n_data_ok++;
    }
  }
  
  for (i = 0;i < mat->n_rows;i++) {
    snprintf(filename, sizeof (filename), "%s.c%d", prefix, i);
    if (access(filename, F_OK)) {
      if (vflag)
        fprintf(stderr, "%s is missing\n", filename);
      c_files[i] = NULL;
    } else {
      if (NULL == (c_files[i] = fopen(filename, "r")))
        xerrormsg("error opening", filename);
      n_coding_ok++;
    }
  }

  if (n_data_ok == mat->n_cols) {
    ret = 0;
    goto end;
  }

  if (n_coding_ok < (mat->n_cols-n_data_ok)) {
    fprintf(stderr, "too many losses\n");
    ret = -1;
    goto end;
  }

  n_total_ok = n_data_ok + n_coding_ok;
  if (vflag)
    fprintf(stderr, "n_data_ok=%d n_coding_ok=%d\n", n_data_ok, n_coding_ok);

  //generate a_prime
  a_prime = mat_xcalloc(n_total_ok, mat->n_cols);
  //for each data available generate the corresponding identity
  k = 0;
  for (i = 0;i < mat->n_cols;i++) {
    if (NULL != d_files[i]) {
      for (j = 0;j < mat->n_cols;j++) {
        if (i == j)
          MAT_ITEM(a_prime, k, j) = 1;
        else
          MAT_ITEM(a_prime, k, j) = 0;
      }
      k++;
    }
  }
  //finish the matrix with every coding available
  for (i = 0;i < mat->n_rows;i++) {
    if (NULL != c_files[i]) {
      //copy corresponding row in vandermonde matrix
      for (j = 0;j < mat->n_cols;j++) {
        MAT_ITEM(a_prime, k, j) = MAT_ITEM(mat, i, j);
      }
      k++;
      //stop when we have enough codings
      if (mat->n_cols == k)
        break ;
    }
  }

  if (vflag) {
    fprintf(stderr, "rebuild matrix:\n");
    mat_dump(a_prime);
  }

  mat_inv(a_prime);

  //read-and-repair
  words = vec_xcalloc(mat->n_cols);
  output = vec_xcalloc(mat->n_cols);
  
  for (i = 0;i < sizew(size);i++) {
    vec_zero(words);
    k = 0;
    for (j = 0;j < mat->n_cols;j++) {
      if (NULL != d_files[j]) {
        wc = freadw(&VEC_ITEM(words, k), d_files[j]);
        if (1 != wc)
          xperror("short read data");
        k++;
      }
    }
    for (j = 0;j < mat->n_rows;j++) {
      if (NULL != c_files[j]) {
        wc = freadw(&VEC_ITEM(words, k), c_files[j]);
        if (1 != wc)
          xperror("short read coding");
        k++;
        //stop when we have enough codings
        if (mat->n_cols == k)
          break ;
      }
    }

    mat_mult(output, a_prime, words);

    for (j = 0;j < mat->n_cols;j++) {
      if (NULL != r_files[j]) {
        wc = fwritew(& VEC_ITEM(output, j), r_files[j]);
        if (1 != wc)
          xperror("short write coding");
      }
    }
  } 
   
  ret = 0;
 end:
  for (i = 0;i < mat->n_cols;i++) {
    if (NULL != d_files[i])
      fclose(d_files[i]);
    if (NULL != r_files[i])
      fclose(r_files[i]);
  }
  
  for (i = 0;i < mat->n_rows;i++) {
    if (NULL != c_files[i])
      fclose(c_files[i]);
  }

  vec_free(words);
  vec_free(output);
  mat_free(a_prime);

  return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movq %rsi, %rbx
movq %rdi, -0x30(%rbp)
movl 0x4(%rsi), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movl (%rsi), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq $0x0, -0x50(%rbp)
cmpl $0x0, 0x4(%rsi)
je 0x1dc7
movq $-0x1, %r14
leaq -0x4f0(%rbp), %r15
xorl %r12d, %r12d
movq $0x0, -0x38(%rbp)
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
leaq 0x1485(%rip), %rdx      # 0x3123
movq -0x30(%rbp), %rcx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
xorl %esi, %esi
callq 0x10e0
cmpl $-0x1, %eax
je 0x1d43
movq -0x58(%rbp), %rax
movq $0x0, (%rax,%r12,8)
movq %r15, %rdi
leaq 0x1455(%rip), %rsi      # 0x312a
callq 0x10f0
movq %rax, %r13
movq -0x48(%rbp), %rax
movq %r13, (%rax,%r12,8)
testq %r13, %r13
jne 0x1cf9
leaq 0x143b(%rip), %rdi      # 0x312c
movq %r15, %rsi
callq 0x2c90
movq %r13, %rdi
callq 0x10c0
movl %eax, %edi
leaq -0xf0(%rbp), %rsi
callq 0x1170
cmpl $-0x1, %eax
jne 0x1d23
leaq 0x141f(%rip), %rdi      # 0x313a
movq %r15, %rsi
callq 0x2c90
cmpq $-0x1, %r14
je 0x1d88
cmpq -0xc0(%rbp), %r14
je 0x1d8f
leaq 0x140f(%rip), %rdi      # 0x3148
movq %r15, %rsi
callq 0x2cd5
jmp 0x1d8f
leaq 0x33ae(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1daa
movq -0x48(%rbp), %rax
movq $0x0, (%rax,%r12,8)
movq %r15, %rdi
leaq 0x13f3(%rip), %rsi      # 0x3158
callq 0x10f0
movq -0x58(%rbp), %rcx
movq %rax, (%rcx,%r12,8)
testq %rax, %rax
jne 0x1d99
leaq 0x13ae(%rip), %rdi      # 0x312c
movq %r15, %rsi
callq 0x2c90
jmp 0x1d99
movq -0xc0(%rbp), %r14
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x38(%rbp)
incq %r12
movl 0x4(%rbx), %eax
cmpq %rax, %r12
jb 0x1c8f
jmp 0x1dd6
movq 0x3247(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x13c2(%rip), %rsi      # 0x317d
movq %r15, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1d4f
movq $-0x1, %r14
movq $0x0, -0x38(%rbp)
movq %r14, -0x60(%rbp)
cmpl $0x0, (%rbx)
je 0x1e9c
leaq 0x1367(%rip), %r15      # 0x3151
leaq -0x4f0(%rbp), %r12
leaq 0x3300(%rip), %r14      # 0x50f8
xorl %r13d, %r13d
movq $0x0, -0x50(%rbp)
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq %r15, %rdx
movq -0x30(%rbp), %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
testl %eax, %eax
je 0x1e3e
cmpl $0x0, (%r14)
jne 0x1e7f
movq -0x40(%rbp), %rax
movq $0x0, (%rax,%r13,8)
jmp 0x1e73
movq %r12, %rdi
leaq 0x12e2(%rip), %rsi      # 0x312a
callq 0x10f0
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testq %rax, %rax
jne 0x1e69
leaq 0x12cb(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2c90
movq -0x50(%rbp), %rax
incl %eax
movq %rax, -0x50(%rbp)
incq %r13
movl (%rbx), %eax
cmpq %rax, %r13
jb 0x1e03
jmp 0x1e9c
movq 0x3172(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x12ed(%rip), %rsi      # 0x317d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1e30
movl 0x4(%rbx), %eax
movq -0x38(%rbp), %rdx
subl %edx, %eax
jne 0x1eba
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, -0x30(%rbp)
jmp 0x20ec
movq -0x50(%rbp), %rcx
cmpl %eax, %ecx
jb 0x2176
leal (%rcx,%rdx), %r15d
leaq 0x3227(%rip), %r14      # 0x50f8
cmpl $0x0, (%r14)
jne 0x2194
movl 0x4(%rbx), %esi
movl %r15d, %edi
callq 0x23f7
movq %rax, %r15
cmpl $0x0, 0x4(%rbx)
movq -0x60(%rbp), %r13
je 0x1f3d
xorl %ecx, %ecx
xorl %eax, %eax
movq -0x48(%rbp), %rdx
cmpq $0x0, (%rdx,%rcx,8)
je 0x1f30
cmpl $0x0, 0x4(%rbx)
je 0x1f2e
movq 0x8(%r15), %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rcx
sete %dil
movl 0x4(%r15), %r8d
imull %eax, %r8d
addl %esi, %r8d
movl %edi, (%rdx,%r8,4)
incq %rsi
cmpl 0x4(%rbx), %esi
jb 0x1f0e
incl %eax
incq %rcx
movl 0x4(%rbx), %edx
cmpq %rdx, %rcx
jb 0x1ef7
jmp 0x1f3f
xorl %eax, %eax
cmpl $0x0, (%rbx)
je 0x1f96
xorl %ecx, %ecx
movq -0x40(%rbp), %rdx
cmpq $0x0, (%rdx,%rcx,8)
je 0x1f8c
movl 0x4(%rbx), %edx
testl %edx, %edx
je 0x1f86
movq 0x8(%rbx), %rsi
movq 0x8(%r15), %rdi
xorl %r8d, %r8d
imull %ecx, %edx
addl %r8d, %edx
movl (%rsi,%rdx,4), %edx
movl 0x4(%r15), %r9d
imull %eax, %r9d
addl %r8d, %r9d
movl %edx, (%rdi,%r9,4)
incl %r8d
movl 0x4(%rbx), %edx
cmpl %edx, %r8d
jb 0x1f63
incl %eax
cmpl %eax, %edx
je 0x1f96
incq %rcx
movl (%rbx), %edx
cmpq %rdx, %rcx
jb 0x1f46
cmpl $0x0, (%r14)
jne 0x21b1
movq %r15, %rdi
callq 0x291b
movl 0x4(%rbx), %edi
callq 0x2d38
movq %rax, %r12
movl 0x4(%rbx), %edi
callq 0x2d38
movq %rax, -0x30(%rbp)
movq %r13, %rdi
callq 0x13bc
testq %rax, %rax
je 0x20ec
xorl %r14d, %r14d
movq %r14, -0x38(%rbp)
movq %r12, %rdi
callq 0x2d24
cmpl $0x0, 0x4(%rbx)
movl $0x0, %r14d
je 0x202f
xorl %r13d, %r13d
xorl %r14d, %r14d
movq -0x48(%rbp), %rax
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
je 0x2024
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r12), %rdi
callq 0x13c1
cmpq $0x1, %rax
je 0x2021
leaq 0x113e(%rip), %rdi      # 0x315a
callq 0x2c80
incl %r14d
incq %r13
movl 0x4(%rbx), %eax
cmpq %rax, %r13
jb 0x1ff1
cmpl $0x0, (%rbx)
je 0x207a
xorl %r13d, %r13d
movq -0x40(%rbp), %rax
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
je 0x2070
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r12), %rdi
callq 0x13c1
cmpq $0x1, %rax
je 0x2067
leaq 0x1169(%rip), %rdi      # 0x31cb
callq 0x2c80
incl %r14d
cmpl %r14d, 0x4(%rbx)
je 0x207a
incq %r13
movl (%rbx), %eax
cmpq %rax, %r13
jb 0x2037
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2bb9
cmpl $0x0, 0x4(%rbx)
je 0x20d3
xorl %r14d, %r14d
xorl %r13d, %r13d
movq -0x58(%rbp), %rax
movq (%rax,%r14,2), %rsi
testq %rsi, %rsi
je 0x20c4
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
addq %r14, %rdi
callq 0x13c4
cmpq $0x1, %rax
je 0x20c4
leaq 0x10ab(%rip), %rdi      # 0x316a
callq 0x2c80
incq %r13
movl 0x4(%rbx), %eax
addq $0x4, %r14
cmpq %rax, %r13
jb 0x2095
movq -0x38(%rbp), %r14
incq %r14
movq -0x60(%rbp), %rdi
callq 0x13bc
cmpq %r14, %rax
ja 0x1fd3
xorl %r13d, %r13d
cmpl $0x0, 0x4(%rbx)
je 0x2127
xorl %r14d, %r14d
movq -0x48(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x210a
callq 0x1060
movq -0x58(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x211c
callq 0x1060
incq %r14
movl 0x4(%rbx), %eax
cmpq %rax, %r14
jb 0x20f8
cmpl $0x0, (%rbx)
je 0x214b
xorl %r14d, %r14d
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x2141
callq 0x1060
incq %r14
movl (%rbx), %eax
cmpq %rax, %r14
jb 0x212f
movq %r12, %rdi
callq 0x2d78
movq -0x30(%rbp), %rdi
callq 0x2d78
movq %r15, %rdi
callq 0x2448
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11e3
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, -0x30(%rbp)
jmp 0x20ef
movq 0x2e5d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xff8(%rip), %rsi       # 0x319d
xorl %eax, %eax
callq 0x10b0
jmp 0x1edb
movq %r15, %rdi
callq 0x11b9
jmp 0x1fa0
nop