// File: /workspace/github_cpp_makefile_star2_O1/louisabraham[P]cage/example.cpp
// Function: main
int main()
{
    srand(time(NULL));

    const int n = 1000;
    const int dim = 2;

    vector<vector<float_t>> X(n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < dim; j++)
            X[i].push_back(drand());

    using elem_t = vector<float_t>;

    const function<elem_t()> init = []() {
        elem_t ans;
        for (int i = 0; i < dim + 1; i++)
            ans.push_back(drand());
        return ans;
    };

    const function<float_t(elem_t, elem_t)> eval = [&](elem_t coefs, elem_t x) {
        float_t ans = coefs[dim];
        for (int i = 0; i < dim; i++)
            ans += coefs[i] * x[i];
        return ans;
    };

    vector<float_t> y(n);
    auto true_coefs = init();
    cout << "real coeffs ";
    for (auto c : true_coefs)
        cout << setprecision(3) << c << ' ';
    cout << endl;
    for (int i = 0; i < n; i++)
        y[i] = eval(true_coefs, X[i]);

    float mean = 0;
    for (int i = 0; i < n; i++)
        mean += y[i];
    mean /= n;
    float var = 0;
    for (int i = 0; i < n; i++)
    {
        float tmp = y[i] - mean;
        var += tmp * tmp;
    }
    var /= n;

    cout << "std " << sqrt(var) << endl;

    const function<float_t(const elem_t &)> score = [&](const elem_t &coefs) {
        float_t error = 0;
        for (int i = 0; i < n; i++)
        {
            float_t e = eval(coefs, X[i]) - y[i];
            error += e * e;
        }
        error /= n;
        if (error != error)
            return (float)1e6;
        return error;
    };

    const function<void(elem_t &)> mutate = [&](elem_t &coefs) {
        float_t &x = coefs[rand() % (dim + 1)];
        auto ptr = reinterpret_cast<uint32_t *>(&x);
        *ptr ^= (1 << (rand() % 32));
    };

    auto ans = genetic_algorithm(
        init,   // generate a new item
        score,  // score function
        true,   // minimize or maximize
        mutate, // in-place mutation function
        10,     // population size
        100000, // number of optimization steps
        50      // if not -1, use optimal Las Vegas restarts
    );

    cout << "solution coeffs ";
    for (auto c : ans.second)
        cout << setprecision(3) << c << ' ';
    cout << endl;

    cout << "rmse " << sqrt(ans.first) << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x10e0
movl %eax, %edi
callq 0x10f0
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x5dc0, %edi           # imm = 0x5DC0
callq 0x1110
movq %rax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
leaq 0x5dc0(%rax), %rbx
movq %rbx, 0x40(%rsp)
movl $0x5dc0, %edx           # imm = 0x5DC0
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %rbx, 0x38(%rsp)
leaq 0x70(%rsp), %rbx
movl $0x1, %ebp
leaq (%r15,%r15,2), %r12
movq 0x30(%rsp), %rax
leaq (%rax,%r12,8), %r14
callq 0x1040
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x1c68(%rip), %xmm0     # 0x3004
movss %xmm0, 0x70(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x13ba
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x13c5
movq %r14, %rdi
movq %rbx, %rdx
callq 0x26a6
decl %ebp
je 0x137f
incq %r15
cmpq $0x3e8, %r15            # imm = 0x3E8
jne 0x1376
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0x7fe(%rip), %rax       # 0x1be2
movq %rax, 0x88(%rsp)
leaq 0x87f(%rip), %rax       # 0x1c72
movq %rax, 0x80(%rsp)
movaps %xmm0, 0xb0(%rsp)
leaq 0x880(%rip), %rax       # 0x1c8a
movq %rax, 0xc8(%rsp)
leaq 0x8ed(%rip), %rax       # 0x1d06
movq %rax, 0xc0(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0xfa0, %edi            # imm = 0xFA0
callq 0x1110
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, %rbx
addq $0xfa0, %rbx            # imm = 0xFA0
movq %rbx, 0x20(%rsp)
movl $0xfa0, %edx            # imm = 0xFA0
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %rbx, 0x18(%rsp)
cmpq $0x0, 0x80(%rsp)
je 0x19d3
leaq 0x98(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq *0x18(%rsi)
movq 0x3b4c(%rip), %rbx      # 0x4fd8
leaq 0x1bcd(%rip), %rsi      # 0x3060
movl $0xc, %edx
movq %rbx, %rdi
callq 0x1130
movq 0x98(%rsp), %r15
movq 0xa0(%rsp), %r12
cmpq %r12, %r15
je 0x14f8
leaq 0x50(%rsp), %r14
movq (%rbx), %rax
movq -0x18(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
movq $0x3, 0x8(%rbx,%rax)
movq %rbx, %rdi
callq 0x1150
movb $0x20, 0x50(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1130
addq $0x4, %r15
cmpq %r12, %r15
jne 0x14ba
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x3ac2(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x10b0
xorl %r13d, %r13d
leaq 0x130(%rsp), %r14
leaq 0x98(%rsp), %r15
leaq 0x118(%rsp), %r12
leaq 0xb0(%rsp), %rbx
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x1f40
movq 0x30(%rsp), %rsi
addq %r13, %rsi
movq %r12, %rdi
callq 0x1f40
cmpq $0x0, 0xc0(%rsp)
je 0x19ce
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq *0xc8(%rsp)
movq 0x10(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x15a9
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x15c6
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
incq %rbp
addq $0x18, %r13
cmpq $0x3e8, %rbp            # imm = 0x3E8
jne 0x1548
xorps %xmm0, %xmm0
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
addss (%rax,%rcx,4), %xmm0
incq %rcx
cmpq $0x3e8, %rcx            # imm = 0x3E8
jne 0x15e4
divss 0x1a0b(%rip), %xmm0     # 0x3008
xorps %xmm2, %xmm2
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
movss (%rax,%rcx,4), %xmm1
subss %xmm0, %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm2
incq %rcx
cmpq $0x3e8, %rcx            # imm = 0x3E8
jne 0x1607
movss %xmm2, 0x2c(%rsp)
movq 0x39a7(%rip), %rdi      # 0x4fd8
leaq 0x1a35(%rip), %rsi      # 0x306d
movl $0x4, %edx
callq 0x1130
movss 0x2c(%rsp), %xmm0
divss 0x19b8(%rip), %xmm0     # 0x3008
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x165e
sqrtss %xmm0, %xmm0
jmp 0x1663
callq 0x1180
cvtss2sd %xmm0, %xmm0
movq 0x396a(%rip), %rdi      # 0x4fd8
callq 0x1150
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10b0
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl $0x18, %edi
callq 0x1110
movq %rbx, (%rax)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, 0x50(%rsp)
leaq 0x649(%rip), %rax       # 0x1d1e
movq %rax, 0x68(%rsp)
leaq 0x795(%rip), %rax       # 0x1e76
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
leaq 0x7e8(%rip), %rax       # 0x1ee0
movq %rax, 0xe8(%rsp)
leaq 0x821(%rip), %rax       # 0x1f28
movq %rax, 0xe0(%rsp)
leaq 0x188(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2534
leaq 0x168(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x2592
leaq 0x148(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x25f0
leaq 0xf8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
leaq 0x168(%rsp), %rdx
leaq 0x148(%rsp), %r8
movl $0x1, %ecx
movl $0xa, %r9d
pushq $0x32
pushq $0x186a0                # imm = 0x186A0
callq 0x1fc0
addq $0x10, %rsp
movq 0x158(%rsp), %rax
testq %rax, %rax
je 0x17a2
leaq 0x148(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x178(%rsp), %rax
testq %rax, %rax
je 0x17c1
leaq 0x168(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x198(%rsp), %rax
testq %rax, %rax
je 0x17e0
leaq 0x188(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x37f1(%rip), %rbx      # 0x4fd8
leaq 0x1884(%rip), %rsi      # 0x3072
movl $0x10, %edx
movq %rbx, %rdi
callq 0x1130
movq 0x100(%rsp), %r15
movq 0x108(%rsp), %r12
cmpq %r12, %r15
je 0x1853
leaq 0xf(%rsp), %r14
movq (%rbx), %rax
movq -0x18(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
movq $0x3, 0x8(%rbx,%rax)
movq %rbx, %rdi
callq 0x1150
movb $0x20, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1130
addq $0x4, %r15
cmpq %r12, %r15
jne 0x1815
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq 0x3767(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x10b0
movq 0x3753(%rip), %rdi      # 0x4fd8
leaq 0x17f7(%rip), %rsi      # 0x3083
movl $0x5, %edx
callq 0x1130
movss 0xf8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x18ad
sqrtss %xmm0, %xmm0
jmp 0x18b2
callq 0x1180
cvtss2sd %xmm0, %xmm0
movq 0x371b(%rip), %rdi      # 0x4fd8
callq 0x1150
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x1100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10b0
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x1909
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xe0(%rsp), %rax
testq %rax, %rax
je 0x1928
leaq 0xd0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x1941
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x195e
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1975
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xc0(%rsp), %rax
testq %rax, %rax
je 0x1994
leaq 0xb0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x19b0
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x30(%rsp), %rdi
callq 0x264e
xorl %eax, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1070
callq 0x1070
jmp 0x1bc5
jmp 0x1bc5
jmp 0x1bc5
jmp 0x1bc5
jmp 0x1bc5
jmp 0x1bc5
jmp 0x1bc5
movq %rax, %rbx
movq 0x158(%rsp), %rax
testq %rax, %rax
je 0x1a27
leaq 0x148(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1a27
jmp 0x1bc5
movq %rax, %rbx
movq 0x178(%rsp), %rax
testq %rax, %rax
je 0x1a50
leaq 0x168(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1a50
jmp 0x1bc5
movq %rax, %rbx
movq 0x198(%rsp), %rax
testq %rax, %rax
je 0x1b11
leaq 0x188(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1b11
jmp 0x1bc5
movq %rax, %rbx
jmp 0x1b11
jmp 0x1b4f
movq %rax, %rbx
jmp 0x1b86
movq %rax, %rbx
jmp 0x1b6f
jmp 0x1b4f
jmp 0x1b4f
jmp 0x1af1
jmp 0x1b4f
movq %rax, %rbx
jmp 0x1ad2
jmp 0x1ab2
movq %rax, %rbx
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x1ad2
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x130(%rsp), %rdi
testq %rdi, %rdi
je 0x1b52
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1b52
movq %rax, %rbx
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x1b11
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xe0(%rsp), %rax
testq %rax, %rax
je 0x1b30
leaq 0xd0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x1b52
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1b52
jmp 0x1bc5
jmp 0x1bc5
movq %rax, %rbx
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x1b6f
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1b86
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0xc0(%rsp), %rax
testq %rax, %rax
je 0x1ba5
leaq 0xb0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x1bd0
leaq 0x70(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x1bd0
jmp 0x1bc5
movq %rax, %rdi
callq 0x269b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x264e
movq %rbx, %rdi
callq 0x1170