// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebx
movq %rdi, %r14
testl %esi, %esi
je 0x84cd
leaq 0x3eae(%rip), %rdi      # 0xc376
callq 0x9c5e
movq %rsp, %rdi
andq $0x0, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq (%r14), %rax
movslq 0x4(%rax), %rsi
callq 0x872c
xorl %r13d, %r13d
movq %rsp, %r15
leaq 0x20(%rsp), %r12
movq (%r14), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r13
jge 0x8539
movq 0x8(%r14), %rax
movq 0x8(%rax), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x20(%rsp)
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movl %r13d, 0x30(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x87d8
incq %r13
jmp 0x84f4
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
leaq -0xc9(%rip), %rdx       # 0x8480
callq 0x89a3
cmpl $0x3, %ebx
jge 0x86b7
pushq $0x1
popq %r12
pushq $0x18
popq %rbp
movq 0x8a73(%rip), %r13      # 0x10fd8
leaq 0x3e63(%rip), %r15      # 0xc3cf
leal -0x1(%r12), %esi
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
cqto
idivq %rbp
decq %rax
cmpq %rsi, %rax
jbe 0x85c3
imulq $0x18, %rsi, %rax
movsd 0x8(%rcx,%rax), %xmm1
movl %r12d, %edx
imulq $0x18, %rdx, %rdx
movsd 0x8(%rcx,%rdx), %xmm0
ucomisd %xmm0, %xmm1
jne 0x85a9
jnp 0x85ae
incl %r12d
jmp 0x856c
addq %rax, %rcx
movq (%r13), %rdi
movl (%rcx), %edx
movq %r15, %rsi
movb $0x1, %al
callq 0x2320
jmp 0x85a9
movq 0x10(%r14), %rax
movl (%rax), %edi
callq 0x97cc
movq %rax, %r15
movq 0x8(%r14), %rax
movslq (%rax), %rdi
callq 0x967f
movq %rax, %r12
movq (%r14), %rax
movl (%rax), %edi
movl 0x4(%rax), %esi
callq 0x9238
movq %rax, %r13
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
cqto
idivq %rbp
xorl %edx, %edx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rax
jbe 0x866b
imulq $0x18, %rsi, %rdi
movl (%rcx,%rdi), %r8d
movsd 0x8(%rcx,%rdi), %xmm0
movslq 0x10(%rcx,%rdi), %rdi
movq 0x8(%r15), %r9
movsd %xmm0, (%r9,%rsi,8)
movq 0x8(%r12), %r9
movl %r8d, (%r9,%rsi,4)
movl (%r13), %r8d
testl %r8d, %r8d
cmovlel %edx, %r8d
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x8667
movq (%r14), %r10
movq 0x8(%r10), %r10
movq (%r10,%r9,8), %r10
movzwl (%r10,%rdi,2), %r10d
movq 0x8(%r13), %r11
movq (%r11,%r9,8), %r11
movw %r10w, (%r11,%rsi,2)
incq %r9
jmp 0x8640
incl %esi
jmp 0x8605
movq 0x8(%r14), %rdi
callq 0x93b4
movq (%r14), %rdi
callq 0x9452
movq 0x10(%r14), %rdi
callq 0x9378
movq %r13, (%r14)
movq %r15, 0x10(%r14)
movq %r12, 0x8(%r14)
testl %ebx, %ebx
je 0x86a0
leaq 0x3d6b(%rip), %rdi      # 0xc406
callq 0x9c5e
movq %rsp, %rdi
callq 0x87fe
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x891a(%rip), %rax      # 0x10fd8
movq (%rax), %rcx
leaq 0x3cde(%rip), %rdi      # 0xc3a6
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x2470
jmp 0x8557
movq %rdx, %r14
movq %rax, %rbx
jmp 0x86f6
jmp 0x86e8
jmp 0x86e8
jmp 0x86e8
jmp 0x86e8
movq %rdx, %r14
movq %rax, %rbx
movq %rsp, %rdi
callq 0x87fe
movq %rbx, %rdi
cmpl $0x1, %r14d
jne 0x8727
callq 0x20d0
movq 0x88cd(%rip), %rax      # 0x10fd8
movq (%rax), %rcx
leaq 0x3d48(%rip), %rdi      # 0xc45d
pushq $0x43
popq %rsi
pushq $0x1
popq %rdx
callq 0x2470
xorl %edi, %edi
callq 0x2280
callq 0x2410