// File: /workspace/github_cpp_makefile_star2_O2/louisabraham[P]cage/./cage.hpp
// Function: std::pair<float, std::vector<float, std::allocator<float>>> genetic_algorithm<std::vector<float, std::allocator<float>>, float>(std::function<std::vector<float, std::allocator<float>> ()>, std::function<float (std::vector<float, std::allocator<float>> const&)>, bool, std::function<void (std::vector<float, std::allocator<float>>&)>, int, int, int)
pair<S, T> genetic_algorithm(
    function<T()> init,           // generate a new item
    function<S(const T &)> score, // score function
    bool minimize,                // minimize or maximize
    function<void(T &)> mutate,   // in-place mutation function
    int pop_size,                 // population size
    int steps,                    // number of optimization steps
    int luby_basis = -1           // if not -1, use optimal Las Vegas restarts
                                  // heuristic: diameter of the search space in number of mutations
)
{
    srand(time(NULL));

    // restarts

    int cur_luby = 1;
    int luby_count = 0;
    int cur_count = 0;
    auto restart = [&]() {
        if (luby_basis == -1)
            return false;
        if (cur_count++ < cur_luby)
            return false;
        cur_count = 0;
        cur_luby = luby_basis * luby(luby_count++);
        return true;
    };

    using pair_t = pair<double, T>;

    auto comp = minimize ? mini<pair_t> : maxi<pair_t>;

    auto rank = [&](pair_t &e) {
        e.first = score(e.second);
    };

    pair_t global_best, best;

    auto reset = [&]() {
        if (comp(best, global_best))
            global_best = best;
        best.second = init();
        rank(best);
        //        cerr << best.first << endl;
    };

    reset();
    global_best = best;

    while (true)
    {
        auto cand = best;
        for (int i = 0; i < pop_size; i++)
        {
            mutate(cand.second);
            rank(cand);
            if (comp(cand, best))
                best = cand;
            steps--;
            if (!steps)
            {
                reset();
                return global_best;
            }
            if (restart())
                reset();
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movl %r9d, 0x34(%rsp)
movq %r8, 0x68(%rsp)
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
xorl %edi, %edi
callq 0x10d0
movl %eax, %edi
callq 0x10e0
leaq 0x8c(%rsp), %rax
leaq 0x88(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x84(%rsp), %rdx
andl $0x0, (%rdx)
movl $0x1, (%rax)
leaq 0x198(%rsp), %rsi
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
movq %rax, 0xc0(%rsp)
movq %rcx, 0xc8(%rsp)
leaq 0x7f3(%rip), %rax       # 0x2481
leaq 0x7f1(%rip), %rcx       # 0x2486
testl %ebp, %ebp
cmovneq %rax, %rcx
leaq 0x78(%rsp), %rax
movq %rcx, (%rax)
leaq 0x70(%rsp), %rcx
movq %r15, (%rcx)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0x90(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
leaq 0x130(%rsp), %rdi
movq %rax, (%rdi)
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
movq %rbx, 0x18(%rdi)
movq %rcx, 0x20(%rdi)
callq 0x2494
leaq 0x40(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x255a
movl 0x190(%rsp), %eax
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movl 0x34(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %r12d
leaq 0xf0(%rsp), %rbx
leaq 0x110(%rsp), %r13
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2578
movq 0x38(%rsp), %rax
leal -0x1(%rax), %r14d
xorl %ebp, %ebp
cmpl %ebp, %r12d
je 0x1e20
movq 0x68(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x258e
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x25a2
movq 0x78(%rsp), %r15
leaq 0xd0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2578
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
callq 0x2578
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
callq *%r15
movl %eax, %r15d
leaq 0x118(%rsp), %rdi
callq 0x23cc
leaq 0xd8(%rsp), %rdi
callq 0x23cc
testb %r15b, %r15b
je 0x1df6
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x255a
cmpl %ebp, %r14d
je 0x1e2e
leaq 0xb0(%rsp), %rdi
callq 0x25bc
testb %al, %al
je 0x1e19
leaq 0x130(%rsp), %rdi
callq 0x2494
incl %ebp
jmp 0x1d69
movq 0x38(%rsp), %rax
subl %ebp, %eax
movq %rax, 0x38(%rsp)
jmp 0x1e7e
leaq 0x130(%rsp), %rdi
callq 0x2494
cvtsd2ss 0x40(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x48(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq $0x0, 0x38(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x23cc
cmpl 0x34(%rsp), %ebp
jge 0x1d4e
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rbx)
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rbx)
leaq 0x98(%rsp), %rdi
callq 0x23cc
leaq 0x48(%rsp), %rdi
callq 0x23cc
movq %rbx, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1fdc
jmp 0x1fa2
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
jmp 0x1fdc
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
jmp 0x1f93
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
leaq 0x118(%rsp), %rdi
callq 0x23cc
leaq 0xd8(%rsp), %rdi
callq 0x23cc
jmp 0x1fcf
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
leaq 0xf8(%rsp), %rdi
callq 0x23cc
leaq 0x98(%rsp), %rdi
callq 0x23cc
leaq 0x48(%rsp), %rdi
callq 0x23cc
movq %rbx, %rdi
callq 0x1150
nop