// File: GenisGE[P]evalAdmix/filereader_and_conversions.cpp
// Function: readmatrix(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
iMatrix *readmatrix(std::string filename,const std::string delim=",;: \t"){
  ///@param filename A filename to read.@param delim A string of delimiters.
  if(0){
    printf("\t-> will try to open postfile: \"%s\" ... \n",filename.c_str());
    fflush(stdout);
  }
  


  const int SIZE = MAX_ELEMS_PER_LINE;
  char buffer[SIZE];
 
  std::ifstream pFile (filename.c_str(),std::ios::in);
 
  
  if(!pFile){
    printf("\t-> will try to open postfile: \"%s\" ... \n",filename.c_str());
    //    std::cout <<"Problems opening file" <<filename<<std::endl;
    exit(0);
  }
  
  
  int doFirstRow =1;
  int itemsInFirstRow=0;
  int numRows =0;
  flush_print("Checking consistency of file...");
  
  while(!pFile.eof()){
    
    std::vector<std::string> tokens;
    std::string tmp_string;

    pFile.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
    
    if(doFirstRow){
      //if file has a emptystart line
      itemsInFirstRow = get_lexemes(tmp_string,tokens,delim);
      
      if (itemsInFirstRow==0)
	continue;
      // printf("items in first rwo:%d\n",itemsInFirstRow);
      doFirstRow=0;
      numRows++;
    }
    else{
      int nItems = get_lexemes(tmp_string,tokens,delim);
	//if line is empty
	if(nItems==0)
	  continue;
	numRows++;
	if(nItems!=itemsInFirstRow){
	  printf("row length mismatch at line:%d numitems is:%d shouldn't be:%d\t will exit\n",numRows,itemsInFirstRow,nItems);
	  exit(0);
	}
      }
    if ((numRows%20 )==0){
      printf("\r\t-> Checking consistency of file: (checking number of items at line: %d )",numRows);
      fflush(stdout);
    }
  }
  pFile.close();

  fflush(stdout);

  
  iMatrix *mat = allocIntMatrix(numRows,itemsInFirstRow);

  numRows = 0;

  std::ifstream pFile2 (filename.c_str(),std::ios::in);
  while(!pFile2.eof()){
    if ((numRows%5 )==0){
      printf("\r\t-> Checking consistency of file: (Now reading in data at line: %d/%d   ) ",numRows,mat->x);
      fflush(stdout);
    }
    
    std::vector<std::string> tokens;
    std::string tmp_string;

    pFile2.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
   
    int itemsInRow = get_lexemes(tmp_string,tokens,delim);
    if (itemsInRow==0)
	continue;
    for(unsigned int i=0; i <tokens.size();i++){
      mat->matrix[numRows][i] = to_int(tokens[i]);
      if(mat->matrix[numRows][i]<0||mat->matrix[numRows][i]>3){
	printf("\n\t-> Error in genotype data: (%d,%d)=%d, value should be between 0 and 3.\n",numRows,i,mat->matrix[numRows][i]);
	exit(0);
      }
    }

    numRows++;
  }
  pFile2.close();
  //copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, ", "));
  
  fflush(stdout);
  return mat;
}

// Assembly:
subq $0x4c51b8, %rsp         # imm = 0x4C51B8
movq %rdi, 0x68(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdi, 0x4c51b0(%rsp)
movq %rsi, 0x4c51a8(%rsp)
movl $0x4c4b40, 0x4c51a4(%rsp) # imm = 0x4C4B40
callq 0x30f0
movq %rax, %rsi
leaq 0x458(%rsp), %rdi
movl $0x8, %edx
callq 0x3540
movq 0x458(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x458(%rsp,%rax), %rdi
callq 0x3120
movb %al, 0x7f(%rsp)
jmp 0xb825
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0xb82f
jmp 0xb86c
movq 0x68(%rsp), %rdi
callq 0x30f0
movq %rax, %rsi
leaq 0xd8b3(%rip), %rdi      # 0x190f6
xorl %eax, %eax
callq 0x3040
jmp 0xb84c
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xc154
movl $0x1, 0x448(%rsp)
movl $0x0, 0x444(%rsp)
movl $0x0, 0x440(%rsp)
leaq 0xd88c(%rip), %rdi      # 0x19120
callq 0x152a0
jmp 0xb89b
jmp 0xb89d
movq 0x458(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x458(%rsp,%rax), %rdi
callq 0x3550
movb %al, 0x67(%rsp)
jmp 0xb8bc
movb 0x67(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xb8cb
jmp 0xbc64
leaq 0x428(%rsp), %rdi
callq 0xdec0
leaq 0x408(%rsp), %rdi
callq 0x33a0
leaq 0x458(%rsp), %rdi
leaq 0x660(%rsp), %rsi
movl $0x4c4b40, %edx         # imm = 0x4C4B40
callq 0x30c0
jmp 0xb901
leaq 0x3e7(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x3500
movq 0x58(%rsp), %rdx
leaq 0x3e8(%rsp), %rdi
leaq 0x660(%rsp), %rsi
callq 0x9280
jmp 0xb92f
leaq 0x408(%rsp), %rdi
leaq 0x3e8(%rsp), %rsi
callq 0x3350
leaq 0x3e8(%rsp), %rdi
callq 0x3170
leaq 0x3e7(%rsp), %rdi
callq 0x32e0
cmpl $0x0, 0x448(%rsp)
je 0xbaa6
leaq 0x3c0(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x3100
jmp 0xb983
movq 0x70(%rsp), %rsi
leaq 0x3a0(%rsp), %rdi
callq 0x3100
jmp 0xb997
leaq 0x3c0(%rsp), %rdi
leaq 0x428(%rsp), %rsi
leaq 0x3a0(%rsp), %rdx
callq 0xab60
movl %eax, 0x54(%rsp)
jmp 0xb9ba
movl 0x54(%rsp), %eax
movl %eax, 0x444(%rsp)
leaq 0x3a0(%rsp), %rdi
callq 0x3170
leaq 0x3c0(%rsp), %rdi
callq 0x3170
cmpl $0x0, 0x444(%rsp)
jne 0xba85
movl $0x2, 0x39c(%rsp)
jmp 0xbc14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xbc45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0x3e7(%rsp), %rdi
callq 0x32e0
jmp 0xbc45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xba73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0x3a0(%rsp), %rdi
callq 0x3170
leaq 0x3c0(%rsp), %rdi
callq 0x3170
jmp 0xbc45
movl $0x0, 0x448(%rsp)
movl 0x440(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x440(%rsp)
jmp 0xbbcb
leaq 0x378(%rsp), %rdi
leaq 0x408(%rsp), %rsi
callq 0x3100
jmp 0xbabd
movq 0x70(%rsp), %rsi
leaq 0x358(%rsp), %rdi
callq 0x3100
jmp 0xbad1
leaq 0x378(%rsp), %rdi
leaq 0x428(%rsp), %rsi
leaq 0x358(%rsp), %rdx
callq 0xab60
movl %eax, 0x50(%rsp)
jmp 0xbaf4
leaq 0x358(%rsp), %rdi
callq 0x3170
leaq 0x378(%rsp), %rdi
callq 0x3170
movl 0x50(%rsp), %eax
movl %eax, 0x398(%rsp)
cmpl $0x0, 0x398(%rsp)
jne 0xbb7c
movl $0x2, 0x39c(%rsp)
jmp 0xbc14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xbb6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0x358(%rsp), %rdi
callq 0x3170
leaq 0x378(%rsp), %rdi
callq 0x3170
jmp 0xbc45
movl 0x440(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x440(%rsp)
movl 0x398(%rsp), %eax
cmpl 0x444(%rsp), %eax
je 0xbbc9
movl 0x440(%rsp), %esi
movl 0x444(%rsp), %edx
movl 0x398(%rsp), %ecx
leaq 0xd49f(%rip), %rdi      # 0x19058
xorl %eax, %eax
callq 0x3040
jmp 0xbbc2
xorl %edi, %edi
callq 0x3340
jmp 0xbbcb
movl 0x440(%rsp), %eax
movl $0x14, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xbc09
movl 0x440(%rsp), %esi
leaq 0xd553(%rip), %rdi      # 0x19140
xorl %eax, %eax
callq 0x3040
jmp 0xbbf6
movq 0x133c3(%rip), %rax     # 0x1efc0
movq (%rax), %rdi
callq 0x3310
jmp 0xbc07
jmp 0xbc09
movl $0x0, 0x39c(%rsp)
leaq 0x408(%rsp), %rdi
callq 0x3170
leaq 0x428(%rsp), %rdi
callq 0xded0
movl 0x39c(%rsp), %eax
testl %eax, %eax
je 0xbc40
jmp 0xbc3b
jmp 0xb89d
jmp 0xb89d
leaq 0x408(%rsp), %rdi
callq 0x3170
leaq 0x428(%rsp), %rdi
callq 0xded0
jmp 0xc154
leaq 0x458(%rsp), %rdi
callq 0x3070
jmp 0xbc73
movq 0x13346(%rip), %rax     # 0x1efc0
movq (%rax), %rdi
callq 0x3310
jmp 0xbc84
movl 0x440(%rsp), %edi
movl 0x444(%rsp), %esi
callq 0x13c40
movq %rax, 0x48(%rsp)
jmp 0xbc9e
movq 0x68(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0x350(%rsp)
movl $0x0, 0x440(%rsp)
callq 0x30f0
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
movl $0x8, %edx
callq 0x3540
jmp 0xbcd7
jmp 0xbcd9
movq 0x148(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x148(%rsp,%rax), %rdi
callq 0x3550
movb %al, 0x47(%rsp)
jmp 0xbcf8
movb 0x47(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xbd07
jmp 0xc0e8
movl 0x440(%rsp), %eax
movl $0x5, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xbd68
movl 0x440(%rsp), %esi
movq 0x350(%rsp), %rax
movl (%rax), %edx
leaq 0xd458(%rip), %rdi      # 0x1918b
xorl %eax, %eax
callq 0x3040
jmp 0xbd3c
movq 0x1327d(%rip), %rax     # 0x1efc0
movq (%rax), %rdi
callq 0x3310
jmp 0xbd4d
jmp 0xbd68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xc147
leaq 0x130(%rsp), %rdi
callq 0xdec0
leaq 0x110(%rsp), %rdi
callq 0x33a0
leaq 0x148(%rsp), %rdi
leaq 0x660(%rsp), %rsi
movl $0x4c4b40, %edx         # imm = 0x4C4B40
callq 0x30c0
jmp 0xbd9e
leaq 0xef(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x3500
movq 0x38(%rsp), %rdx
leaq 0xf0(%rsp), %rdi
leaq 0x660(%rsp), %rsi
callq 0x9280
jmp 0xbdcc
leaq 0x110(%rsp), %rdi
movq %rdi, 0x30(%rsp)
leaq 0xf0(%rsp), %rsi
movq %rsi, 0x28(%rsp)
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x3170
leaq 0xef(%rsp), %rdi
callq 0x32e0
movq 0x30(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x3100
jmp 0xbe16
movq 0x70(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x3100
jmp 0xbe2a
leaq 0xc8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0xab60
movl %eax, 0x24(%rsp)
jmp 0xbe4d
leaq 0xa8(%rsp), %rdi
callq 0x3170
leaq 0xc8(%rsp), %rdi
callq 0x3170
movl 0x24(%rsp), %eax
movl %eax, 0xe8(%rsp)
cmpl $0x0, 0xe8(%rsp)
jne 0xbf18
movl $0x4, 0x39c(%rsp)
jmp 0xc09b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xc0cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0xef(%rsp), %rdi
callq 0x32e0
jmp 0xc0cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
jmp 0xbf06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3170
leaq 0xc8(%rsp), %rdi
callq 0x3170
jmp 0xc0cc
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movq %rax, 0x18(%rsp)
leaq 0x130(%rsp), %rdi
callq 0xdea0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0xc07f
movl 0xa4(%rsp), %eax
movl %eax, %esi
leaq 0x130(%rsp), %rdi
callq 0xdf30
movq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x3100
jmp 0xbf75
leaq 0x80(%rsp), %rdi
callq 0xa740
movl %eax, 0x14(%rsp)
jmp 0xbf88
movl 0x14(%rsp), %edx
movq 0x350(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x440(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xa4(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
leaq 0x80(%rsp), %rdi
callq 0x3170
movq 0x350(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x440(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xa4(%rsp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jl 0xc005
movq 0x350(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x440(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0xa4(%rsp), %ecx
cmpl $0x3, (%rax,%rcx,4)
jle 0xc067
movslq 0x440(%rsp), %rdi
movl %edi, %esi
movl 0xa4(%rsp), %eax
movl %eax, %ecx
movl %ecx, %edx
movq 0x350(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%rdi,8), %rax
movl (%rax,%rcx,4), %ecx
leaq 0xd1a3(%rip), %rdi      # 0x191d7
xorl %eax, %eax
callq 0x3040
jmp 0xc03d
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x450(%rsp)
movl %eax, 0x44c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x3170
jmp 0xc0cc
jmp 0xc069
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xbf23
movl 0x440(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x440(%rsp)
movl $0x0, 0x39c(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x3170
leaq 0x130(%rsp), %rdi
callq 0xded0
movl 0x39c(%rsp), %eax
testl %eax, %eax
je 0xc0c7
jmp 0xc0c2
jmp 0xbcd9
jmp 0xbcd9
leaq 0x110(%rsp), %rdi
callq 0x3170
leaq 0x130(%rsp), %rdi
callq 0xded0
jmp 0xc147
leaq 0x148(%rsp), %rdi
callq 0x3070
jmp 0xc0f7
movq 0x12ec2(%rip), %rax     # 0x1efc0
movq (%rax), %rdi
callq 0x3310
jmp 0xc108
movq 0x350(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0x1, 0x39c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x3080
leaq 0x458(%rsp), %rdi
callq 0x3080
movq 0x8(%rsp), %rax
addq $0x4c51b8, %rsp         # imm = 0x4C51B8
retq
leaq 0x148(%rsp), %rdi
callq 0x3080
leaq 0x458(%rsp), %rdi
callq 0x3080
movq 0x450(%rsp), %rdi
callq 0x34f0
nop