// File: /workspace/github_cpp_makefile_star2_O1/Quinny[P]IteratedPrisoners/./ipd/genetic.h
// Function: ipd::genetic::evolve(int, int, int, std::function<std::vector<std::pair<ipd::prisoner_t, int>, std::allocator<std::pair<ipd::prisoner_t, int>>> (std::vector<ipd::prisoner_t, std::allocator<ipd::prisoner_t>> const&)>)
prisoner_t evolve(int pop_size, int mutation_rate, int generations,
        fitness_fn fitness_func) {
    auto population = initial_population(
            pop_size, ipd::config::mem_first, ipd::config::mem_last);
    auto mutate     = std::bind(mutate_strategy, _1, mutation_rate);

    for (int i = 0; i < generations; ++i) {
        // fitness
        auto evaluation = fitness_func(std::cref(population));

        // selection weighted on fitness
        auto selection = weighted_random_sample(evaluation, pop_size);

        // cross over
        std::vector<std::string> genomes;
        for (auto i = 0UL; i < selection.size() - 1; ++i) {
            auto p = cross(selection[i].name, selection[i + 1].name);
            genomes.push_back(p.first); genomes.push_back(p.second);
        }

        // mutate
        std::transform(genomes.begin(), genomes.end(), genomes.begin(), mutate);

        // build next population
        population.clear();
        for (auto i: genomes)
            population.push_back(make_prisoner_t(genetic_strategy(i)));
    }

    // final eval uses overall performance
    auto evaluation = evaluate_async(std::cref(population));
    std::sort(evaluation.begin(), evaluation.end(), std::greater<score_t>());
    return evaluation.front().first;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r8, %r15
movl %ecx, %ebx
movl %edx, -0x30(%rbp)
movq %rdi, -0xc8(%rbp)
movl %esi, -0x34(%rbp)
movslq %esi, %rsi
movslq 0xe7a1(%rip), %rdx      # 0x1535c
movslq 0xe79e(%rip), %rcx      # 0x15360
leaq -0x68(%rbp), %rdi
callq 0x63fd
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
jle 0x6e4a
xorl %r12d, %r12d
leaq -0x160(%rbp), %rbx
movq %r15, -0xd0(%rbp)
cmpq $0x0, 0x10(%r15)
je 0x6f09
leaq -0x128(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
leaq -0x68(%rbp), %rdx
callq *0x18(%r15)
movq %r12, -0xd8(%rbp)
leaq -0xc0(%rbp), %rdi
movq %r14, %rsi
movl -0x34(%rbp), %edx
callq 0x8cc5
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rax
subq %rdx, %rax
cmpq $0x40, %rax
je 0x6ce0
movl $0x40, %r15d
xorl %r12d, %r12d
leaq (%rdx,%r15), %rsi
addq $-0x40, %rsi
addq %r15, %rdx
leaq -0xa8(%rbp), %r13
movq %r13, %rdi
callq 0x6a68
leaq -0x50(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x8f08
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
callq 0x8f08
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x6ca0
callq 0x4350
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x6cb8
callq 0x4350
incq %r12
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rax
subq %rdx, %rax
sarq $0x6, %rax
decq %rax
addq $0x40, %r15
cmpq %rax, %r12
jb 0x6c53
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x339(%rip), %rax      # 0x69b6
movq %rax, -0x110(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x108(%rbp)
movq %rdi, %rdx
leaq -0x110(%rbp), %rcx
callq 0x8f89
movq -0x68(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0xd87a
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq %rax, %r15
leaq -0x128(%rbp), %r12
je 0x6e12
leaq -0xf0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq -0x100(%rbp), %r14
movq %r14, %rdi
callq 0x9bf0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9006
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6185
leaq -0x68(%rbp), %rdi
movq %r14, %rsi
callq 0x92bc
movq -0x78(%rbp), %rax
testq %rax, %rax
je 0x6da4
leaq -0x88(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq -0xf0(%rbp), %r13
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x6dc3
callq 0x4350
movq -0x148(%rbp), %rdi
testq %rdi, %rdi
je 0x6dd4
callq 0x4350
movq -0x160(%rbp), %rdi
cmpq %rbx, %rdi
je 0x6df0
movq (%rdi), %r14
callq 0x4350
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x6de0
movq -0x100(%rbp), %rdi
cmpq %r13, %rdi
je 0x6e01
callq 0x4350
addq $0x20, %r15
cmpq -0xe0(%rbp), %r15
jne 0x6d3a
leaq -0x50(%rbp), %rdi
callq 0x90e4
leaq -0xc0(%rbp), %rdi
callq 0x84bc
movq %r12, %rdi
callq 0x8446
movq -0xd8(%rbp), %r12
incl %r12d
cmpl -0x2c(%rbp), %r12d
movq -0xd0(%rbp), %r15
jne 0x6be7
leaq -0xa8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x6527
movq -0xa8(%rbp), %rbx
movq -0xa0(%rbp), %r14
cmpq %r14, %rbx
movq -0xc8(%rbp), %r15
je 0x6eaf
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9f80
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa05e
movq -0xa8(%rbp), %rbx
leaq 0x10(%r15), %r14
movq %r14, (%r15)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x9bf0
leaq 0x20(%r15), %rdi
addq $0x20, %rbx
movq %rbx, %rsi
callq 0x9302
leaq -0xa8(%rbp), %rdi
callq 0x8446
leaq -0x68(%rbp), %rdi
callq 0x84bc
movq %r15, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4120
movq %rax, %rbx
movq (%r15), %rdi
cmpq %r14, %rdi
je 0x6f25
callq 0x4350
jmp 0x6f25
jmp 0x6f3a
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
jmp 0x6fb8
jmp 0x6f88
jmp 0x6f3a
movq %rax, %rbx
jmp 0x6fb1
movq %rax, %rbx
jmp 0x6fbd
movq %rax, %rdi
callq 0x92f4
jmp 0x6f88
movq %rax, %rbx
jmp 0x6f6e
movq %rax, %rbx
jmp 0x6f62
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x8404
leaq -0x160(%rbp), %rdi
callq 0x89f2
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x6f9c
callq 0x4350
jmp 0x6f9c
movq %rax, %rbx
jmp 0x6f9c
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x8f54
leaq -0x50(%rbp), %rdi
callq 0x90e4
leaq -0xc0(%rbp), %rdi
callq 0x84bc
leaq -0x128(%rbp), %rdi
callq 0x8446
leaq -0x68(%rbp), %rdi
callq 0x84bc
movq %rbx, %rdi
callq 0x4520