// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %esi, %ebx
movq %rdi, %r14
testl %esi, %esi
je 0x9acd
leaq 0x48ee(%rip), %rdi      # 0xe3b6
callq 0xb482
xorpd %xmm0, %xmm0
movq %rsp, %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq (%r14), %rax
movslq 0x4(%rax), %rsi
callq 0x9dc2
movq (%r14), %rax
cmpl $0x0, 0x4(%rax)
jle 0x9b6a
xorl %r13d, %r13d
movq %rsp, %r15
leaq 0x30(%rsp), %r12
movq 0x8(%r14), %rax
movq 0x8(%rax), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x30(%rsp)
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x38(%rsp)
movl %r13d, 0x40(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x9b50
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd 0x30(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x8(%rsp)
jmp 0x9b5b
movq %r15, %rdi
movq %r12, %rdx
callq 0x9ea0
incq %r13
movq (%r14), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r13
jl 0x9b00
movq (%rsp), %r15
movq 0x8(%rsp), %r12
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
cmpq %r12, %r15
je 0x9bc1
movq %r12, %rax
subq %r15, %rax
sarq $0x3, %rax
imulq %rbp, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x124(%rip), %rcx      # 0x9a80
movq %r15, %rdi
movq %r12, %rsi
callq 0x9ff1
leaq -0x136(%rip), %rdx      # 0x9a80
movq %r15, %rdi
movq %r12, %rsi
callq 0xa154
cmpl $0x3, %ebx
jge 0x9d77
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x18, %rdx
jne 0x9cf6
movq 0x10(%r14), %rax
movl (%rax), %edi
callq 0xaf20
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rax
movslq (%rax), %rdi
callq 0xadb7
movq %rax, %r12
movl %ebx, 0x24(%rsp)
movq (%r14), %rax
movl (%rax), %edi
movl 0x4(%rax), %esi
callq 0xa7f6
movq %rax, %r13
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0x9c94
sarq $0x3, %rcx
imulq %rbp, %rcx
movq 0x28(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x8(%r12), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r8
movl (%rax,%r8,8), %r9d
movsd 0x8(%rax,%r8,8), %xmm0
movslq 0x10(%rax,%r8,8), %r8
movsd %xmm0, (%rdx,%rdi,8)
movl %r9d, (%rsi,%rdi,4)
movslq (%r13), %r9
testq %r9, %r9
jle 0x9c89
movq (%r14), %r10
movq 0x8(%r10), %r10
movq 0x8(%r13), %r11
xorl %ebp, %ebp
movq (%r10,%rbp,8), %rbx
movzwl (%rbx,%r8,2), %ebx
movq (%r11,%rbp,8), %r15
movw %bx, (%r15,%rdi,2)
incq %rbp
cmpq %rbp, %r9
jne 0x9c6f
incq %rdi
movl %edi, %r8d
cmpq %r8, %rcx
ja 0x9c3c
movq 0x8(%r14), %rdi
callq 0xa98b
movq (%r14), %rdi
callq 0xaa2f
movq 0x10(%r14), %rdi
callq 0xa94b
movq %r13, (%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r12, 0x8(%r14)
cmpl $0x0, 0x24(%rsp)
je 0x9cd1
leaq 0x477a(%rip), %rdi      # 0xe446
callq 0xb482
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9ce7
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edx
xorl %r13d, %r13d
movq 0x92d3(%rip), %r12      # 0x12fd8
leaq 0x4703(%rip), %r15      # 0xe40f
leaq (,%r13,2), %rsi
addq %r13, %rsi
movsd 0x8(%rax,%rsi,8), %xmm1
movl %edx, %r13d
leaq (,%r13,2), %rdx
addq %r13, %rdx
movsd 0x8(%rax,%rdx,8), %xmm0
ucomisd %xmm0, %xmm1
jne 0x9d39
jnp 0x9d58
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
imulq %rbp, %rsi
decq %rsi
leal 0x1(%r13), %edx
cmpq %r13, %rsi
ja 0x9d0c
jmp 0x9be3
leaq (%rax,%rsi,8), %rax
movq (%r12), %rdi
movl (%rax), %edx
movq %r15, %rsi
movb $0x1, %al
callq 0x2310
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
jmp 0x9d39
callq 0x2b13
jmp 0x9bca
movq %rdx, %rbx
movq %rax, %r14
jmp 0x9dad
jmp 0x9d91
jmp 0x9d91
jmp 0x9d91
jmp 0x9d91
movq %rdx, %rbx
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9dad
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq %r14, %rdi
cmpl $0x1, %ebx
jne 0x9dbc
movl %ebx, %esi
callq 0x2b35
callq 0x2400
nop