// File: /workspace/github_c_makefile_star2_O2/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, -0x48(%rbp)
movl 0x4(%rsi), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x50(%rbp)
movq %rdx, %rsp
movl (%rsi), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x38(%rbp)
movq %rdx, %rsp
pushq $-0x1
popq %r15
leaq -0x4f0(%rbp), %r12
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %eax, %eax
cmpq %rax, %r14
jae 0x1c08
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
leaq 0x1656(%rip), %rdx      # 0x3123
movq -0x48(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
cmpl $-0x1, %eax
je 0x1b82
movq %r13, -0x30(%rbp)
movq -0x50(%rbp), %rax
andq $0x0, (%rax,%r14,8)
movq %r12, %rdi
leaq 0x1625(%rip), %rsi      # 0x312a
callq 0x10f0
movq %rax, %r12
movq -0x40(%rbp), %rax
movq %r12, (%rax,%r14,8)
testq %r12, %r12
jne 0x1b2d
leaq 0x160b(%rip), %rdi      # 0x312c
leaq -0x4f0(%rbp), %rsi
callq 0x2a0e
movq %r12, %rdi
callq 0x10c0
movl %eax, %edi
leaq -0xf0(%rbp), %rsi
callq 0x1170
cmpl $-0x1, %eax
jne 0x1b5b
leaq 0x15eb(%rip), %rdi      # 0x313a
leaq -0x4f0(%rbp), %rsi
callq 0x2a0e
movq -0xc0(%rbp), %rax
cmpq $-0x1, %r15
je 0x1bcb
cmpq %rax, %r15
je 0x1bce
leaq 0x15d4(%rip), %rdi      # 0x3148
leaq -0x4f0(%rbp), %rsi
callq 0x2a51
jmp 0x1bce
leaq 0x356f(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1be7
movq -0x40(%rbp), %rax
andq $0x0, (%rax,%r14,8)
leaq -0x4f0(%rbp), %r12
movq %r12, %rdi
leaq 0x15b0(%rip), %rsi      # 0x3158
callq 0x10f0
movq -0x50(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
testq %rax, %rax
jne 0x1bd5
leaq 0x156b(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2a0e
jmp 0x1bd5
movq %rax, %r15
movq -0x30(%rbp), %r13
incl %r13d
incq %r14
movl 0x4(%rbx), %eax
leaq -0x4f0(%rbp), %r12
jmp 0x1ab3
movq 0x340a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1585(%rip), %rsi      # 0x317d
leaq -0x4f0(%rbp), %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1b8e
movq %r13, -0x30(%rbp)
movq %r15, -0x60(%rbp)
leaq -0x4f0(%rbp), %r12
xorl %r13d, %r13d
xorl %r14d, %r14d
movq -0x48(%rbp), %r15
movl (%rbx), %eax
cmpq %rax, %r13
jae 0x1cbd
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
leaq 0x1516(%rip), %rdx      # 0x3151
movq %r15, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
testl %eax, %eax
je 0x1c6d
leaq 0x349b(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1ca0
movq -0x38(%rbp), %rax
andq $0x0, (%rax,%r13,8)
jmp 0x1c9b
movq %r12, %rdi
leaq 0x14b3(%rip), %rsi      # 0x312a
callq 0x10f0
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testq %rax, %rax
jne 0x1c98
leaq 0x149c(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2a0e
incl %r14d
incq %r13
jmp 0x1c21
movq 0x3351(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x14cc(%rip), %rsi      # 0x317d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1c62
movl 0x4(%rbx), %esi
movl %esi, %eax
movq -0x30(%rbp), %rdx
subl %edx, %eax
jne 0x1d65
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
xorl %r14d, %r14d
movl 0x4(%rbx), %eax
cmpq %rax, %r14
jae 0x1d18
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1d01
callq 0x1060
movq -0x50(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1d13
callq 0x1060
incq %r14
jmp 0x1ce7
xorl %r14d, %r14d
movl (%rbx), %eax
cmpq %rax, %r14
jae 0x1d39
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0x1d34
callq 0x1060
incq %r14
jmp 0x1d1b
movq %r13, %rdi
callq 0x2afe
movq -0x30(%rbp), %rdi
callq 0x2afe
movq %r15, %rdi
callq 0x227e
movq -0x58(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl %eax, %r14d
jb 0x1f6d
leal (%r14,%rdx), %r15d
leaq 0x337f(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1fa3
movl %r15d, %edi
callq 0x222f
movq %rax, %r15
movl 0x4(%rbx), %ecx
xorl %edx, %edx
xorl %eax, %eax
movq -0x60(%rbp), %r14
movl %ecx, %esi
cmpq %rsi, %rdx
jae 0x1dda
movq -0x40(%rbp), %rsi
cmpq $0x0, (%rsi,%rdx,8)
je 0x1dd5
xorl %esi, %esi
cmpl %ecx, %esi
jae 0x1dd3
xorl %ecx, %ecx
cmpq %rsi, %rdx
sete %cl
movq 0x8(%r15), %rdi
movl 0x4(%r15), %r8d
imull %eax, %r8d
addl %esi, %r8d
movl %ecx, (%rdi,%r8,4)
movl 0x4(%rbx), %ecx
incq %rsi
jmp 0x1dac
incl %eax
incq %rdx
jmp 0x1d98
xorl %edx, %edx
movl (%rbx), %esi
cmpq %rsi, %rdx
jae 0x1e25
movq -0x38(%rbp), %rsi
cmpq $0x0, (%rsi,%rdx,8)
je 0x1e20
xorl %esi, %esi
cmpl %ecx, %esi
jae 0x1e1a
movq 0x8(%rbx), %rdi
imull %edx, %ecx
addl %esi, %ecx
movl (%rdi,%rcx,4), %ecx
movq 0x8(%r15), %rdi
movl 0x4(%r15), %r8d
imull %eax, %r8d
addl %esi, %r8d
movl %ecx, (%rdi,%r8,4)
incl %esi
movl 0x4(%rbx), %ecx
jmp 0x1df0
incl %eax
cmpl %eax, %ecx
je 0x1e25
incq %rdx
jmp 0x1ddc
leaq 0x32cc(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1fc6
movq %r15, %rdi
callq 0x26dc
movl 0x4(%rbx), %edi
callq 0x2ac0
movq %rax, %r13
movl 0x4(%rbx), %edi
callq 0x2ac0
movq %rax, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x127c
cmpq %r12, %rax
jbe 0x1ce4
movq %r12, -0x48(%rbp)
movq %r13, %rdi
callq 0x2aac
xorl %r12d, %r12d
xorl %r14d, %r14d
movl 0x4(%rbx), %eax
cmpq %rax, %r12
jae 0x1ec1
movq -0x40(%rbp), %rax
movq (%rax,%r12,8), %rsi
testq %rsi, %rsi
je 0x1ebc
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r13), %rdi
callq 0x1281
cmpq $0x1, %rax
je 0x1eb9
leaq 0x12a6(%rip), %rdi      # 0x315a
callq 0x2a00
incl %r14d
incq %r12
jmp 0x1e82
xorl %r12d, %r12d
movl (%rbx), %eax
cmpq %rax, %r12
jae 0x1f08
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %rsi
testq %rsi, %rsi
je 0x1f03
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r13), %rdi
callq 0x1281
cmpq $0x1, %rax
je 0x1efa
leaq 0x12d6(%rip), %rdi      # 0x31cb
callq 0x2a00
incl %r14d
cmpl %r14d, 0x4(%rbx)
je 0x1f08
incq %r12
jmp 0x1ec4
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x294a
xorl %r14d, %r14d
xorl %r12d, %r12d
movl 0x4(%rbx), %eax
cmpq %rax, %r12
jae 0x1f5d
movq -0x50(%rbp), %rax
movq (%rax,%r14,2), %rsi
testq %rsi, %rsi
je 0x1f54
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
addq %r14, %rdi
callq 0x1284
cmpq $0x1, %rax
je 0x1f54
leaq 0x121b(%rip), %rdi      # 0x316a
callq 0x2a00
incq %r12
addq $0x4, %r14
jmp 0x1f1d
movq -0x48(%rbp), %r12
incq %r12
movq -0x60(%rbp), %r14
jmp 0x1e5f
movq 0x3084(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x120e(%rip), %rdi      # 0x318c
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x1140
pushq $-0x1
popq %rax
movq %rax, -0x58(%rbp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, -0x30(%rbp)
jmp 0x1ce4
movq 0x304e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11e9(%rip), %rsi      # 0x319d
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10b0
movl 0x4(%rbx), %esi
jmp 0x1d82
movq 0x302b(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x11e3(%rip), %rdi      # 0x31ba
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x1140
movq %r15, %rdi
callq 0x229a
jmp 0x1e35
nop