// File: /workspace/github_cpp_makefile_star2_O3/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 -0x2740(%rbp), %r12
movq %r12, %rdi
callq 0x6fc0
movq %r12, %rdi
callq 0x21a0
movl %eax, %eax
movq %rax, -0x13b8(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x13b8(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x3e70
movq $0x270, -0x38(%rbp)     # imm = 0x270
movq (%r15), %r12
movq 0x8(%r15), %r13
cmpq %r13, %r12
je 0x3ffe
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq -0x13b8(%rbp), %r15
movsd (%r12), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %r15, %rdx
callq 0x79f3
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq %rdi, %rax
cmpq %rsi, %rdi
movsd 0x7124(%rip), %xmm8     # 0xb008
movapd 0x71a3(%rip), %xmm9     # 0xb090
movsd 0x712a(%rip), %xmm10    # 0xb020
movsd -0x30(%rbp), %xmm11
je 0x3ff1
movsd 0x18(%rax), %xmm2
mulsd %xmm11, %xmm2
movsd 0x20(%rax), %xmm1
mulsd %xmm11, %xmm1
movapd %xmm8, %xmm0
minsd %xmm1, %xmm0
movq (%rax), %rdx
movq 0x8(%rax), %rcx
shlq $0x4, %rdx
shlq $0x4, %rcx
movsd (%r14,%rdx), %xmm5
movsd 0x8(%r14,%rdx), %xmm3
movapd %xmm5, %xmm1
subsd (%r14,%rcx), %xmm1
movapd %xmm3, %xmm6
subsd 0x8(%r14,%rcx), %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
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,%rdx)
mulsd %xmm4, %xmm6
addsd %xmm3, %xmm6
movsd %xmm6, 0x8(%r14,%rdx)
mulsd %xmm0, %xmm1
addsd (%r14,%rcx), %xmm1
movsd %xmm1, (%r14,%rcx)
mulsd %xmm0, %xmm4
addsd 0x8(%r14,%rcx), %xmm4
movsd %xmm4, 0x8(%r14,%rcx)
addq $0x28, %rax
cmpq %rsi, %rax
jne 0x3f02
addq $0x8, %r12
cmpq %r13, %r12
jne 0x3ebb
leaq -0x2740(%rbp), %rdi
callq 0x2130
addq $0x2718, %rsp           # imm = 0x2718
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4037
jmp 0x4020
movq %rax, %rbx
leaq -0x2740(%rbp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x7da0