// File: GenisGE[P]evalAdmix/filereader_and_conversions.cpp
// Function: doBimFile(myPars*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bArray *doBimFile(myPars* pars,const char *filename,const std::string delim){
   ///@param filename A filename to read.@param delim A string of delimiters.
  
  std::vector<int> chromos;//for all lines
  std::vector<double> positions;//for all lines
  

  const int SIZE=500000;//this should only be 6 elements but lets make it big..
  char buffer[SIZE];
 
  std::ifstream pFile (filename,std::ios::in);
 
  
  if(!pFile){
    fileError(filename);
    exit(0);
  }

  
  std::string tmp_string;
  int itemsInRow;
  int numRows =0;
  while(!pFile.eof()){
    pFile.getline(buffer,SIZE);
    tmp_string = std::string(buffer);
    std::vector<std::string> tokens;
    itemsInRow = get_lexemes(tmp_string,tokens,delim);
      
    if (itemsInRow!=6 && itemsInRow!=0){
      printf("plink bim file:%s doesn't have 6 columns in row:%d\n",filename,numRows);
      exit(0);
    }else if(itemsInRow==0)
      break;
    chromos.push_back(atoi((tokens[0]).c_str()));
    positions.push_back(atof((tokens[3]).c_str()));
    numRows++;
  }

  int warn=0;
  //  copy(chromos.begin(), chromos.end(), std::ostream_iterator<int>(std::cout, ", "));  
  //  copy(positions.begin(), positions.end(), std::ostream_iterator<float>(std::cout, ", "));  
  bArray *ret = allocBoolArray(chromos.size());
  int numTrue = 0;
  for(unsigned int i=0;i<chromos.size();i++){
    ret->array[i] = 1;
    numTrue++;
 

  }
  ret->numTrue = numTrue;

  dArray *pos = allocDoubleArray(ret->numTrue);
  iArray *chr = allocIntArray(ret->numTrue);
  int atPos=0;
  for(int i=0;i<ret->x;i++){
    if(ret->array[i]){
      pos->array[atPos] = positions[i]/PLINK_POS_SCALING;
      chr->array[atPos] = chromos[i];
      
      atPos++;
    }

  }
  pars->chr  = chr;
  pars->position= pos;




  return ret;
}

// Assembly:
subq $0x7a518, %rsp          # imm = 0x7A518
movq %rdx, 0x70(%rsp)
movq %rdi, 0x7a510(%rsp)
movq %rsi, 0x7a508(%rsp)
movq %rdx, 0x7a500(%rsp)
leaq 0x7a4e8(%rsp), %rdi
callq 0xe030
leaq 0x7a4d0(%rsp), %rdi
callq 0xe040
movl $0x7a120, 0x7a4cc(%rsp) # imm = 0x7A120
movq 0x7a508(%rsp), %rsi
leaq 0x198(%rsp), %rdi
movl $0x8, %edx
callq 0x3540
jmp 0xd245
movq 0x198(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x198(%rsp,%rax), %rdi
callq 0x3120
movb %al, 0x6f(%rsp)
jmp 0xd264
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0xd271
jmp 0xd354
movq 0x7a508(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x167(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x3500
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
leaq 0x168(%rsp), %rdi
callq 0x9280
jmp 0xd2a9
leaq 0x168(%rsp), %rdi
callq 0xa100
jmp 0xd2b8
leaq 0x168(%rsp), %rdi
callq 0x3170
leaq 0x167(%rsp), %rdi
callq 0x32e0
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd8c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd8b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd342
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x3170
leaq 0x167(%rsp), %rdi
callq 0x32e0
jmp 0xd8b4
leaq 0x140(%rsp), %rdi
callq 0x33a0
movl $0x0, 0x138(%rsp)
movq 0x198(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x198(%rsp,%rax), %rdi
callq 0x3550
movb %al, 0x57(%rsp)
jmp 0xd38b
movb 0x57(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xd39a
jmp 0xd666
leaq 0x198(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
movl $0x7a120, %edx          # imm = 0x7A120
callq 0x30c0
jmp 0xd3b6
leaq 0x117(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x3500
movq 0x48(%rsp), %rdx
leaq 0x118(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x9280
jmp 0xd3e4
leaq 0x140(%rsp), %rdi
movq %rdi, 0x40(%rsp)
leaq 0x118(%rsp), %rsi
movq %rsi, 0x38(%rsp)
callq 0x3350
movq 0x38(%rsp), %rdi
callq 0x3170
leaq 0x117(%rsp), %rdi
callq 0x32e0
leaq 0xf8(%rsp), %rdi
callq 0xdec0
movq 0x40(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x3100
jmp 0xd43b
movq 0x70(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3100
jmp 0xd44f
leaq 0xd8(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xab60
movl %eax, 0x34(%rsp)
jmp 0xd472
movl 0x34(%rsp), %eax
movl %eax, 0x13c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3170
leaq 0xd8(%rsp), %rdi
callq 0x3170
cmpl $0x6, 0x13c(%rsp)
je 0xd57a
cmpl $0x0, 0x13c(%rsp)
je 0xd57a
movq 0x7a508(%rsp), %rsi
movl 0x138(%rsp), %edx
leaq 0xbe23(%rip), %rdi      # 0x192ec
xorl %eax, %eax
callq 0x3040
jmp 0xd4d2
xorl %edi, %edi
callq 0x3340
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd8a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
leaq 0x117(%rsp), %rdi
callq 0x32e0
jmp 0xd8a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd654
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xd568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3170
leaq 0xd8(%rsp), %rdi
callq 0x3170
jmp 0xd654
cmpl $0x0, 0x13c(%rsp)
jne 0xd594
movl $0x3, 0xb4(%rsp)
jmp 0xd633
jmp 0xd596
xorl %eax, %eax
movl %eax, %esi
leaq 0xf8(%rsp), %rdi
callq 0xdf30
movq %rax, %rdi
callq 0x30f0
movq %rax, %rdi
callq 0x3460
movl %eax, 0xb0(%rsp)
leaq 0x7a4e8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe050
jmp 0xd5d5
leaq 0xf8(%rsp), %rdi
movl $0x3, %esi
callq 0xdf30
movq %rax, %rdi
callq 0x30f0
movq %rax, %rdi
callq 0x3370
movsd %xmm0, 0xa8(%rsp)
leaq 0x7a4d0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xe080
jmp 0xd617
movl 0x138(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
movl $0x0, 0xb4(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xded0
movl 0xb4(%rsp), %eax
testl %eax, %eax
je 0xd64f
jmp 0xd64d
jmp 0xd666
jmp 0xd36c
leaq 0xf8(%rsp), %rdi
callq 0xded0
jmp 0xd8a7
movl $0x0, 0xa4(%rsp)
leaq 0x7a4e8(%rsp), %rdi
callq 0xe0b0
movl %eax, %edi
callq 0x14900
movq %rax, 0x28(%rsp)
jmp 0xd68c
movq 0x28(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x0, 0x90(%rsp)
movl 0x90(%rsp), %eax
movq %rax, 0x20(%rsp)
leaq 0x7a4e8(%rsp), %rdi
callq 0xe0b0
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0xd713
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x90(%rsp), %ecx
movl $0x1, (%rax,%rcx,4)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0xd6af
movl 0x94(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x98(%rsp), %rax
movl 0x4(%rax), %edi
callq 0x14a30
movq %rax, 0x18(%rsp)
jmp 0xd73c
movq 0x18(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x98(%rsp), %rax
movslq 0x4(%rax), %rdi
callq 0x14740
movq %rax, 0x10(%rsp)
jmp 0xd761
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movq 0x98(%rsp), %rcx
cmpl (%rcx), %eax
jge 0xd826
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x78(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0xd814
movslq 0x78(%rsp), %rsi
leaq 0x7a4d0(%rsp), %rdi
callq 0xe0d0
movsd (%rax), %xmm0
movsd 0xb751(%rip), %xmm1     # 0x18f18
divsd %xmm1, %xmm0
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x7c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movslq 0x78(%rsp), %rsi
leaq 0x7a4e8(%rsp), %rdi
callq 0xe0f0
movl (%rax), %edx
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x7c(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xd816
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xd77e
movq 0x80(%rsp), %rcx
movq 0x7a510(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x88(%rsp), %rcx
movq 0x7a510(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0x1, 0xb4(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x3170
leaq 0x198(%rsp), %rdi
callq 0x3080
leaq 0x7a4d0(%rsp), %rdi
callq 0xe110
leaq 0x7a4e8(%rsp), %rdi
callq 0xe170
movq 0x8(%rsp), %rax
addq $0x7a518, %rsp          # imm = 0x7A518
retq
leaq 0x140(%rsp), %rdi
callq 0x3170
leaq 0x198(%rsp), %rdi
callq 0x3080
leaq 0x7a4d0(%rsp), %rdi
callq 0xe110
leaq 0x7a4e8(%rsp), %rdi
callq 0xe170
movq 0x190(%rsp), %rdi
callq 0x34f0
nopl (%rax,%rax)