// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movq %rdi, %r14
movq (%rdi), %rsi
leaq 0x368(%rsp), %r15
movq %r15, %rdi
movl $0x8, %edx
callq 0x2440
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x388(%rsp,%rax)
jne 0x7bd0
movq %r14, 0x110(%rsp)
leaq 0x6c4a(%rip), %rdi      # 0xe110
callq 0xb482
movq 0x368(%rsp), %rax
movq -0x18(%rax), %rcx
xorl %r13d, %r13d
testb $0x2, 0x388(%rsp,%rcx)
movl $0x0, 0x8(%rsp)
jne 0x783a
addq $-0x18, %rax
movl $0x1, 0x4(%rsp)
leaq 0x570(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0xd0(%rsp), %r14
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movl %r13d, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
xorl %edx, %edx
movq %rdx, 0x60(%rsp)
leaq 0x170(%rsp), %rcx
movq %rcx, 0x160(%rsp)
movq %rdx, 0x168(%rsp)
movb $0x0, 0x170(%rsp)
movq (%rax), %rdi
leaq 0x368(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0x4c4b40, %edx         # imm = 0x4C4B40
leaq 0x368(%rsp), %rdi
movq %r15, %rsi
callq 0x2460
movq %r14, %r13
leaq 0x20(%rsp), %r14
movq %r14, 0x10(%rsp)
movq %r15, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x570, %rdx            # imm = 0x570
movq %r12, %rdi
movq %r15, %rbp
movq %r15, %rsi
callq 0x61b8
leaq 0x160(%rsp), %rdi
movq %r12, %r15
movq %r12, %rsi
callq 0x22a0
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x75d8
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
cmpl $0x0, 0x4(%rsp)
movq %r13, %r14
je 0x76bf
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x160(%rsp), %rsi
movq 0x168(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x5fae
leaq 0xf0(%rsp), %r13
movq %r15, %r12
movq %rbp, %r15
leaq 0x100(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x5fae
leaq 0x50(%rsp), %rbp
leaq 0x90(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x698a
movl %eax, %r13d
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x7686
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x76ab
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2230
testl %r13d, %r13d
je 0x77ae
movl 0x8(%rsp), %esi
incl %esi
jmp 0x778b
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x160(%rsp), %rsi
movq 0x168(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x5fae
movq %r15, %r12
leaq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x5fae
movq %rbp, %r15
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x698a
movl %eax, %r13d
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x774e
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x7770
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
testl %r13d, %r13d
je 0x77e2
movl 0x8(%rsp), %esi
incl %esi
cmpl 0xc(%rsp), %r13d
jne 0x7bc0
movl 0xc(%rsp), %r13d
imull $0xcccccccd, %esi, %eax # imm = 0xCCCCCCCD
addl $0x19999998, %eax       # imm = 0x19999998
rorl $0x2, %eax
cmpl $0xccccccc, %eax        # imm = 0xCCCCCCC
movl %esi, 0x8(%rsp)
jbe 0x77bb
movl $0x0, 0x4(%rsp)
jmp 0x77ef
movl $0x1, 0x4(%rsp)
xorl %r13d, %r13d
jmp 0x77ef
movl $0x0, 0x4(%rsp)
leaq 0x6966(%rip), %rdi      # 0xe130
xorl %eax, %eax
callq 0x2040
movq 0xb7e0(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
jmp 0x77ef
movl $0x0, 0x4(%rsp)
movl 0xc(%rsp), %r13d
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x7814
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2230
movq %rbp, %rdi
callq 0x900c
movq 0x368(%rsp), %rax
movq -0x18(%rax), %rcx
addq $-0x18, %rax
testb $0x2, 0x388(%rsp,%rcx)
je 0x751c
leaq 0x368(%rsp), %rdi
callq 0x2050
movq 0xb76a(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
movl 0x8(%rsp), %edi
movl %r13d, %esi
callq 0xa698
movq %rax, %r15
movq 0x110(%rsp), %rax
movq (%rax), %rsi
leaq 0x160(%rsp), %rdi
movl $0x8, %edx
callq 0x2440
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rcx
testb $0x2, 0x180(%rsp,%rcx)
leaq 0x160(%rsp), %r14
jne 0x7b57
leaq 0x150(%rsp), %r13
leaq 0x140(%rsp), %rbp
movl $0x0, 0x4(%rsp)
movq %rbx, 0x118(%rsp)
imull $0xcccccccd, 0x4(%rsp), %ecx # imm = 0xCCCCCCCD
addl $0x19999999, %ecx       # imm = 0x19999999
cmpl $0x33333332, %ecx       # imm = 0x33333332
ja 0x790e
movl (%r15), %edx
leaq 0x6897(%rip), %rdi      # 0xe17b
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x2040
movq 0xb6c2(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
leaq 0x160(%rsp), %r14
movq 0x160(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
xorl %edx, %edx
movq %rdx, 0x40(%rsp)
leaq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2200
leaq 0x570(%rsp), %r12
movsbl %al, %ecx
movl $0x4c4b40, %edx         # imm = 0x4C4B40
movq %r14, %rdi
movq %r12, %rsi
callq 0x2460
leaq 0x60(%rsp), %r14
movq %r14, 0x50(%rsp)
movq %r12, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x570, %rdx            # imm = 0x570
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x61b8
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x22a0
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
leaq 0x130(%rsp), %r12
je 0x79b6
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2230
movq %r12, 0x120(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x120(%rsp), %rdi
callq 0x5fae
movq 0x118(%rsp), %rbx
leaq 0xb0(%rsp), %r14
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x5fae
leaq 0x120(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %r14, %rdx
callq 0x698a
movl %eax, %r14d
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7a47
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x120(%rsp), %rdi
cmpq %r12, %rdi
je 0x7a64
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x2230
testl %r14d, %r14d
je 0x7b0f
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x7b0b
movslq 0x4(%rsp), %rbx
xorl %r14d, %r14d
movq %r14, %rcx
shlq $0x5, %rcx
movq %r13, 0x140(%rsp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x5fae
movq %rbp, %rdi
callq 0x662f
movq 0x8(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, (%rcx,%r14,4)
movq 0x140(%rsp), %rdi
cmpq %r13, %rdi
je 0x7ae5
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax,%r14,4), %eax
cmpl $0x4, %eax
jae 0x7ba2
incl %r14d
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %r14, %rcx
ja 0x7a85
incl 0x4(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7b2b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x30(%rsp), %rdi
callq 0x900c
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rcx
testb $0x2, 0x180(%rsp,%rcx)
leaq 0x160(%rsp), %r14
je 0x78c4
leaq 0x160(%rsp), %rdi
callq 0x2050
movq 0xb44d(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
callq 0x2260
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 0x661e(%rip), %rdi      # 0xe1c7
movl 0x4(%rsp), %esi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x2040
xorl %edi, %edi
callq 0x2290
leaq 0x6481(%rip), %rdi      # 0xe048
movl 0xc(%rsp), %edx
movl %r13d, %ecx
jmp 0x7bb2
movq (%r14), %rsi
leaq 0x650c(%rip), %rdi      # 0xe0e6
xorl %eax, %eax
callq 0x2040
jmp 0x7bb9
movq %rax, %rbx
jmp 0x7d68
jmp 0x7bef
jmp 0x7bef
movq %rax, %rbx
jmp 0x7d75
movq %rax, %rbx
jmp 0x7c29
jmp 0x7ce9
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x7c29
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x7cec
movq 0x80(%rsp), %rsi
jmp 0x7c93
movq %rax, %rbx
jmp 0x7c76
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x7c76
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x7cec
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x7cec
jmp 0x7d1d
jmp 0x7d1d
movq %rax, %rbx
jmp 0x7cce
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7cce
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x120(%rsp), %rdi
cmpq %r12, %rdi
je 0x7d42
movq 0x130(%rsp), %rsi
jmp 0x7d3a
jmp 0x7ce9
jmp 0x7d1d
movq %rax, %rbx
movq 0x160(%rsp), %rdi
leaq 0x170(%rsp), %rax
cmpq %rax, %rdi
je 0x7d11
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x50(%rsp), %rdi
callq 0x900c
jmp 0x7d75
movq %rax, %rbx
jmp 0x7d42
movq %rax, %rbx
movq 0x140(%rsp), %rdi
cmpq %r13, %rdi
je 0x7d42
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7d5e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x30(%rsp), %rdi
callq 0x900c
leaq 0x160(%rsp), %rdi
callq 0x2070
leaq 0x368(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2400