// File: 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:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movl %esi, 0x5c(%rsp)
movl %edx, 0x58(%rsp)
movl 0xc927(%rip), %eax      # 0x1922e
movl %eax, 0x54(%rsp)
movb $0x3, 0x53(%rsp)
movq 0x60(%rsp), %rdi
leaq 0xb7de(%rip), %rsi      # 0x180fa
callq 0x3320
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0xc948
movq 0x60(%rsp), %rsi
leaq 0xc8f8(%rip), %rdi      # 0x19232
movb $0x0, %al
callq 0x3040
xorl %edi, %edi
callq 0x3340
leaq 0x45(%rsp), %rdi
movq 0x48(%rsp), %rcx
movl $0x1, %esi
movl $0x3, %edx
callq 0x3400
cmpq $0x3, %rax
je 0xc97c
leaq 0xc8e0(%rip), %rdi      # 0x1924e
movb $0x0, %al
callq 0x3040
xorl %edi, %edi
callq 0x3340
movzbl 0x45(%rsp), %eax
cmpl $0x6c, %eax
jne 0xc990
movzbl 0x46(%rsp), %eax
cmpl $0x1b, %eax
je 0xc9af
movzbl 0x45(%rsp), %esi
movzbl 0x46(%rsp), %edx
leaq 0xc8ca(%rip), %rdi      # 0x1926b
movb $0x0, %al
callq 0x3040
xorl %edi, %edi
callq 0x3340
movl 0x5c(%rsp), %edi
movl 0x58(%rsp), %esi
callq 0x13c40
movq %rax, 0x38(%rsp)
movl $0x1, %edi
callq 0x3030
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x3140
movzbl 0x47(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
cmpl $0x0, 0x28(%rsp)
jne 0xca5b
movq 0x48(%rsp), %rdi
callq 0x3450
cmpl $0x0, %eax
jne 0xca3e
movq 0x48(%rsp), %rcx
leaq 0x17(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x3400
cmpq $0x0, %rax
jne 0xca53
leaq 0xc85c(%rip), %rdi      # 0x192a1
movb $0x0, %al
callq 0x3040
xorl %edi, %edi
callq 0x3340
movl $0x4, 0x28(%rsp)
movzbl 0x17(%rsp), %eax
andl $0x3, %eax
movb %al, 0x16(%rsp)
movzbl 0x17(%rsp), %eax
sarl $0x2, %eax
movb %al, 0x17(%rsp)
movl 0x28(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x28(%rsp)
movzbl 0x16(%rsp), %eax
movb 0x54(%rsp,%rax), %cl
movq 0x30(%rsp), %rax
movb %cl, (%rax)
movq 0x30(%rsp), %rax
movzbl (%rax), %edx
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
cmpl $0x3, (%rax,%rcx,4)
jne 0xcaf0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0xcba2
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
cmpl $0x1, (%rax,%rcx,4)
jne 0xcb2d
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
movl $0x3, (%rax,%rcx,4)
jmp 0xcba0
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0xcb67
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
cmpl $0x3, (%rax,%rcx,4)
jle 0xcb9e
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0xc730(%rip), %rdi      # 0x192c0
movb $0x0, %al
callq 0x3040
xorl %edi, %edi
callq 0x3340
jmp 0xcba0
jmp 0xcba2
cmpl $0x0, 0x2c(%rsp)
je 0xcbf7
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jne 0xcbf5
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x58(%rsp), %eax
jne 0xcbf3
jmp 0xcc53
jmp 0xcbf5
jmp 0xcc4e
movslq 0x5c(%rsp), %rax
addq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x58(%rsp), %eax
jne 0xcc4c
movl $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jne 0xcc42
jmp 0xcc53
movslq 0x24(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0xcc4e
jmp 0xca0b
movq 0x48(%rsp), %rdi
callq 0x3290
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xcc77
movq 0x8(%rsp), %rdi
callq 0x3390
movq 0x38(%rsp), %rax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)