// File: /workspace/github_cpp_makefile_star2_O1/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 0x99f5
leaq 0x49c6(%rip), %rdi      # 0xe3b6
callq 0xb282
xorpd %xmm0, %xmm0
movq %rsp, %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq (%r14), %rax
movslq 0x4(%rax), %rsi
callq 0x9ce2
movq (%r14), %rax
cmpl $0x0, 0x4(%rax)
jle 0x9a92
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 0x9a78
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd 0x30(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x8(%rsp)
jmp 0x9a83
movq %r15, %rdi
movq %r12, %rdx
callq 0x9dba
incq %r13
movq (%r14), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r13
jl 0x9a28
movq (%rsp), %r15
movq 0x8(%rsp), %r12
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
cmpq %r12, %r15
je 0x9ae9
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      # 0x99a8
movq %r15, %rdi
movq %r12, %rsi
callq 0x9f0b
leaq -0x136(%rip), %rdx      # 0x99a8
movq %r15, %rdi
movq %r12, %rsi
callq 0xa06e
cmpl $0x3, %ebx
jge 0x9c98
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
cmpq $0x18, %rcx
jne 0x9c1b
movq 0x10(%r14), %rax
movl (%rax), %edi
callq 0xadcc
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rax
movslq (%rax), %rdi
callq 0xac62
movq %rax, %r12
movl %ebx, 0x24(%rsp)
movq (%r14), %rax
movl (%rax), %edi
movl 0x4(%rax), %esi
callq 0xa725
movq %rax, %r13
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
je 0x9bb9
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 0x9bae
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 0x9b94
incq %rdi
movl %edi, %r8d
cmpq %r8, %rcx
ja 0x9b61
movq 0x8(%r14), %rdi
callq 0xa8c3
movq (%r14), %rdi
callq 0xa97c
movq 0x10(%r14), %rdi
callq 0xa875
movq %r13, (%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r12, 0x8(%r14)
cmpl $0x0, 0x24(%rsp)
je 0x9bf6
leaq 0x4855(%rip), %rdi      # 0xe446
callq 0xb282
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9c0c
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, %r12d
xorl %r13d, %r13d
leaq 0x47e4(%rip), %r15      # 0xe40f
leaq (,%r13,2), %rcx
addq %r13, %rcx
movsd 0x8(%rax,%rcx,8), %xmm1
movl %r12d, %r13d
leaq (,%r13,2), %rdx
addq %r13, %rdx
movsd 0x8(%rax,%rdx,8), %xmm0
ucomisd %xmm0, %xmm1
jne 0x9c58
jnp 0x9c7c
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %rbp, %rcx
decq %rcx
incl %r12d
cmpq %r13, %rcx
ja 0x9c2b
jmp 0x9b08
leaq (%rax,%rcx,8), %rax
movq 0x9351(%rip), %rcx      # 0x12fd8
movq (%rcx), %rdi
movl (%rax), %edx
movq %r15, %rsi
movb $0x1, %al
callq 0x2310
jmp 0x9c58
callq 0x2b13
jmp 0x9af2
movq %rdx, %rbx
movq %rax, %r14
jmp 0x9cce
jmp 0x9cb2
jmp 0x9cb2
jmp 0x9cb2
jmp 0x9cb2
movq %rdx, %rbx
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9cce
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq %r14, %rdi
cmpl $0x1, %ebx
jne 0x9cdd
movl %ebx, %esi
callq 0x2b35
callq 0x2400