// File: /workspace/github_cpp_makefile_star2_O2/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 0x420c(%rip), %rsi      # 0xb0f7
callq 0x2260
testq %rax, %rax
je 0x7053
movq %rax, %rbx
leaq 0x9(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x3
popq %rdx
movq %rax, %rcx
callq 0x2350
cmpq $0x3, %rax
jne 0x7066
movq %rbx, 0x10(%rsp)
movzbl 0x9(%rsp), %esi
movb 0xa(%rsp), %al
cmpl $0x6c, %esi
jne 0x706f
cmpb $0x1b, %al
jne 0x706f
movl %ebp, %edi
movl 0xc(%rsp), %esi
callq 0x90f4
movq %rax, %r12
xorl %r13d, %r13d
movb 0xb(%rsp), %r9b
leaq 0x5317(%rip), %r8       # 0xc26c
pushq $0x1
popq %r10
xorl %ebx, %ebx
xorl %eax, %eax
movb %r9b, 0x7(%rsp)
testl %eax, %eax
je 0x6f6a
xorl %ecx, %ecx
jmp 0x6fbb
movl %ebp, %r15d
movq %r8, %r14
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x2390
testl %eax, %eax
jne 0x703e
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %rbp, %rcx
callq 0x2350
testq %rax, %rax
je 0x703e
movzbl 0x8(%rsp), %ecx
andl $0x3, %ecx
pushq $0x4
popq %rax
movq %r14, %r8
movl %r15d, %ebp
movb 0x7(%rsp), %r9b
pushq $0x1
popq %r10
movzbl (%rcx,%r8), %esi
movq 0x8(%r12), %rcx
movslq %r13d, %rdx
movq (%rcx,%rdx,8), %rcx
movslq %ebx, %rdx
movl %esi, (%rcx,%rdx,4)
movl %r10d, %edi
cmpl $0x3, %esi
je 0x6fe2
cmpl $0x1, %esi
jne 0x6fe5
pushq $0x3
popq %rdi
movl %edi, (%rcx,%rdx,4)
decl %eax
testb %r9b, %r9b
je 0x7005
incl %r13d
cmpl %ebp, %r13d
jne 0x6f62
incl %ebx
cmpl 0xc(%rsp), %ebx
je 0x7022
xorl %r13d, %r13d
jmp 0x701b
incl %ebx
cmpl 0xc(%rsp), %ebx
jne 0x6f62
incl %r13d
cmpl %ebp, %r13d
je 0x7022
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0x6f62
movq 0x10(%rsp), %rdi
callq 0x2210
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5208(%rip), %rdi      # 0xc24d
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2280
leaq 0x5184(%rip), %rdi      # 0xc1de
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
jmp 0x704c
leaq 0x518d(%rip), %rdi      # 0xc1fa
jmp 0x7045
movzbl %al, %edx
leaq 0x519e(%rip), %rdi      # 0xc217
xorl %eax, %eax
callq 0x2040
jmp 0x704c