// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::sgd(double*, std::vector<sgd2::term_sparse, std::allocator<sgd2::term_sparse>>&, std::vector<double, std::allocator<double>> const&)
void sgd(double* X, std::vector<term_sparse> &terms, const std::vector<double> &etas)
{
    std::random_device rd;
    std::mt19937 rg(rd());

    // iterate through step sizes
    for (double eta : etas)
    {
        // shuffle terms
        std::shuffle(terms.begin(), terms.end(), rg);

        for (const term_sparse& t : terms)
        {
            // cap step size
            double mu_i = eta * t.w_ij;
            if (mu_i > 1)
                mu_i = 1;

            // cap step size
            double mu_j = eta * t.w_ji;
            if (mu_j > 1)
                mu_j = 1;

            double d_ij = t.d;
            uint64_t i = t.i, j = t.j;

            double dx = X[i*2]-X[j*2], dy = X[i*2+1]-X[j*2+1];
            double mag = sqrt(dx*dx + dy*dy);

            double r = (mag-d_ij) / (2*mag);
            double r_x = r * dx;
            double r_y = r * dy;

            X[i*2] -= mu_i * r_x;
            X[i*2+1] -= mu_i * r_y;
            X[j*2] += mu_j * r_x;
            X[j*2+1] += mu_j * r_y;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2718, %rsp           # imm = 0x2718
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x13b8(%rbp), %r12
movq %r12, %rdi
callq 0x5b0e
movq %r12, %rdi
callq 0x2180
movl %eax, %esi
leaq -0x2740(%rbp), %rdi
callq 0x6ccc
movq (%r15), %r12
movq 0x8(%r15), %r13
leaq -0x2740(%rbp), %r15
cmpq %r13, %r12
je 0x378b
movsd (%r12), %xmm0
movsd %xmm0, -0x30(%rbp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq %r15, %rdx
callq 0x62bb
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movsd 0x7992(%rip), %xmm8     # 0xb008
movapd 0x7a11(%rip), %xmm9     # 0xb090
movsd 0x7998(%rip), %xmm10    # 0xb020
movsd -0x30(%rbp), %xmm11
cmpq %rcx, %rax
je 0x3782
movsd 0x18(%rax), %xmm2
movsd 0x20(%rax), %xmm1
mulsd %xmm11, %xmm1
movapd %xmm8, %xmm0
minsd %xmm1, %xmm0
movq (%rax), %rsi
movq 0x8(%rax), %rdx
shlq $0x4, %rsi
shlq $0x4, %rdx
movsd (%r14,%rsi), %xmm5
movsd 0x8(%r14,%rsi), %xmm3
movapd %xmm5, %xmm1
subsd (%r14,%rdx), %xmm1
movapd %xmm3, %xmm6
subsd 0x8(%r14,%rdx), %xmm6
movapd %xmm6, %xmm4
mulsd %xmm6, %xmm4
movapd %xmm1, %xmm7
mulsd %xmm1, %xmm7
addsd %xmm4, %xmm7
sqrtsd %xmm7, %xmm7
movapd %xmm7, %xmm4
subsd 0x10(%rax), %xmm4
mulsd %xmm11, %xmm2
addsd %xmm7, %xmm7
divsd %xmm7, %xmm4
mulsd %xmm4, %xmm1
mulsd %xmm6, %xmm4
movapd %xmm8, %xmm6
cmpltsd %xmm2, %xmm6
xorpd %xmm9, %xmm2
movapd %xmm6, %xmm7
andnpd %xmm2, %xmm7
andpd %xmm10, %xmm6
orpd %xmm7, %xmm6
movapd %xmm6, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm5, %xmm2
movsd %xmm2, (%r14,%rsi)
mulsd %xmm4, %xmm6
addsd %xmm3, %xmm6
movsd %xmm6, 0x8(%r14,%rsi)
mulsd %xmm0, %xmm1
addsd (%r14,%rdx), %xmm1
movsd %xmm1, (%r14,%rdx)
mulsd %xmm0, %xmm4
addsd 0x8(%r14,%rdx), %xmm4
movsd %xmm4, 0x8(%r14,%rdx)
addq $0x28, %rax
jmp 0x368e
addq $0x8, %r12
jmp 0x3643
leaq -0x13b8(%rbp), %rdi
callq 0x5d56
addq $0x2718, %rsp           # imm = 0x2718
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x37ab
movq %rax, %rbx
leaq -0x13b8(%rbp), %rdi
callq 0x5d56
movq %rbx, %rdi
callq 0x2210