// File: gabrielfelipeg[P]TravellingSalesman-GeneticAlgo/src/population.cpp
// Function: Population::select()
void Population::select(){
    int elitism = population.size() * 0.1;
    std::vector <Individual> newPop;
    std::vector < std::pair<long long, Individual> > aux;
    for(int i = 0; i < population.size(); i++){
        aux.emplace_back(F(i), population[i]);
    }
    sort(aux.begin(), aux.end(), comparator);
    currentBest = std::min(currentBest, aux[0].first);
    for(int i = 0; i < elitism; i++){
        newPop.emplace_back(aux[i].second);    
    }
    int needed = ((int)population.size() / 2) - (int)newPop.size();
    std::vector <Individual> secondChance;
    for(int i = elitism; i < population.size() && needed; i++){
        double chance = (population.size() - i + elitism * 1.0) / ((double)population.size() * population.size() * 1.0 - population.size());
        double random = ((double)rand()) / RAND_MAX;
        if(chance >= random){
            newPop.emplace_back(aux[i].second);
            needed--;
        }else{
            secondChance.emplace_back(aux[i].second);
        }
    }
    int ind = 0;
    std::random_shuffle(secondChance.begin(), secondChance.end());
    while(needed > 0){
        needed--;
        newPop.emplace_back(secondChance[ind++]);
    }
    secondChance.clear();
    aux.clear();
    population.clear();
    population = newPop;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
addq $0x10, %rdi
callq 0x5530
movq %rax, %xmm0
movaps 0x6e61(%rip), %xmm1     # 0xb0b0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x6e65(%rip), %xmm1     # 0xb0c0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x6e26(%rip), %xmm1     # 0xb098
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xc(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x50b0
leaq -0x40(%rbp), %rdi
callq 0x5560
movl $0x0, -0x44(%rbp)
movq -0xc8(%rbp), %rdi
movslq -0x44(%rbp), %rax
movq %rax, -0xd0(%rbp)
addq $0x10, %rdi
callq 0x5530
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x4317
movq -0xc8(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x4120
movq -0xc8(%rbp), %rdi
movq %rax, -0x50(%rbp)
addq $0x10, %rdi
movslq -0x44(%rbp), %rsi
callq 0x5510
movq %rax, %rdx
leaq -0x40(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x5580
jmp 0x42f9
jmp 0x42fb
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4296
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4767
leaq -0x40(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x5650
movq -0xd8(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x5680
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
leaq -0x14a(%rip), %rdx      # 0x4200
callq 0x5600
jmp 0x4351
movq -0xc8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xe8(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x56f0
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0x56b0
movq %rax, -0xe0(%rbp)
jmp 0x4388
movq -0xc8(%rbp), %rax
movq -0xe0(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x43d8
movslq -0x74(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x56f0
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x28(%rbp), %rdi
callq 0x5710
jmp 0x43cb
jmp 0x43cd
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x43a4
movq -0xc8(%rbp), %rdi
addq $0x10, %rdi
callq 0x5530
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0xec(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5530
movq %rax, %rcx
movl -0xec(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x78(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x50b0
movl -0xc(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0xc8(%rbp), %rdi
movslq -0x94(%rbp), %rax
movq %rax, -0xf8(%rbp)
addq $0x10, %rdi
callq 0x5530
movq -0xf8(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0xed(%rbp)
jae 0x4464
cmpl $0x0, -0x78(%rbp)
setne %al
movb %al, -0xed(%rbp)
movb -0xed(%rbp), %al
testb $0x1, %al
jne 0x4473
jmp 0x4679
movq -0xc8(%rbp), %rdi
addq $0x10, %rdi
movq %rdi, -0x138(%rbp)
callq 0x5530
movq -0x138(%rbp), %rdi
movslq -0x94(%rbp), %rcx
subq %rcx, %rax
movq %rax, %xmm1
movaps 0x6c09(%rip), %xmm0     # 0xb0b0
movaps %xmm0, -0x130(%rbp)
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x6c06(%rip), %xmm0     # 0xb0c0
movaps %xmm0, -0x120(%rbp)
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
cvtsi2sdl -0xc(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
callq 0x5530
movq -0x138(%rbp), %rdi
movaps -0x130(%rbp), %xmm2
movaps -0x120(%rbp), %xmm1
movq %rax, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
callq 0x5530
movsd -0x140(%rbp), %xmm0
movq -0x138(%rbp), %rdi
movaps -0x130(%rbp), %xmm3
movaps -0x120(%rbp), %xmm2
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
callq 0x5530
movaps -0x130(%rbp), %xmm4
movaps -0x120(%rbp), %xmm3
movsd -0x108(%rbp), %xmm1
movsd -0x100(%rbp), %xmm0
movq %rax, %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
subpd %xmm3, %xmm2
movaps %xmm2, %xmm3
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm3, %xmm2
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
callq 0x1030
cvtsi2sd %eax, %xmm0
movsd 0x6acc(%rip), %xmm1     # 0xb0a0
divsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa0(%rbp), %xmm0
ucomisd -0xa8(%rbp), %xmm0
jb 0x463c
movslq -0x94(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x56f0
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x28(%rbp), %rdi
callq 0x5710
jmp 0x4614
movl -0x78(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x4663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x1890
jmp 0x4767
movslq -0x94(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x56f0
movq %rax, %rsi
addq $0x8, %rsi
leaq -0x90(%rbp), %rdi
callq 0x5710
jmp 0x4661
jmp 0x4663
jmp 0x4665
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x4422
movl $0x0, -0xac(%rbp)
leaq -0x90(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x5860
movq -0x148(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x5890
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x5790
jmp 0x46c5
jmp 0x46c7
cmpl $0x0, -0x78(%rbp)
jle 0x4702
movl -0x78(%rbp), %eax
decl %eax
movl %eax, -0x78(%rbp)
movslq -0xac(%rbp), %rsi
movl %esi, %eax
incl %eax
movl %eax, -0xac(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5510
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x5710
jmp 0x4700
jmp 0x46c7
leaq -0x90(%rbp), %rdi
callq 0x50f0
leaq -0x40(%rbp), %rdi
callq 0x58c0
movq -0xc8(%rbp), %rdi
addq $0x10, %rdi
movq %rdi, -0x150(%rbp)
callq 0x50f0
movq -0x150(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x58e0
jmp 0x4740
leaq -0x90(%rbp), %rdi
callq 0x1890
leaq -0x40(%rbp), %rdi
callq 0x5b30
leaq -0x28(%rbp), %rdi
callq 0x1890
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x5b30
leaq -0x28(%rbp), %rdi
callq 0x1890
movq -0x58(%rbp), %rdi
callq 0x1180
nopw %cs:(%rax,%rax)