// File: /workspace/github_cpp_makefile_star2_O3/GenisGE[P]evalAdmix/filereader_and_conversions.cpp
// Function: bed_to_iMatrix(char const*, int, int)
iMatrix *bed_to_iMatrix(const char* file, int nrow,int ncol) {

  //  const unsigned char recode[4] = {'\x01', '\x00', '\x02', '\x03'};
  //0,1,2: 3 is missing 
  const unsigned char recode[4] = { '\x02','\x01', '\x03', '\x00'};
  const unsigned char mask = '\x03';


  FILE *in = fopen(file, "r");
  if (!in){
    printf("Couln't open input file: %s", file);
    exit(0);
  }
  unsigned char start[3];
  if (fread(start, 1, 3, in)!=3){
    printf("Failed to read first 3 bytes");
    exit(0);
  }
  if (start[0]!='\x6C' || start[1]!='\x1B'){
    printf("Input file does not appear to be a .bed file (%X, %X)", 
	   start[0], start[1]);
    exit(0);
  }
  /* Create output object */
  
  
  iMatrix *returnMat = allocIntMatrix(nrow,ncol);
  unsigned char *result = new unsigned char[1];
  memset(result, 0x00, 1);
  // int ncell = nrow*ncol;
  // unsigned char *result = new unsigned char[nrow*ncol]; 
  // memset(result, 0x00, ncell);

  /* Read in data */

  int snp_major = start[2];
  int part=0, i=0, j=0;
  size_t ij=0;
  while (1) {
    unsigned char byte;
    if (!part) {
      if (feof(in) || !fread(&byte, 1, 1, in)) {
	printf("Unexpected end of file reached");
	exit(0);
      }
      part = 4;
    }
    unsigned char code = byte & mask;
    byte = byte >> 2;
    part--;
    // result[ij] = recode[code];
    // returnMat->matrix[i][j] = result[ij];
    result[0] = recode[code];
    returnMat->matrix[i][j] = result[0];
    if(returnMat->matrix[i][j]==3)
      returnMat->matrix[i][j]=1;
    else if(returnMat->matrix[i][j]==1)
      returnMat->matrix[i][j]=3;
    else if(returnMat->matrix[i][j]<0 || returnMat->matrix[i][j]>3){
      printf("Problem in bed file at position=(%d,%d)=%d\n",i,j,returnMat->matrix[i][j]);
      exit(0);
    }
    // printf("(%d,%d)=%d ",i,j,result[ij]);
    if (snp_major) {
      ij++;
      i++;
      if (i==nrow) {
	i = part = 0;
	j++;
	if (j==ncol)
	  break;
      }
    }	
    else {
      ij += nrow;
      j++;
      if (j==ncol){
	j = part = 0;
	i++;
	if (i==nrow)
	  break;
	ij = i;
      }
    }
  }
  fclose(in);
  delete [] result;
  return returnMat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, 0xc(%rsp)
movl %esi, %ebp
movq %rdi, %r15
leaq 0x4eb6(%rip), %rsi      # 0xd137
callq 0x2270
testq %rax, %rax
je 0x83ef
movq %rax, %rbx
leaq 0x9(%rsp), %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %rax, %rcx
callq 0x2340
cmpq $0x3, %rax
jne 0x8402
movq %rbx, 0x10(%rsp)
movzbl 0x9(%rsp), %esi
movb 0xa(%rsp), %al
cmpl $0x6c, %esi
jne 0x840b
cmpb $0x1b, %al
jne 0x840b
movl %ebp, %edi
movl 0xc(%rsp), %esi
callq 0xa698
movq %rax, %r13
xorl %r12d, %r12d
movb 0xb(%rsp), %r8b
leaq 0x5fbd(%rip), %rdi      # 0xe2ac
xorl %ebx, %ebx
xorl %eax, %eax
movb %r8b, 0x7(%rsp)
testl %eax, %eax
je 0x8300
xorl %ecx, %ecx
jmp 0x8353
movl %ebp, %r15d
movq %rdi, %r14
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x2380
testl %eax, %eax
jne 0x83da
movl $0x1, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbp, %rcx
callq 0x2340
testq %rax, %rax
je 0x83da
movzbl 0x8(%rsp), %ecx
andl $0x3, %ecx
movl $0x4, %eax
movq %r14, %rdi
movl %r15d, %ebp
movb 0x7(%rsp), %r8b
movzbl (%rcx,%rdi), %esi
movq 0x8(%r13), %rcx
movslq %r12d, %rdx
movq (%rcx,%rdx,8), %rcx
movslq %ebx, %rdx
movl %esi, (%rcx,%rdx,4)
cmpl $0x3, %esi
je 0x8379
cmpl $0x1, %esi
jne 0x8381
movl $0x3, %esi
jmp 0x837e
movl $0x1, %esi
movl %esi, (%rcx,%rdx,4)
decl %eax
testb %r8b, %r8b
je 0x83a1
incl %r12d
cmpl %ebp, %r12d
jne 0x82f8
incl %ebx
cmpl 0xc(%rsp), %ebx
je 0x83be
xorl %r12d, %r12d
jmp 0x83b7
incl %ebx
cmpl 0xc(%rsp), %ebx
jne 0x82f8
incl %r12d
cmpl %ebp, %r12d
je 0x83be
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0x82f8
movq 0x10(%rsp), %rdi
callq 0x2210
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5eac(%rip), %rdi      # 0xe28d
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2290
leaq 0x5e28(%rip), %rdi      # 0xe21e
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
jmp 0x83e8
leaq 0x5e31(%rip), %rdi      # 0xe23a
jmp 0x83e1
movzbl %al, %edx
leaq 0x5e42(%rip), %rdi      # 0xe257
xorl %eax, %eax
callq 0x2040
jmp 0x83e8