// File: /workspace/github_cpp_makefile_star2_O1/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 $0x4c50c8, %rsp         # imm = 0x4C50C8
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %rsi
leaq 0x378(%rsp), %r15
movq %r15, %rdi
movl $0x8, %edx
callq 0x2440
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x398(%rsp,%rax)
jne 0x7b35
movq %rbx, 0x30(%rsp)
movq %r14, 0x148(%rsp)
leaq 0x6cbe(%rip), %rdi      # 0xe110
callq 0xb282
movq 0x378(%rsp), %rax
movq -0x18(%rax), %rcx
xorl %ebx, %ebx
testb $0x2, 0x398(%rsp,%rcx)
movl $0x0, %ebp
jne 0x77aa
addq $-0x18, %rax
movl $0x1, %r14d
leaq 0x108(%rsp), %r15
leaq 0x60(%rsp), %r12
xorl %ebp, %ebp
xorl %ebx, %ebx
movl %r14d, 0xc(%rsp)
movl %ebx, 0x3c(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
xorl %edx, %edx
movq %rdx, 0x70(%rsp)
leaq 0x180(%rsp), %rcx
movq %rcx, 0x170(%rsp)
movq %rdx, 0x178(%rsp)
movb $0x0, 0x180(%rsp)
movq (%rax), %rdi
leaq 0x378(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0x4c4b40, %edx         # imm = 0x4C4B40
leaq 0x378(%rsp), %rdi
leaq 0x580(%rsp), %rsi
callq 0x2460
movq %r12, %rbx
movq %r15, %r13
movl %ebp, %r15d
leaq 0x20(%rsp), %r14
movq %r14, 0x10(%rsp)
leaq 0x580(%rsp), %rbp
movq %rbp, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x580, %rdx            # imm = 0x580
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x618e
leaq 0x170(%rsp), %rdi
movq %r12, %rsi
callq 0x22a0
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x7565
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
cmpl $0x0, 0xc(%rsp)
movl %r15d, %ebp
movq %rbx, %r12
je 0x7646
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x170(%rsp), %rsi
movq 0x178(%rsp), %rdx
addq %rsi, %rdx
leaq 0xa8(%rsp), %rdi
callq 0x5f92
leaq 0x128(%rsp), %rbx
movq %r13, %r15
leaq 0x138(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x5f92
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x694a
movl %eax, %ebx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x7612
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x7637
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2230
testl %ebx, %ebx
je 0x7733
incl %ebp
jmp 0x7719
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x170(%rsp), %rsi
movq 0x178(%rsp), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x5f92
movq %r13, %r15
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x5f92
movl 0x3c(%rsp), %ebx
leaq 0x88(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x694a
movl %eax, %r13d
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x76df
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x7704
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2230
testl %r13d, %r13d
je 0x7714
incl %ebp
cmpl %ebx, %r13d
jne 0x7b25
testl %r13d, %r13d
je 0x772e
imull $0xcccccccd, %ebp, %eax # imm = 0xCCCCCCCD
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x2, %eax
cmpl $0xccccccc, %eax        # imm = 0xCCCCCCC
jbe 0x773d
xorl %r14d, %r14d
jmp 0x775f
movl $0x1, %r14d
xorl %ebx, %ebx
jmp 0x775f
xorl %r14d, %r14d
leaq 0x69e9(%rip), %rdi      # 0xe130
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0xb861(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
movq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x7784
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x2230
movq %r12, %rdi
callq 0x8f4c
movq 0x378(%rsp), %rax
movq -0x18(%rax), %rcx
addq $-0x18, %rax
testb $0x2, 0x398(%rsp,%rcx)
je 0x7493
leaq 0x378(%rsp), %rdi
callq 0x2050
movq 0xb7fa(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
movl %ebp, %edi
movl %ebx, %esi
callq 0xa5cc
movq %rax, %r15
movq 0x148(%rsp), %rax
movq (%rax), %rsi
leaq 0x170(%rsp), %rdi
movl $0x8, %edx
callq 0x2440
movq 0x170(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x190(%rsp,%rax)
jne 0x7abe
leaq 0x160(%rsp), %r13
leaq 0x580(%rsp), %r14
leaq 0x60(%rsp), %rbp
leaq 0x150(%rsp), %r12
movl $0x0, 0xc(%rsp)
imull $0xcccccccd, 0xc(%rsp), %eax # imm = 0xCCCCCCCD
addl $0x19999999, %eax       # imm = 0x19999999
cmpl $0x33333332, %eax       # imm = 0x33333332
ja 0x7866
movl (%r15), %edx
leaq 0x692f(%rip), %rdi      # 0xe17b
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x2040
movq 0xb75a(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x50(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq 0x170(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0x170(%rsp), %rbx
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0x4c4b40, %edx         # imm = 0x4C4B40
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2460
movq %r14, %rbx
leaq 0x70(%rsp), %r14
movq %r14, 0x60(%rsp)
movq %rbx, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x580, %rdx            # imm = 0x580
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x618e
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x22a0
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x790c
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0xf8(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5f92
leaq 0xc8(%rsp), %r14
movq 0x30(%rsp), %rbp
leaq 0xd8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x5f92
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x694a
movl %eax, %r14d
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x79a1
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x79c6
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2230
testl %r14d, %r14d
je 0x7a71
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0x7a6d
movslq 0xc(%rsp), %rbx
xorl %r14d, %r14d
movq %r14, %rcx
shlq $0x5, %rcx
movq %r13, 0x150(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x5f92
movq %r12, %rdi
callq 0x65f5
movq 0x8(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, (%rcx,%r14,4)
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x7a3b
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax,%r14,4), %ecx
cmpl $0x4, %ecx
jae 0x7b09
incl %r14d
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %r14, %rcx
ja 0x79e7
incl 0xc(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
leaq 0x580(%rsp), %r14
je 0x7a95
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x40(%rsp), %rdi
callq 0x8f4c
movq 0x170(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x190(%rsp,%rax)
leaq 0x60(%rsp), %rbp
je 0x782e
leaq 0x170(%rsp), %rdi
callq 0x2050
movq 0xb4e6(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
leaq 0x170(%rsp), %rdi
callq 0x2070
leaq 0x378(%rsp), %rdi
callq 0x2070
movq %r15, %rax
addq $0x4c50c8, %rsp         # imm = 0x4C50C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66b7(%rip), %rdi      # 0xe1c7
movl 0xc(%rsp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2290
leaq 0x651c(%rip), %rdi      # 0xe048
movl %ebp, %esi
movl %ebx, %edx
movl %r13d, %ecx
jmp 0x7b17
movq (%r14), %rsi
leaq 0x65a7(%rip), %rdi      # 0xe0e6
xorl %eax, %eax
callq 0x2040
jmp 0x7b1e
movq %rax, %rbx
jmp 0x7cde
jmp 0x7b54
jmp 0x7b54
movq %rax, %rbx
jmp 0x7ceb
movq %rax, %rbx
jmp 0x7b8e
jmp 0x7c5f
movq %rax, %rbx
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x7b8e
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x7c62
movq 0x98(%rsp), %rsi
jmp 0x7bfb
movq %rax, %rbx
jmp 0x7bde
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x7bde
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x7c62
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x7c62
jmp 0x7c93
jmp 0x7c93
movq %rax, %rbx
jmp 0x7c3c
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x7c3c
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x7cb8
movq 0xf8(%rsp), %rsi
jmp 0x7cb0
jmp 0x7c5f
jmp 0x7c93
movq %rax, %rbx
movq 0x170(%rsp), %rdi
leaq 0x180(%rsp), %rax
cmpq %rax, %rdi
je 0x7c87
movq 0x180(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x60(%rsp), %rdi
callq 0x8f4c
jmp 0x7ceb
movq %rax, %rbx
jmp 0x7cb8
movq %rax, %rbx
movq 0x150(%rsp), %rdi
cmpq %r13, %rdi
je 0x7cb8
movq 0x160(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7cd4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x40(%rsp), %rdi
callq 0x8f4c
leaq 0x170(%rsp), %rdi
callq 0x2070
leaq 0x378(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2400