// File: /workspace/github_cpp_makefile_star2_O3/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::sgd(double*, std::vector<sgd2::term, std::allocator<sgd2::term>>&, std::vector<double, std::allocator<double>> const&, double)
void sgd(double* X, std::vector<term> &terms, const std::vector<double> &etas, double delta)
{
    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);

        double Delta_max = 0;
        for (const term &t : terms)
        {
            // cap step size
            double w_ij = t.w;
            double mu = eta * w_ij;
            if (mu > 1)
                mu = 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);

            // check distances for early stopping
            double Delta = mu * (mag-d_ij) / 2;
            if (Delta > Delta_max)
                Delta_max = Delta;

            double r = Delta / mag;
            double r_x = r * dx;
            double r_y = r * dy;
            
            X[i*2] -= r_x;
            X[i*2+1] -= r_y;
            X[j*2] += r_x;
            X[j*2+1] += r_y;
        }
        //std::cerr << ++iteration << ", eta: " << eta << ", Delta: " << Delta_max << std::endl;
        if (Delta_max < delta)
            return;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2728, %rsp           # imm = 0x2728
movsd %xmm0, -0x30(%rbp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x2748(%rbp), %r12
movq %r12, %rdi
callq 0x6fc0
movq %r12, %rdi
callq 0x21a0
movl %eax, %eax
movq %rax, -0x13c0(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x13c0(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x2683
movq $0x270, -0x40(%rbp)     # imm = 0x270
movq (%r15), %r12
movq 0x8(%r15), %r13
cmpq %r13, %r12
je 0x27f8
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq -0x13c0(%rbp), %r15
movsd (%r12), %xmm0
movsd %xmm0, -0x38(%rbp)
movq %r15, %rdx
callq 0x703d
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
cmpq %rsi, %rdi
je 0x27dc
xorpd %xmm0, %xmm0
movq %rdi, %rax
movsd 0x8907(%rip), %xmm9     # 0xb008
movsd 0x8906(%rip), %xmm10    # 0xb010
movsd -0x38(%rbp), %xmm11
movsd 0x18(%rax), %xmm1
mulsd %xmm11, %xmm1
movapd %xmm9, %xmm3
minsd %xmm1, %xmm3
movq (%rax), %rdx
movq 0x8(%rax), %rcx
shlq $0x4, %rdx
shlq $0x4, %rcx
movsd (%r14,%rdx), %xmm4
movsd 0x8(%r14,%rdx), %xmm5
movapd %xmm4, %xmm2
subsd (%r14,%rcx), %xmm2
movapd %xmm5, %xmm6
subsd 0x8(%r14,%rcx), %xmm6
movapd %xmm6, %xmm1
mulsd %xmm6, %xmm1
movapd %xmm2, %xmm7
mulsd %xmm2, %xmm7
addsd %xmm1, %xmm7
sqrtsd %xmm7, %xmm7
movapd %xmm7, %xmm8
subsd 0x10(%rax), %xmm8
mulsd %xmm3, %xmm8
mulsd %xmm10, %xmm8
movapd %xmm8, %xmm1
divsd %xmm7, %xmm8
mulsd %xmm8, %xmm2
mulsd %xmm6, %xmm8
subsd %xmm2, %xmm4
movsd %xmm4, (%r14,%rdx)
subsd %xmm8, %xmm5
movsd %xmm5, 0x8(%r14,%rdx)
addsd (%r14,%rcx), %xmm2
maxsd %xmm0, %xmm1
movsd %xmm2, (%r14,%rcx)
addsd 0x8(%r14,%rcx), %xmm8
movsd %xmm8, 0x8(%r14,%rcx)
addq $0x20, %rax
movapd %xmm1, %xmm0
cmpq %rsi, %rax
jne 0x2710
jmp 0x27e0
xorpd %xmm1, %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x27f8
addq $0x8, %r12
cmpq %r13, %r12
jne 0x26ce
leaq -0x2748(%rbp), %rdi
callq 0x2130
addq $0x2728, %rsp           # imm = 0x2728
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2831
jmp 0x281a
movq %rax, %rbx
leaq -0x2748(%rbp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x7da0