// File: /workspace/github_c_makefile_star2_O2/vrancurel[P]ec/ec.c
// Function: create_coding_files
void create_coding_files(char *prefix, t_mat *mat)
{
  int i, j;
  FILE *d_files[mat->n_cols];
  FILE *c_files[mat->n_rows];
  char filename[1024];
  struct stat stbuf;
  size_t size = -1, wc;
  t_vec *words = NULL;
  t_vec *output = NULL;

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

  for (i = 0;i < mat->n_cols;i++) {
    snprintf(filename, sizeof (filename), "%s.d%d", prefix, i);
    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);
  }
  
  for (i = 0;i < mat->n_rows;i++) {
    snprintf(filename, sizeof (filename), "%s.c%d", prefix, i);
    if (NULL == (c_files[i] = fopen(filename, "w")))
      xerrormsg("error opening", filename);
  }
  
  words = vec_xcalloc(mat->n_cols);
  output = vec_xcalloc(mat->n_cols);
  
  for (i = 0;i < sizew(size);i++) {
    vec_zero(words);
    for (j = 0;j < mat->n_cols;j++) {
      wc = freadw(&VEC_ITEM(words, j), d_files[j]);
      if (1 != wc)
        xperror("short read data");
    }
    mat_mult(output, mat, words);
    for (j = 0;j < mat->n_rows;j++) {
      wc = fwritew(& VEC_ITEM(output, j), c_files[j]);
      if (1 != wc)
        xperror("short write coding");
    }
  } 
    
  for (i = 0;i < mat->n_cols;i++) {
    fclose(d_files[i]);
  }
  
  for (i = 0;i < mat->n_rows;i++) {
    fclose(c_files[i]);
  }

  vec_free(words);
  vec_free(output);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
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, -0x38(%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
leaq 0x392f(%rip), %rax      # 0x50f8
cmpl $0x0, (%rax)
jne 0x1a1a
pushq $-0x1
popq %r13
leaq -0x4e0(%rbp), %r12
xorl %r15d, %r15d
movl 0x4(%rbx), %eax
cmpq %rax, %r15
jae 0x188f
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
leaq 0x1928(%rip), %rdx      # 0x3123
movq -0x30(%rbp), %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
leaq 0x1917(%rip), %rsi      # 0x312a
callq 0x10f0
movq %rax, %r14
movq -0x38(%rbp), %rax
movq %r14, (%rax,%r15,8)
testq %r14, %r14
jne 0x1837
leaq 0x18fd(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2a0e
movq %r14, %rdi
callq 0x10c0
movl %eax, %edi
leaq -0xd8(%rbp), %rsi
callq 0x1170
cmpl $-0x1, %eax
jne 0x1861
leaq 0x18e1(%rip), %rdi      # 0x313a
movq %r12, %rsi
callq 0x2a0e
movq -0xa8(%rbp), %rax
cmpq $-0x1, %r13
je 0x1884
cmpq %rax, %r13
je 0x1887
leaq 0x18ce(%rip), %rdi      # 0x3148
movq %r12, %rsi
callq 0x2a51
jmp 0x1887
movq %rax, %r13
incq %r15
jmp 0x17e0
leaq -0x4e0(%rbp), %r12
leaq 0x18bb(%rip), %r15      # 0x3158
xorl %r14d, %r14d
movl (%rbx), %eax
cmpq %rax, %r14
jae 0x18f0
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
leaq 0x189b(%rip), %rdx      # 0x3151
movq -0x30(%rbp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1080
movq %r12, %rdi
movq %r15, %rsi
callq 0x10f0
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
testq %rax, %rax
jne 0x18eb
leaq 0x1849(%rip), %rdi      # 0x312c
movq %r12, %rsi
callq 0x2a0e
incq %r14
jmp 0x18a0
movl 0x4(%rbx), %edi
callq 0x2ac0
movq %rax, %r15
movl 0x4(%rbx), %edi
callq 0x2ac0
movq %rax, %r12
xorl %r14d, %r14d
movq %r13, -0x48(%rbp)
movq %r13, %rdi
callq 0x127c
cmpq %r14, %rax
jbe 0x19c2
movq %r14, -0x30(%rbp)
movq %r15, %rdi
callq 0x2aac
xorl %r14d, %r14d
xorl %r13d, %r13d
movl 0x4(%rbx), %eax
cmpq %rax, %r13
jae 0x1967
movq 0x8(%r15), %rdi
addq %r14, %rdi
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %rsi
callq 0x1281
cmpq $0x1, %rax
je 0x195e
leaq 0x1801(%rip), %rdi      # 0x315a
callq 0x2a00
incq %r13
addq $0x4, %r14
jmp 0x1930
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x294a
xorl %r14d, %r14d
xorl %r13d, %r13d
movl (%rbx), %eax
cmpq %rax, %r13
jae 0x19b2
movq 0x8(%r12), %rdi
addq %r14, %rdi
movq -0x40(%rbp), %rax
movq (%rax,%r13,8), %rsi
callq 0x1284
cmpq $0x1, %rax
je 0x19a9
leaq 0x17c6(%rip), %rdi      # 0x316a
callq 0x2a00
incq %r13
addq $0x4, %r14
jmp 0x197b
movq -0x30(%rbp), %r14
incq %r14
movq -0x48(%rbp), %r13
jmp 0x190d
xorl %r14d, %r14d
movl 0x4(%rbx), %eax
cmpq %rax, %r14
jae 0x19df
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x1060
incq %r14
jmp 0x19c5
xorl %r14d, %r14d
movl (%rbx), %eax
cmpq %rax, %r14
jae 0x19fb
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rdi
callq 0x1060
incq %r14
jmp 0x19e2
movq %r15, %rdi
callq 0x2afe
movq %r12, %rdi
callq 0x2afe
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x35d7(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x16e6(%rip), %rdi      # 0x3111
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x1140
movq %rbx, %rdi
callq 0x229a
jmp 0x17d2