// File: /workspace/github_c_makefile_star2_O3/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, %r15
movl 0x4(%rsi), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x50(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, -0x58(%rbp)
movq %rdx, %rsp
movl (%rsi), %eax
movq %rsp, %rsi
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
subq %rdx, %rsi
movq %rsi, -0x48(%rbp)
movq %rsi, %rsp
movq $0x0, -0x38(%rbp)
testq %rcx, %rcx
je 0x1dc7
movq $-0x1, -0x40(%rbp)
leaq -0x4f0(%rbp), %r12
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %r15, -0x30(%rbp)
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
leaq 0x1493(%rip), %rdx      # 0x3123
movq %r15, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
cmpl $-0x1, %eax
je 0x1d45
movq %r12, %rdi
movq %r13, %r15
movq -0x58(%rbp), %rax
movq $0x0, (%rax,%r14,8)
leaq 0x1461(%rip), %rsi      # 0x312a
callq 0x10f0
movq %rax, %r13
movq -0x50(%rbp), %rax
movq %r13, (%rax,%r14,8)
testq %r13, %r13
jne 0x1ced
leaq 0x1447(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2c98
movq %r13, %rdi
callq 0x10c0
movl %eax, %edi
leaq -0xf0(%rbp), %rsi
callq 0x1170
cmpl $-0x1, %eax
jne 0x1d17
leaq 0x142b(%rip), %rdi      # 0x313a
movq %r12, %rsi
callq 0x2c98
movq -0xc0(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq $-0x1, %rcx
movq %r15, %r13
je 0x1d8a
cmpq %rax, %rcx
movq -0x30(%rbp), %r15
je 0x1d92
leaq 0x140d(%rip), %rdi      # 0x3148
movq %r12, %rsi
callq 0x2cdd
jmp 0x1d92
leaq 0x33ac(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1da6
movq -0x50(%rbp), %rax
movq $0x0, (%rax,%r14,8)
movq %r12, %rdi
leaq 0x13f1(%rip), %rsi      # 0x3158
callq 0x10f0
movq -0x58(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
testq %rax, %rax
jne 0x1d95
leaq 0x13ac(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2c98
jmp 0x1d95
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %r15
incl %r13d
incq %r14
movl 0x4(%rbx), %esi
cmpq %rsi, %r14
jb 0x1c81
jmp 0x1dc3
movq 0x324b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x13c6(%rip), %rsi      # 0x317d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1d51
movl (%rbx), %eax
jmp 0x1dd4
movq $-0x1, -0x40(%rbp)
xorl %esi, %esi
xorl %r13d, %r13d
testl %eax, %eax
je 0x1e9e
movq %r13, -0x60(%rbp)
leaq 0x136a(%rip), %r14      # 0x3151
leaq -0x4f0(%rbp), %r12
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
testl %eax, %eax
je 0x1e39
leaq 0x32d2(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1e7a
movq -0x48(%rbp), %rax
movq $0x0, (%rax,%r13,8)
jmp 0x1e6e
movq %r12, %rdi
leaq 0x12e7(%rip), %rsi      # 0x312a
callq 0x10f0
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
testq %rax, %rax
jne 0x1e64
leaq 0x12d0(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2c98
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x38(%rbp)
incq %r13
movl (%rbx), %eax
cmpq %rax, %r13
jb 0x1df9
jmp 0x1e97
movq 0x3177(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x12f2(%rip), %rsi      # 0x317d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x1e2b
movl 0x4(%rbx), %esi
movq -0x60(%rbp), %r13
movl %esi, %eax
subl %r13d, %eax
jne 0x1eb8
movq $0x0, -0x30(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x20f4
movq -0x38(%rbp), %rcx
cmpl %eax, %ecx
jb 0x2181
leal (%rcx,%r13), %r14d
leaq 0x3229(%rip), %r12      # 0x50f8
cmpl $0x0, (%r12)
jne 0x219f
movl %r14d, %edi
callq 0x2407
movq %rax, %r14
movl 0x4(%rbx), %ecx
testl %ecx, %ecx
je 0x1f38
xorl %edx, %edx
xorl %eax, %eax
movq -0x50(%rbp), %rsi
cmpq $0x0, (%rsi,%rdx,8)
je 0x1f2c
testl %ecx, %ecx
je 0x1f28
movq 0x8(%r14), %rsi
xorl %edi, %edi
xorl %ecx, %ecx
cmpq %rdi, %rdx
sete %cl
movl 0x4(%r14), %r8d
imull %eax, %r8d
addl %edi, %r8d
movl %ecx, (%rsi,%r8,4)
movl 0x4(%rbx), %ecx
incq %rdi
cmpl %ecx, %edi
jb 0x1f05
jmp 0x1f2a
xorl %ecx, %ecx
incl %eax
incq %rdx
movl %ecx, %esi
cmpq %rsi, %rdx
jb 0x1ef0
jmp 0x1f3c
xorl %ecx, %ecx
xorl %eax, %eax
movl (%rbx), %esi
testl %esi, %esi
je 0x1f97
xorl %edx, %edx
movq -0x48(%rbp), %rdi
cmpq $0x0, (%rdi,%rdx,8)
je 0x1f8d
testl %ecx, %ecx
je 0x1f83
movq 0x8(%rbx), %rsi
movq 0x8(%r14), %rdi
xorl %r8d, %r8d
imull %edx, %ecx
addl %r8d, %ecx
movl (%rsi,%rcx,4), %ecx
movl 0x4(%r14), %r9d
imull %eax, %r9d
addl %r8d, %r9d
movl %ecx, (%rdi,%r9,4)
incl %r8d
movl 0x4(%rbx), %ecx
cmpl %ecx, %r8d
jb 0x1f5e
jmp 0x1f85
xorl %ecx, %ecx
incl %eax
cmpl %eax, %ecx
je 0x1f97
movl (%rbx), %esi
incq %rdx
movl %esi, %edi
cmpq %rdi, %rdx
jb 0x1f44
cmpl $0x0, (%r12)
jne 0x21c2
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x2938
movl 0x4(%rbx), %edi
callq 0x2d40
movq %rax, %r12
movl 0x4(%rbx), %edi
callq 0x2d40
movq %rax, %r13
movq -0x40(%rbp), %rdi
callq 0x138c
testq %rax, %rax
je 0x20f4
xorl %r14d, %r14d
movq %r14, -0x38(%rbp)
movq %r12, %rdi
callq 0x2d2c
movl 0x4(%rbx), %eax
movl $0x0, %r14d
testl %eax, %eax
je 0x2038
xorl %r15d, %r15d
xorl %r14d, %r14d
movq -0x50(%rbp), %rcx
movq (%rcx,%r15,8), %rsi
testq %rsi, %rsi
je 0x202e
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r12), %rdi
callq 0x1391
cmpq $0x1, %rax
je 0x2028
leaq 0x1137(%rip), %rdi      # 0x315a
callq 0x2c88
incl %r14d
movl 0x4(%rbx), %eax
incq %r15
movl %eax, %ecx
cmpq %rcx, %r15
jb 0x1ff8
movl (%rbx), %eax
testl %eax, %eax
je 0x2086
xorl %r15d, %r15d
movq -0x48(%rbp), %rcx
movq (%rcx,%r15,8), %rsi
testq %rsi, %rsi
je 0x207c
movslq %r14d, %rdi
shlq $0x2, %rdi
addq 0x8(%r12), %rdi
callq 0x1391
cmpq $0x1, %rax
je 0x2071
leaq 0x115f(%rip), %rdi      # 0x31cb
callq 0x2c88
incl %r14d
cmpl %r14d, 0x4(%rbx)
je 0x2086
movl (%rbx), %eax
incq %r15
movl %eax, %ecx
cmpq %rcx, %r15
jb 0x2041
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %r12, %rdx
callq 0x2bbe
cmpl $0x0, 0x4(%rbx)
je 0x20db
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x58(%rbp), %rax
movq (%rax,%r14,2), %rsi
testq %rsi, %rsi
je 0x20cc
movq 0x8(%r13), %rdi
addq %r14, %rdi
callq 0x1394
cmpq $0x1, %rax
je 0x20cc
leaq 0x10a3(%rip), %rdi      # 0x316a
callq 0x2c88
incq %r15
movl 0x4(%rbx), %eax
addq $0x4, %r14
cmpq %rax, %r15
jb 0x20a1
movq -0x38(%rbp), %r14
incq %r14
movq -0x40(%rbp), %rdi
callq 0x138c
cmpq %r14, %rax
ja 0x1fd9
xorl %r14d, %r14d
cmpl $0x0, 0x4(%rbx)
je 0x212f
xorl %r15d, %r15d
movq -0x50(%rbp), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x2112
callq 0x1060
movq -0x58(%rbp), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x2124
callq 0x1060
incq %r15
movl 0x4(%rbx), %eax
cmpq %rax, %r15
jb 0x2100
movl (%rbx), %eax
testl %eax, %eax
je 0x2156
xorl %r15d, %r15d
movq -0x48(%rbp), %rcx
movq (%rcx,%r15,8), %rdi
testq %rdi, %rdi
je 0x214c
callq 0x1060
movl (%rbx), %eax
incq %r15
movl %eax, %ecx
cmpq %rcx, %r15
jb 0x2138
movq %r12, %rdi
callq 0x2d80
movq %r13, %rdi
callq 0x2d80
movq -0x30(%rbp), %rdi
callq 0x2458
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x11ba
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $0x0, -0x30(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0x20f7
movq 0x2e52(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xfed(%rip), %rsi       # 0x319d
movl %r13d, %edx
xorl %eax, %eax
callq 0x10b0
movl 0x4(%rbx), %esi
jmp 0x1eda
movq %r14, %rdi
callq 0x1190
jmp 0x1fa2
nop