// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
movq 0x10(%rdi), %r15
movq 0x18(%rdi), %rcx
movq %rcx, %rax
subq %r15, %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq %rax, %xmm0
punpckldq 0x2307(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x230f(%rip), %xmm0     # 0x40c0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x231b(%rip), %xmm1     # 0x40e0
leaq 0x10(%rdi), %rax
movq %rax, 0x70(%rsp)
cvttsd2si %xmm1, %eax
movl %eax, 0x60(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0x20(%rsp), %r12
andq $0x0, 0x10(%r12)
movapd %xmm0, (%r12)
xorl %ebx, %ebx
movq %rsp, %r13
xorl %ebp, %ebp
subq %r15, %rcx
movq %rcx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rbp, %rax
jbe 0x1e41
movq %r14, %rdi
movl %ebp, %esi
callq 0x1d24
movq %rax, (%rsp)
addq %rbx, %r15
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x269a
incq %rbp
movq 0x10(%r14), %r15
movq 0x18(%r14), %rcx
addq $0x18, %rbx
jmp 0x1dfe
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq -0xea(%rip), %rdx       # 0x1d68
callq 0x30bb
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x8(%r14), %rcx
cmpq %rcx, %rax
cmovlq %rax, %rcx
movq %rcx, 0x8(%r14)
xorl %ebx, %ebx
movl 0x60(%rsp), %ebp
testl %ebp, %ebp
movl $0x0, %r12d
cmovgl %ebp, %r12d
shlq $0x5, %r12
leaq 0x40(%rsp), %r15
cmpq %rbx, %r12
je 0x1ea9
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x26dc
addq $0x20, %rbx
jmp 0x1e89
movq 0x10(%r14), %rdi
movq 0x18(%r14), %rcx
movq %rcx, %rax
subq %rdi, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
pushq $0x2
popq %rsi
cltd
idivl %esi
movl %eax, %esi
leaq 0x40(%rsp), %rbx
movq 0x8(%rbx), %rax
subq (%rbx), %rax
pushq $-0x18
popq %r8
cqto
idivq %r8
movq %rax, %r15
addl %esi, %r15d
xorpd %xmm0, %xmm0
movq %rsp, %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, (%rax)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x78(%rsp)
movslq %ebp, %r12
movq %r12, %r13
negq %r13
movq %r12, %rbp
shlq $0x5, %rbp
orq $0x8, %rbp
subq %rdi, %rcx
movq %rcx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r12, %rax
jbe 0x1ffe
testl %r15d, %r15d
je 0x1ffe
leaq (%rax,%r13), %rcx
movq %rcx, %xmm0
movq 0x216f(%rip), %xmm1     # 0x40b0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x2173(%rip), %xmm2     # 0x40c0
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addsd 0x78(%rsp), %xmm3
movapd %xmm3, 0x60(%rsp)
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
subsd %xmm1, %xmm0
shufpd $0x2, 0x2139(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movapd %xmm0, 0x80(%rsp)
callq 0x1030
cvtsi2sd %eax, %xmm0
movapd 0x60(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
divpd 0x80(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movq 0x20(%rsp), %rsi
addq %rbp, %rsi
ucomisd %xmm0, %xmm1
jae 0x1fdc
movq %rsp, %rdi
callq 0x26dc
jmp 0x1fe7
movq %rbx, %rdi
callq 0x26dc
decl %r15d
incq %r12
movq 0x10(%r14), %rdi
movq 0x18(%r14), %rcx
decq %r13
addq $0x20, %rbp
jmp 0x1f10
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2718
incl %r15d
xorl %esi, %esi
leaq 0x40(%rsp), %r14
decl %r15d
testl %r15d, %r15d
jle 0x2033
leaq 0x18(%rsi), %rbx
addq (%rsp), %rsi
movq %r14, %rdi
callq 0x26dc
movq %rbx, %rsi
jmp 0x2016
movq %rsp, %rdi
callq 0x24c0
leaq 0x20(%rsp), %rdi
callq 0x277e
movq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x24c0
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x2786
movq %rsp, %rdi
callq 0x1538
leaq 0x20(%rsp), %rdi
callq 0x2864
leaq 0x40(%rsp), %rdi
callq 0x1538
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x20a2
jmp 0x2093
jmp 0x2093
movq %rax, %rbx
movq %rsp, %rdi
callq 0x1538
jmp 0x20a5
jmp 0x20a2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2864
leaq 0x40(%rsp), %rdi
callq 0x1538
movq %rbx, %rdi
callq 0x1180
nop