// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movl %edx, 0x4(%rsp)
movl %esi, %ebp
movq %rdi, %r15
leaq 0x4ef1(%rip), %rsi      # 0xd137
callq 0x2270
testq %rax, %rax
je 0x839e
movq %rax, %r14
leaq 0x1(%rsp), %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %rax, %rcx
callq 0x2340
cmpq $0x3, %rax
jne 0x83b1
movzbl 0x1(%rsp), %esi
movb 0x2(%rsp), %al
cmpl $0x6c, %esi
jne 0x83ba
cmpb $0x1b, %al
jne 0x83ba
movl %ebp, %edi
movl 0x4(%rsp), %esi
callq 0xa5cc
movq %rax, %r15
xorl %r12d, %r12d
movb 0x3(%rsp), %r13b
xorl %ebx, %ebx
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x82e3
movq %r14, %rdi
callq 0x2380
testl %eax, %eax
jne 0x8389
movl $0x1, %esi
movl $0x1, %edx
movq %rsp, %rdi
movq %r14, %rcx
callq 0x2340
movl $0x4, %ecx
testq %rax, %rax
je 0x8389
movzbl (%rsp), %eax
movl %eax, %edx
andl $0x3, %edx
shrb $0x2, %al
movb %al, (%rsp)
leaq 0x5fb3(%rip), %rax      # 0xe2ac
movzbl (%rdx,%rax), %esi
movq 0x8(%r15), %rax
movslq %r12d, %rdx
movq (%rax,%rdx,8), %rax
movslq %ebx, %rdx
movl %esi, (%rax,%rdx,4)
cmpl $0x3, %esi
je 0x831f
cmpl $0x1, %esi
jne 0x8327
movl $0x3, %esi
jmp 0x8324
movl $0x1, %esi
movl %esi, (%rax,%rdx,4)
decl %ecx
testb %r13b, %r13b
je 0x8349
incl %r12d
cmpl %ebp, %r12d
jne 0x8365
incl %ebx
xorl %eax, %eax
cmpl 0x4(%rsp), %ebx
sete %al
leal (%rax,%rax,2), %eax
xorl %r12d, %r12d
jmp 0x8361
incl %ebx
cmpl 0x4(%rsp), %ebx
jne 0x8365
incl %r12d
xorl %eax, %eax
cmpl %ebp, %r12d
sete %al
leal (%rax,%rax,2), %eax
xorl %ebx, %ebx
xorl %ecx, %ecx
jmp 0x8367
xorl %eax, %eax
testl %eax, %eax
je 0x82ac
movq %r14, %rdi
callq 0x2210
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5efd(%rip), %rdi      # 0xe28d
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2290
leaq 0x5e79(%rip), %rdi      # 0xe21e
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
jmp 0x8397
leaq 0x5e82(%rip), %rdi      # 0xe23a
jmp 0x8390
movzbl %al, %edx
leaq 0x5e93(%rip), %rdi      # 0xe257
xorl %eax, %eax
callq 0x2040
jmp 0x8397