// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c50b8, %rsp         # imm = 0x4C50B8
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq (%rdi), %rsi
leaq 0x368(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x2450
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x388(%rsp,%rax)
jne 0x6a9c
movq %r14, 0x58(%rsp)
leaq 0x5a73(%rip), %rdi      # 0xc0d0
callq 0x9c5e
pushq $0x1
popq %r13
xorl %ebx, %ebx
leaq 0x60(%rsp), %r12
leaq 0x140(%rsp), %r14
xorl %ebp, %ebp
movq 0x368(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x388(%rsp,%rax)
jne 0x682a
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x170(%rsp), %rax
movq %rax, 0x160(%rsp)
andq $0x0, 0x168(%rsp)
movb $0x0, 0x170(%rsp)
movl $0x4c4b40, %edx         # imm = 0x4C4B40
leaq 0x368(%rsp), %rdi
leaq 0x570(%rsp), %rsi
callq 0x20b0
movq %r14, %r15
movq %r12, %rdi
leaq 0x570(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x5646
leaq 0x160(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2290
movq %r12, %rdi
callq 0x20f0
testl %r13d, %r13d
je 0x6763
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %r14
callq 0x22f0
leaq 0x120(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x22f0
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq %r15, %rdx
callq 0x5f0c
movl %eax, %ebx
movq %r15, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x20f0
testl %ebx, %ebx
je 0x67fd
incl %ebp
jmp 0x67cd
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x22f0
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x22f0
movl %ebx, (%rsp)
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
movq %r14, %rdx
callq 0x5f0c
movl %eax, %r13d
movq %r14, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x20f0
testl %r13d, %r13d
je 0x6805
incl %ebp
cmpl (%rsp), %r13d
jne 0x6a8b
movq %r15, %r14
movl (%rsp), %ebx
movl %ebp, %eax
cltd
pushq $0x14
popq %rcx
idivl %ecx
xorl %r13d, %r13d
testl %edx, %edx
jne 0x680e
leaq 0x590d(%rip), %rdi      # 0xc0f0
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0xa7cd(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2250
jmp 0x680e
xorl %ebx, %ebx
pushq $0x1
popq %r13
jmp 0x680e
xorl %r13d, %r13d
movl (%rsp), %ebx
movq %r15, %r14
leaq 0x160(%rsp), %rdi
callq 0x20f0
leaq 0x30(%rsp), %rdi
callq 0x7976
jmp 0x6677
leaq 0x368(%rsp), %rdi
callq 0x2050
movq 0xa782(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2250
movl %ebp, %edi
movl %ebx, %esi
callq 0x90f4
movq %rax, %r15
movq 0x58(%rsp), %rax
movq (%rax), %rsi
leaq 0x160(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2450
leaq 0x570(%rsp), %r14
leaq 0x30(%rsp), %rbx
leaq 0xc0(%rsp), %r13
xorl %ebp, %ebp
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x180(%rsp,%rax)
jne 0x6a25
movl %ebp, %eax
cltd
pushq $0x5
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0x68c9
movl (%r15), %edx
leaq 0x588a(%rip), %rdi      # 0xc13b
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0xa6ff(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2250
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
andq $0x0, 0x68(%rsp)
movb $0x0, 0x70(%rsp)
movl $0x4c4b40, %edx         # imm = 0x4C4B40
leaq 0x160(%rsp), %rdi
movq %r14, %rsi
callq 0x20b0
movl %ebp, (%rsp)
movq %rbx, %rdi
movq %rbx, %rbp
movq %r14, %r12
movq %r14, %rsi
leaq 0x7(%rsp), %rdx
callq 0x5646
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2290
movq %rbp, %rbx
movq %rbp, %rdi
callq 0x20f0
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x22f0
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x22f0
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x5f0c
movq %r14, %rdi
movl %eax, %r14d
callq 0x20f0
movq %rbp, %rdi
callq 0x20f0
testl %r14d, %r14d
je 0x6a06
movslq (%rsp), %rbx
xorl %r14d, %r14d
movl %r14d, %r14d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x69f2
movq %r14, %rax
shlq $0x5, %rax
addq %rax, %rsi
movq %r13, %rdi
callq 0x22f0
movq %r13, %rdi
callq 0x5c75
movq 0x8(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, (%rcx,%r14,4)
movq %r13, %rdi
callq 0x20f0
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax,%r14,4), %ecx
cmpl $0x4, %ecx
jae 0x6a70
incl %r14d
jmp 0x6991
movl (%rsp), %ebp
incl %ebp
leaq 0x570(%rsp), %r14
leaq 0x30(%rsp), %rbx
jmp 0x6a0c
movl (%rsp), %ebp
movq %r12, %r14
leaq 0x60(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x7976
jmp 0x6881
leaq 0x160(%rsp), %rdi
callq 0x2050
movq 0xa587(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x2250
leaq 0x160(%rsp), %rdi
callq 0x2070
leaq 0x368(%rsp), %rdi
callq 0x2070
movq %r15, %rax
addq $0x4c50b8, %rsp         # imm = 0x4C50B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5710(%rip), %rdi      # 0xc187
movl (%rsp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2280
leaq 0x5576(%rip), %rdi      # 0xc008
movl %ebp, %esi
movl (%rsp), %edx
movl %r13d, %ecx
jmp 0x6a7d
movq (%r14), %rsi
leaq 0x5600(%rip), %rdi      # 0xc0a6
xorl %eax, %eax
callq 0x2040
jmp 0x6a84
movq %rax, %rbx
jmp 0x6b74
jmp 0x6abb
jmp 0x6abb
movq %rax, %rbx
jmp 0x6b81
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x20f0
jmp 0x6ad8
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x6b01
jmp 0x6b2f
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x20f0
jmp 0x6af9
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x20f0
jmp 0x6b32
jmp 0x6b5d
jmp 0x6b5d
movq %rax, %rbx
jmp 0x6b21
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x20f0
leaq 0x80(%rsp), %rdi
jmp 0x6b56
jmp 0x6b5d
jmp 0x6b2f
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x20f0
leaq 0x30(%rsp), %rdi
callq 0x7976
jmp 0x6b81
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x20f0
jmp 0x6b60
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x7976
leaq 0x160(%rsp), %rdi
callq 0x2070
leaq 0x368(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2410