// File: GenisGE[P]evalAdmix/asort.cpp
// Function: mysort(myPars*, int)
void mysort(myPars *pars,int print_info){
  try{
    if(print_info)
      flush_print("Will sort input data...                        ");
    
    std::vector<dats> data;
    // we know the endsize of the vector so lets make it this size to avoid nasty resizes
    data.reserve(pars->data->y); 
    for(int i=0;i<pars->data->y;i++){
      dats tmp = {pars->chr->array[i],pars->position->array[i],i};
      data.push_back(tmp);
    }
    //sort data
    std::sort(data.begin(),data.end(),comp);
    if(print_info>2){
      fprintf(stderr,"\n\t-> Will check uniqueness of positions:");
      
    }  
    //check for uniqueness of positions per chromosome
    for(unsigned int i=0;i<data.size()-1;i++)
      if(data[i].pos==data[i+1].pos){
	fprintf(stderr,"\t-> Non unique position: chromosome:%d position:%fMb\n ",data[i].chr,data[i+1].pos);
      }
    
    
    dArray *newPos = allocDoubleArray(pars->position->x);
    iArray *newChr = allocIntArray(pars->chr->x);
    usiMatrix *newData = allocUSIntMatrix(pars->data->x,pars->data->y);
    for(unsigned int i=0;i<data.size();i++) { 
      dats v= data[i];
      newPos->array[i] = v.pos;
      newChr->array[i] = v.chr;
      for(int s =0;s<newData->x;s++)
	newData->matrix[s][i] = pars->data->matrix[s][v.id];
      //fprintf(stderr,"chr:%d \tpos:%f \t id:%d \n",v.chr,v.pos,v.id);
    }
    killArray(pars->chr);
    killMatrix(pars->data);
    killArray(pars->position);
    
    pars->data=newData;
    pars->position=newPos;
    pars->chr=newChr;
    if(print_info)
      flush_print("\r\t-> Done sorting...                                                                 \n");
  } catch(std::exception & e){
    fprintf(stderr,"\n\t-> Allocation failed, likely cause: need more memory, will exit.\n");
    exit(0);
  } 
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movl %esi, 0xfc(%rsp)
cmpl $0x0, 0xfc(%rsp)
je 0x112b9
leaq 0x8105(%rip), %rdi      # 0x1939c
callq 0x152a0
jmp 0x1129e
jmp 0x112b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0x113b2
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x11790
movq 0x48(%rsp), %rdi
movq 0x100(%rsp), %rax
movq (%rax), %rax
movslq 0x4(%rax), %rsi
callq 0x117a0
jmp 0x112e6
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movq 0x100(%rsp), %rcx
movq (%rcx), %rcx
cmpl 0x4(%rcx), %eax
jge 0x113f7
movq 0x100(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movslq 0xcc(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xb0(%rsp)
movq 0x100(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq 0xcc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0xb8(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0xc0(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x118b0
jmp 0x11379
jmp 0x1137b
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x112f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x11a10
movl 0xec(%rsp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1177c
movq 0xf0(%rsp), %rdi
callq 0x30e0
movq %rax, 0x50(%rsp)
movq 0xdbf9(%rip), %rax      # 0x1efd8
movq (%rax), %rdi
leaq 0x809a(%rip), %rsi      # 0x19483
movb $0x0, %al
callq 0x33c0
xorl %edi, %edi
callq 0x3340
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x11970
movq 0x40(%rsp), %rdi
movq %rax, 0xa8(%rsp)
callq 0x119a0
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
leaq -0x25a(%rip), %rdx      # 0x111e0
callq 0x11920
jmp 0x11441
cmpl $0x2, 0xfc(%rsp)
jle 0x11463
movq 0xdb86(%rip), %rax      # 0x1efd8
movq (%rax), %rdi
leaq 0x7f70(%rip), %rsi      # 0x193cc
movb $0x0, %al
callq 0x33c0
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
movq %rax, 0x38(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x119d0
movq %rax, %rcx
movq 0x38(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x11561
movl 0x9c(%rsp), %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x119f0
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x119f0
movsd 0x30(%rsp), %xmm0
ucomisd 0x8(%rax), %xmm0
jne 0x11549
jp 0x11549
movq 0xdaea(%rip), %rax      # 0x1efd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x119f0
movl (%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x119f0
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %edx
movsd 0x8(%rax), %xmm0
leaq 0x7eb3(%rip), %rsi      # 0x193f5
movb $0x1, %al
callq 0x33c0
jmp 0x1154b
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x1146e
movq 0x100(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edi
callq 0x14a30
movq %rax, 0x18(%rsp)
jmp 0x1157b
movq 0x18(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x100(%rsp), %rax
movq 0x8(%rax), %rax
movslq (%rax), %rdi
callq 0x14740
movq %rax, 0x10(%rsp)
jmp 0x115a3
movq 0x10(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x100(%rsp), %rax
movq (%rax), %rax
movl (%rax), %edi
movl 0x4(%rax), %esi
callq 0x13e70
movq %rax, 0x8(%rsp)
jmp 0x115cc
movq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movq %rax, (%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x119d0
movq %rax, %rcx
movq (%rsp), %rax
cmpq %rcx, %rax
jae 0x116da
movl 0x7c(%rsp), %eax
movl %eax, %esi
leaq 0xd0(%rsp), %rdi
callq 0x119f0
movq (%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x70(%rsp)
movsd 0x68(%rsp), %xmm0
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x7c(%rsp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movl 0x60(%rsp), %edx
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x7c(%rsp), %ecx
movl %edx, (%rax,%rcx,4)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movq 0x80(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x116c8
movq 0x100(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movslq 0x5c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x70(%rsp), %rcx
movw (%rax,%rcx,2), %dx
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x5c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x7c(%rsp), %ecx
movw %dx, (%rax,%rcx,2)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1166d
jmp 0x116ca
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x115e1
movq 0x100(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x14130
jmp 0x116ed
movq 0x100(%rsp), %rax
movq (%rax), %rdi
callq 0x142f0
jmp 0x116ff
movq 0x100(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x14090
jmp 0x11712
movq 0x80(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, (%rax)
movq 0x90(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x88(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, 0x8(%rax)
cmpl $0x0, 0xfc(%rsp)
je 0x11767
leaq 0x7cce(%rip), %rdi      # 0x1942c
callq 0x152a0
jmp 0x11765
jmp 0x11767
leaq 0xd0(%rsp), %rdi
callq 0x11a10
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xf0(%rsp), %rdi
callq 0x34f0
nopl (%rax)