// File: /workspace/github_cpp_makefile_star2_O1/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 $0x2778, %rsp           # imm = 0x2778
movsd %xmm0, -0x40(%rbp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x2798(%rbp), %r12
movq %r12, %rdi
callq 0x76d4
movq %r12, %rdi
callq 0x21b0
movl %eax, %eax
movq %rax, -0x1410(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x1410(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x26a3
movq $0x270, -0x90(%rbp) # imm = 0x270
movq (%r15), %r12
movq 0x8(%r15), %rax
movq %rax, -0x38(%rbp)
cmpq %rax, %r12
je 0x2892
movq %rbx, -0x30(%rbp)
movsd (%r12), %xmm0
movsd %xmm0, -0x88(%rbp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq -0x1410(%rbp), %rdx
callq 0x7751
movq %r12, -0x48(%rbp)
movq (%rbx), %r15
movq 0x8(%rbx), %rdx
cmpq %rdx, %r15
je 0x286d
xorpd %xmm3, %xmm3
movq %rdx, -0x50(%rbp)
movsd 0x18(%r15), %xmm2
mulsd -0x88(%rbp), %xmm2
movq (%r15), %r13
movq 0x8(%r15), %rbx
leaq (,%r13,2), %rsi
movq %r13, %rax
shlq $0x4, %rax
movsd (%r14,%rax), %xmm4
movq %rbx, %rax
shlq $0x4, %rax
subsd (%r14,%rax), %xmm4
leaq (%rbx,%rbx), %r12
movsd 0x8(%r14,%rsi,8), %xmm5
subsd 0x8(%r14,%r12,8), %xmm5
movsd 0x10(%r15), %xmm6
movapd %xmm5, %xmm1
mulsd %xmm5, %xmm1
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
ucomisd 0x8885(%rip), %xmm0     # 0xb018
jb 0x279b
sqrtsd %xmm0, %xmm0
jmp 0x27de
movsd %xmm3, -0x80(%rbp)
movsd %xmm4, -0x78(%rbp)
movsd %xmm5, -0x70(%rbp)
movq %rsi, -0x68(%rbp)
movsd %xmm2, -0x60(%rbp)
movsd %xmm6, -0x58(%rbp)
callq 0x2100
movsd -0x58(%rbp), %xmm6
movsd -0x60(%rbp), %xmm2
movq -0x68(%rbp), %rsi
movsd -0x70(%rbp), %xmm5
movsd -0x78(%rbp), %xmm4
movsd -0x80(%rbp), %xmm3
movq -0x50(%rbp), %rdx
movsd 0x8822(%rip), %xmm1     # 0xb008
minsd %xmm2, %xmm1
leaq 0x1(,%r13,2), %rcx
leaq 0x1(,%rbx,2), %rax
movapd %xmm0, %xmm2
subsd %xmm6, %xmm2
mulsd %xmm1, %xmm2
mulsd 0x8802(%rip), %xmm2     # 0xb010
movapd %xmm2, %xmm1
divsd %xmm0, %xmm2
mulsd %xmm2, %xmm4
mulsd %xmm2, %xmm5
movsd (%r14,%rsi,8), %xmm0
subsd %xmm4, %xmm0
movsd %xmm0, (%r14,%rsi,8)
movsd (%r14,%rcx,8), %xmm0
subsd %xmm5, %xmm0
movsd %xmm0, (%r14,%rcx,8)
addsd (%r14,%r12,8), %xmm4
maxsd %xmm3, %xmm1
movsd %xmm4, (%r14,%r12,8)
addsd (%r14,%rax,8), %xmm5
movsd %xmm5, (%r14,%rax,8)
addq $0x20, %r15
movapd %xmm1, %xmm3
cmpq %rdx, %r15
jne 0x2728
jmp 0x2871
xorpd %xmm1, %xmm1
movsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
movq -0x30(%rbp), %rbx
movq -0x48(%rbp), %r12
ja 0x2892
addq $0x8, %r12
cmpq -0x38(%rbp), %r12
jne 0x26eb
leaq -0x2798(%rbp), %rdi
callq 0x2140
addq $0x2778, %rsp           # imm = 0x2778
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28cb
jmp 0x28b4
movq %rax, %rbx
leaq -0x2798(%rbp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x84c7