// File: fabianp[P]ProxASAGA/proxasaga_atomic.cpp
// Function: saga_single_thread_nonatomic(double*, double*, double*, double*, long*, long*, double*, double*, long, long, double, double, double, long, double*, double*, long)
void saga_single_thread_nonatomic(
        double* x, double* memory_gradient, double* gradient_average,
        double* A_data, int64_t* A_indices, int64_t* A_indptr, double* b, double* d, int64_t n_samples,
        int64_t n_features, double alpha, double beta, double step_size, int64_t max_iter,
        double* trace_x, double* trace_time, int64_t iter_freq) {
          int64_t i, j, j_idx, local_counter=0, global_counter;
          double p, grad_i, incr, old_grad, delta;
          std::random_device rd;
          std::mt19937 rng(rd());
          std::uniform_int_distribution<int64_t> uni(0, n_samples-1);
          struct timespec start, finish;
          double elapsed;
          clock_gettime(CLOCK_MONOTONIC, &start);

          while (true) {
            /* take a snapshot of the current vector of iterates in trace_x
               and time in trace_time, in order to plot convergence later on */
            if (local_counter % iter_freq == 0) {
                int64_t c = local_counter / iter_freq;
                for (j=0; j<n_features; j++) {
                    trace_x[n_features * c + j] = x[j];
                }
                clock_gettime(CLOCK_MONOTONIC, &finish);
                elapsed = (finish.tv_sec - start.tv_sec);
                elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
                trace_time[c] = (double) elapsed;
                printf(".. iteration %lld, time elapsed %f min ..\n", c, elapsed / 60.);
            }

            i = uni(rng);
            p = 0.;
            for (j=A_indptr[i]; j < A_indptr[i+1]; j++) {
                j_idx = A_indices[j];
                p += x[j_idx] * A_data[j];
            }
            grad_i = partial_gradient(p, b[i]);
            old_grad = memory_gradient[i];
            memory_gradient[i] = grad_i;
            incr = grad_i - old_grad;

            // .. update coefficients ..
            for (j=A_indptr[i]; j < A_indptr[i+1]; j++){
                j_idx = A_indices[j];
                delta = incr * A_data[j] + d[j_idx] * (gradient_average[j_idx] + alpha * x[j_idx]);
                x[j_idx] = prox(x[j_idx] - step_size * delta, beta * step_size * d[j_idx]);
                gradient_average[j_idx] += incr * A_data[j] / n_samples;
            }
            local_counter ++;
            if (local_counter >= iter_freq * max_iter){
                printf("..  done %lld iterations ..\n", local_counter / iter_freq);
                return;
            }
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2820, %rsp           # imm = 0x2820
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movsd %xmm2, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x1420(%rbp), %rdi
movq %rdi, -0x2808(%rbp)
callq 0xa6d0
movq -0x2808(%rbp), %rdi
callq 0xa540
movl %eax, -0x27fc(%rbp)
jmp 0xb7a1
movl -0x27fc(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x27a8(%rbp), %rdi
callq 0xa680
jmp 0xb7b9
movq 0x20(%rbp), %rdx
decq %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x27c8(%rbp), %rdi
callq 0xa2c0
jmp 0xb7d2
movl $0x1, %edi
leaq -0x27d8(%rbp), %rsi
callq 0xa7e0
jmp 0xb7e5
movq -0x68(%rbp), %rax
cqto
idivq 0x48(%rbp)
cmpq $0x0, %rdx
jne 0xb91d
movq -0x68(%rbp), %rax
cqto
idivq 0x48(%rbp)
movq %rax, -0x27f8(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0xb873
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x38(%rbp), %rax
movq 0x28(%rbp), %rcx
imulq -0x27f8(%rbp), %rcx
addq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb812
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x27b0(%rbp)
movl %eax, -0x27b4(%rbp)
leaq -0x1420(%rbp), %rdi
callq 0xa5d0
jmp 0xbba8
movl $0x1, %edi
leaq -0x27e8(%rbp), %rsi
callq 0xa7e0
movq -0x27e8(%rbp), %rax
movq -0x27d8(%rbp), %rcx
subq %rcx, %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x27f0(%rbp)
movq -0x27e0(%rbp), %rax
movq -0x27d0(%rbp), %rcx
subq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd 0x5740(%rip), %xmm0     # 0x11000
divsd %xmm0, %xmm1
movsd -0x27f0(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x27f0(%rbp)
movsd -0x27f0(%rbp), %xmm0
movq 0x40(%rbp), %rax
movq -0x27f8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x27f8(%rbp), %rsi
movsd -0x27f0(%rbp), %xmm0
movsd 0x5701(%rip), %xmm1     # 0x11008
divsd %xmm1, %xmm0
leaq 0x571a(%rip), %rdi      # 0x1102c
movb $0x1, %al
callq 0xa070
jmp 0xb91b
jmp 0xb91d
leaq -0x27c8(%rbp), %rdi
leaq -0x27a8(%rbp), %rsi
callq 0xa600
movq %rax, -0x2810(%rbp)
jmp 0xb939
movq -0x2810(%rbp), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0xb9b9
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x78(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb95c
movsd -0x78(%rbp), %xmm0
movq 0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0xa2a0
movsd %xmm0, -0x2818(%rbp)
jmp 0xb9da
movsd -0x2818(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x80(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x80(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0xbb50
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movsd -0x88(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x18(%rbp), %rax
movsd (%rax,%rcx,8), %xmm4
movsd -0x38(%rbp), %xmm3
movq -0x8(%rbp), %rax
movsd (%rax,%rcx,8), %xmm5
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd -0x48(%rbp), %xmm2
movsd -0x98(%rbp), %xmm3
movaps %xmm2, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
movsd -0x40(%rbp), %xmm1
mulsd %xmm2, %xmm1
movq 0x18(%rbp), %rax
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm1
callq 0xa530
movsd %xmm0, -0x2820(%rbp)
jmp 0xbaf9
movsd -0x2820(%rbp), %xmm0
movq -0x8(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x88(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
cvtsi2sdq 0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xba33
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq 0x48(%rbp), %rcx
imulq 0x30(%rbp), %rcx
cmpq %rcx, %rax
jl 0xbba3
movq -0x68(%rbp), %rax
movq 0x48(%rbp), %rcx
cqto
idivq %rcx
movq %rax, %rsi
leaq 0x54d2(%rip), %rdi      # 0x11057
xorl %eax, %eax
callq 0xa070
jmp 0xbb8e
leaq -0x1420(%rbp), %rdi
callq 0xa5d0
addq $0x2820, %rsp           # imm = 0x2820
popq %rbp
retq
jmp 0xb7e3
movq -0x27b0(%rbp), %rdi
callq 0xaf00
nopw %cs:(%rax,%rax)