// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2778, %rsp           # imm = 0x2778
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x27a0(%rbp), %r12
movq %r12, %rdi
callq 0x76d4
movq %r12, %rdi
callq 0x21b0
movl %eax, %eax
movq %rax, -0x1418(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x1418(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x3fac
movq $0x270, -0x98(%rbp) # imm = 0x270
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rcx, -0x38(%rbp)
cmpq %rcx, %rax
je 0x41b7
movq %rbx, -0x30(%rbp)
movq %rax, -0x40(%rbp)
movsd (%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq -0x1418(%rbp), %rdx
callq 0x811f
movq (%rbx), %r15
movq 0x8(%rbx), %rdx
cmpq %rdx, %r15
movq %rdx, -0x48(%rbp)
je 0x41a1
movsd 0x18(%r15), %xmm6
movsd -0x78(%rbp), %xmm0
mulsd %xmm0, %xmm6
movsd 0x20(%r15), %xmm2
mulsd %xmm0, %xmm2
movq (%r15), %r13
movq 0x8(%r15), %rbx
leaq (,%r13,2), %r12
movq %r13, %rax
shlq $0x4, %rax
movsd (%r14,%rax), %xmm4
movq %rbx, %rax
shlq $0x4, %rax
subsd (%r14,%rax), %xmm4
leaq (%rbx,%rbx), %rsi
movsd 0x8(%r14,%r12,8), %xmm5
subsd 0x8(%r14,%rsi,8), %xmm5
movsd 0x10(%r15), %xmm7
movapd %xmm5, %xmm1
mulsd %xmm5, %xmm1
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
ucomisd 0x6f7a(%rip), %xmm0     # 0xb018
jb 0x40a6
sqrtsd %xmm0, %xmm0
jmp 0x40ef
movsd %xmm4, -0x70(%rbp)
movsd %xmm5, -0x68(%rbp)
movapd %xmm6, -0x90(%rbp)
movq %rsi, -0x60(%rbp)
movsd %xmm2, -0x58(%rbp)
movsd %xmm7, -0x50(%rbp)
callq 0x2100
movsd -0x50(%rbp), %xmm7
movsd -0x58(%rbp), %xmm2
movq -0x60(%rbp), %rsi
movapd -0x90(%rbp), %xmm6
movsd -0x68(%rbp), %xmm5
movsd -0x70(%rbp), %xmm4
movq -0x48(%rbp), %rdx
movsd 0x6f11(%rip), %xmm3     # 0xb008
movapd %xmm3, %xmm1
minsd %xmm2, %xmm1
leaq 0x1(,%r13,2), %rcx
leaq 0x1(,%rbx,2), %rax
movapd %xmm0, %xmm2
subsd %xmm7, %xmm2
addsd %xmm0, %xmm0
divsd %xmm0, %xmm2
mulsd %xmm2, %xmm4
mulsd %xmm2, %xmm5
movapd %xmm3, %xmm0
cmpltsd %xmm6, %xmm0
xorpd 0x6f58(%rip), %xmm6     # 0xb090
movapd %xmm0, %xmm2
movsd 0x6edc(%rip), %xmm3     # 0xb020
andpd %xmm3, %xmm2
andnpd %xmm6, %xmm0
orpd %xmm2, %xmm0
movapd %xmm0, %xmm2
mulsd %xmm4, %xmm2
addsd (%r14,%r12,8), %xmm2
movsd %xmm2, (%r14,%r12,8)
mulsd %xmm5, %xmm0
addsd (%r14,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rcx,8)
mulsd %xmm1, %xmm4
addsd (%r14,%rsi,8), %xmm4
movsd %xmm4, (%r14,%rsi,8)
mulsd %xmm1, %xmm5
addsd (%r14,%rax,8), %xmm5
movsd %xmm5, (%r14,%rax,8)
addq $0x28, %r15
cmpq %rdx, %r15
jne 0x4028
movq -0x40(%rbp), %rax
addq $0x8, %rax
cmpq -0x38(%rbp), %rax
movq -0x30(%rbp), %rbx
jne 0x3ff4
leaq -0x27a0(%rbp), %rdi
callq 0x2140
addq $0x2778, %rsp           # imm = 0x2778
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x41f0
jmp 0x41d9
movq %rax, %rbx
leaq -0x27a0(%rbp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x84c7