// File: /workspace/github_c_makefile_star2_O0/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
subq $0x530, %rsp            # imm = 0x530
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x528(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x520(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x518(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movq $-0x1, -0x4d8(%rbp)
movq $0x0, -0x4e8(%rbp)
movl $0x0, -0x4ec(%rbp)
movl $0x0, -0x4f0(%rbp)
movq $0x0, -0x500(%rbp)
movq $0x0, -0x508(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x210c
leaq -0x440(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl -0x14(%rbp), %r8d
movl $0x400, %esi            # imm = 0x400
leaq 0x21b3(%rip), %rdx      # 0x4123
movb $0x0, %al
callq 0x1070
leaq -0x440(%rbp), %rdi
xorl %esi, %esi
callq 0x10d0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x2016
leaq 0x414d(%rip), %rax      # 0x60e8
cmpl $0x0, (%rax)
je 0x1fbf
movq 0x4051(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq -0x440(%rbp), %rdx
leaq 0x21c5(%rip), %rsi      # 0x417d
movb $0x0, %al
callq 0x10a0
movq -0x528(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
leaq -0x440(%rbp), %rdi
leaq 0x2178(%rip), %rsi      # 0x4158
callq 0x10e0
movq %rax, %rcx
movq -0x520(%rbp), %rax
movslq -0x14(%rbp), %rdx
movq %rcx, (%rax,%rdx,8)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x2011
leaq -0x440(%rbp), %rsi
leaq 0x2120(%rip), %rdi      # 0x412c
callq 0x37f0
jmp 0x20fc
movq -0x520(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
leaq -0x440(%rbp), %rdi
leaq 0x20f3(%rip), %rsi      # 0x412a
callq 0x10e0
movq %rax, %rcx
movq -0x528(%rbp), %rax
movslq -0x14(%rbp), %rdx
movq %rcx, (%rax,%rdx,8)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x2068
leaq -0x440(%rbp), %rsi
leaq 0x20c9(%rip), %rdi      # 0x412c
callq 0x37f0
movq -0x528(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10b0
movl %eax, %edi
leaq -0x4d0(%rbp), %rsi
callq 0x1150
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x20a8
leaq -0x440(%rbp), %rsi
leaq 0x2097(%rip), %rdi      # 0x413a
callq 0x37f0
movq $-0x1, %rax
cmpq -0x4d8(%rbp), %rax
jne 0x20c8
movq -0x4a0(%rbp), %rax
movq %rax, -0x4d8(%rbp)
jmp 0x20ed
movq -0x4d8(%rbp), %rax
cmpq -0x4a0(%rbp), %rax
je 0x20eb
leaq -0x440(%rbp), %rsi
leaq 0x2062(%rip), %rdi      # 0x4148
callq 0x3860
jmp 0x20ed
movl -0x4ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4ec(%rbp)
jmp 0x20fe
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f45
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x21f5
leaq -0x440(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl -0x14(%rbp), %r8d
movl $0x400, %esi            # imm = 0x400
leaq 0x2014(%rip), %rdx      # 0x4151
movb $0x0, %al
callq 0x1070
leaq -0x440(%rbp), %rdi
xorl %esi, %esi
callq 0x10d0
cmpl $0x0, %eax
je 0x2197
leaq 0x3f8a(%rip), %rax      # 0x60e8
cmpl $0x0, (%rax)
je 0x2182
movq 0x3e8e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq -0x440(%rbp), %rdx
leaq 0x2002(%rip), %rsi      # 0x417d
movb $0x0, %al
callq 0x10a0
movq -0x518(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x21e5
leaq -0x440(%rbp), %rdi
leaq 0x1f85(%rip), %rsi      # 0x412a
callq 0x10e0
movq %rax, %rcx
movq -0x518(%rbp), %rax
movslq -0x14(%rbp), %rdx
movq %rcx, (%rax,%rdx,8)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x21d6
leaq -0x440(%rbp), %rsi
leaq 0x1f5b(%rip), %rdi      # 0x412c
callq 0x37f0
movl -0x4f0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4f0(%rbp)
jmp 0x21e7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2113
movl -0x4ec(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x2213
movl $0x0, -0x50c(%rbp)
jmp 0x2694
movl -0x4f0(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
subl -0x4ec(%rbp), %ecx
cmpl %ecx, %eax
jae 0x2251
movq 0x3dc7(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1f51(%rip), %rsi      # 0x418c
movb $0x0, %al
callq 0x10a0
movl $0xffffffff, -0x50c(%rbp) # imm = 0xFFFFFFFF
jmp 0x2694
movl -0x4ec(%rbp), %eax
addl -0x4f0(%rbp), %eax
movl %eax, -0x4f4(%rbp)
leaq 0x3e7e(%rip), %rax      # 0x60e8
cmpl $0x0, (%rax)
je 0x2293
movq 0x3d82(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl -0x4ec(%rbp), %edx
movl -0x4f0(%rbp), %ecx
leaq 0x1f11(%rip), %rsi      # 0x419d
movb $0x0, %al
callq 0x10a0
movl -0x4f4(%rbp), %edi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x2a10
movq %rax, -0x4e8(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x236a
movq -0x528(%rbp), %rcx
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x235a
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2351
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x231f
movq -0x4e8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x4e8(%rbp), %rdx
imull 0x4(%rdx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, %ecx
movl $0x1, (%rax,%rcx,4)
jmp 0x2344
movq -0x4e8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x4e8(%rbp), %rdx
imull 0x4(%rdx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, %ecx
movl $0x0, (%rax,%rcx,4)
jmp 0x2346
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x22e4
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x235c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x22ba
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x2416
movq -0x518(%rbp), %rcx
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x2406
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x23ed
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rdx
imull 0x4(%rdx), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x4e8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x4e8(%rbp), %rsi
imull 0x4(%rsi), %ecx
addl -0x18(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x239a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0x2404
jmp 0x2416
jmp 0x2406
jmp 0x2408
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2371
leaq 0x3ccb(%rip), %rax      # 0x60e8
cmpl $0x0, (%rax)
je 0x2446
movq 0x3bcf(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1d87(%rip), %rsi      # 0x41ba
movb $0x0, %al
callq 0x10a0
movq -0x4e8(%rbp), %rdi
callq 0x2ab0
movq -0x4e8(%rbp), %rdi
callq 0x3240
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x3950
movq %rax, -0x500(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x3950
movq %rax, -0x508(%rbp)
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x530(%rbp)
movq -0x4d8(%rbp), %rdi
callq 0x1260
movq %rax, %rcx
movq -0x530(%rbp), %rax
cmpq %rcx, %rax
jae 0x268a
movq -0x500(%rbp), %rdi
callq 0x3920
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2543
movq -0x528(%rbp), %rcx
movslq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x2536
movq -0x528(%rbp), %rax
movq -0x500(%rbp), %rcx
movq 0x8(%rcx), %rdi
movslq -0x1c(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1280
movq %rax, -0x4e0(%rbp)
movl $0x1, %eax
cmpq -0x4e0(%rbp), %rax
je 0x252d
leaq 0x1c32(%rip), %rdi      # 0x415a
callq 0x37d0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2538
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x24c3
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x25e0
movq -0x518(%rbp), %rcx
movslq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x25d0
movq -0x518(%rbp), %rax
movq -0x500(%rbp), %rcx
movq 0x8(%rcx), %rdi
movslq -0x1c(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1280
movq %rax, -0x4e0(%rbp)
movl $0x1, %eax
cmpq -0x4e0(%rbp), %rax
je 0x25b7
leaq 0x1c19(%rip), %rdi      # 0x41cb
callq 0x37d0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0x25ce
jmp 0x25e0
jmp 0x25d0
jmp 0x25d2
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x254a
movq -0x508(%rbp), %rdi
movq -0x4e8(%rbp), %rsi
movq -0x500(%rbp), %rdx
callq 0x36a0
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x267a
movq -0x520(%rbp), %rcx
movslq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x266d
movq -0x520(%rbp), %rax
movq -0x508(%rbp), %rcx
movq 0x8(%rcx), %rdi
movslq -0x18(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rdi
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1290
movq %rax, -0x4e0(%rbp)
movl $0x1, %eax
cmpq -0x4e0(%rbp), %rax
je 0x266b
leaq 0x1b04(%rip), %rdi      # 0x416a
callq 0x37d0
jmp 0x266d
jmp 0x266f
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2601
jmp 0x267c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x247f
movl $0x0, -0x50c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2702
movq -0x528(%rbp), %rcx
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x26ce
movq -0x528(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1050
movq -0x520(%rbp), %rcx
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x26f5
movq -0x520(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1050
jmp 0x26f7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x269b
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x2748
movq -0x518(%rbp), %rcx
movslq -0x14(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
je 0x273b
movq -0x518(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1050
jmp 0x273d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2709
movq -0x500(%rbp), %rdi
callq 0x39a0
movq -0x508(%rbp), %rdi
callq 0x39a0
movq -0x4e8(%rbp), %rdi
callq 0x2a80
movl -0x50c(%rbp), %eax
movq -0x28(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop