// File: /workspace/github_c_makefile_star2_O0/zpole[P]IICV/vl/svm.c
// Function: vl_svm_sgd_train
void _vl_svm_sgd_train (VlSvm *self)
{
  vl_index * permutation ;
  double * scores ;
  double * previousScores ;
  vl_uindex i, t, k ;
  double inner, gradient, rate, biasRate, p ;
  double factor = 1.0 ;
  double biasFactor = 1.0 ; /* to allow slower bias learning rate */
  vl_index t0 = VL_MAX(2, vl_ceil_d(1.0 / self->lambda)) ;
  //t0=2 ;

  double startTime = vl_get_cpu_time () ;
  VlRand * rand = vl_get_rand() ;

  permutation = vl_calloc(self->numData, sizeof(vl_index)) ;
  scores = vl_calloc(self->numData * 2, sizeof(double)) ;
  previousScores = scores + self->numData ;

  for (i = 0 ; i < (unsigned)self->numData; i++) {
    permutation [i] = i ;
    previousScores [i] = - VL_INFINITY_D ;
  }

  /*
   We store the w vector as the product fw (factor * model).
   We also use a different factor for the bias: biasFactor * biasMultiplier
   to enable a slower learning rate for the bias.

   Given this representation, it is easy to carry the two key operations:

   * Inner product: <fw,x> = f <w,x>

   * Model update: fp wp = fw - rate * lambda * w - rate * g
                         = f(1 - rate * lambda) w - rate * g

     Thus the update equations are:

                   fp = f(1 - rate * lambda), and
                   wp = w + rate / fp * g ;

   * Realization of the scaling factor. Before the statistics function
     is called, or training finishes, the factor (and biasFactor)
     are explicitly applied to the model and the bias.
  */

  for (t = 0 ; 1 ; ++t) {

    if (t % self->numData == 0) {
      /* once a new epoch is reached (all data have been visited),
       change permutation */
      vl_rand_permute_indexes(rand, permutation, self->numData) ;
    }

    /* pick a sample and compute update */
    i = permutation[t % self->numData] ;
    p = (self->weights) ? self->weights[i] : 1.0 ;
    p = VL_MAX(0.0, p) ; /* we assume non-negative weights, so this is just for robustness */
    inner = factor * self->innerProductFn(self->data, i, self->model) ;
    inner += biasFactor * (self->biasMultiplier * self->bias) ;
    gradient = p * self->lossDerivativeFn(inner, self->labels[i]) ;
    previousScores[i] = scores[i] ;
    scores[i] = inner ;

    /* apply update */
    rate = 1.0 /  (self->lambda * (t + t0)) ;
    biasRate = rate * self->biasLearningRate ;
    factor *= (1.0 - self->lambda * rate) ;
    biasFactor *= (1.0 - self->lambda * biasRate) ;

    /* debug: realize the scaling factor all the times */
    /*
    for (k = 0 ; k < self->dimension ; ++k) self->model[k] *= factor ;
    self->bias *= biasFactor;
    factor = 1.0 ;
    biasFactor = 1.0 ;
    */

    if (gradient != 0) {
      self->accumulateFn(self->data, i, self->model, - gradient * rate / factor) ;
      self->bias += self->biasMultiplier * (- gradient * biasRate / biasFactor) ;
    }

    /* call diagnostic occasionally */
    if ((t + 1) % self->diagnosticFrequency == 0 || t + 1 == self->maxNumIterations) {

      /* realize factor before computing statistics or completing training */
      for (k = 0 ; k < self->dimension ; ++k) self->model[k] *= factor ;
      self->bias *= biasFactor;
      factor = 1.0 ;
      biasFactor = 1.0 ;

      _vl_svm_update_statistics (self) ;

      for (k = 0 ; k < self->numData ; ++k) {
        double delta = scores[k] - previousScores[k] ;
        self->statistics.scoresVariation += delta * delta ;
      }
      self->statistics.scoresVariation = sqrt(self->statistics.scoresVariation) / self->numData ;

      self->statistics.elapsedTime = vl_get_cpu_time() - startTime ;
      self->statistics.iteration = t ;
      self->statistics.epoch = t / self->numData ;

      self->statistics.status = VlSvmStatusTraining ;
      if (self->statistics.scoresVariation < self->epsilon) {
        self->statistics.status = VlSvmStatusConverged ;
      }
      else if (t + 1 == self->maxNumIterations) {
        self->statistics.status = VlSvmStatusMaxNumIterationsReached ;
      }

      if (self->diagnosticFn) {
        self->diagnosticFn(self, self->diagnosticFnData) ;
      }

      if (self->statistics.status != VlSvmStatusTraining) {
        break ;
      }
    }
  } /* next iteration */

  vl_free (scores) ;
  vl_free (permutation) ;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movsd 0x2611(%rip), %xmm0     # 0x590d8
movsd %xmm0, 0x60(%rsp)
movsd 0x2603(%rip), %xmm0     # 0x590d8
movsd %xmm0, 0x58(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x25ed(%rip), %xmm0     # 0x590d8
divsd 0x28(%rax), %xmm0
callq 0x57340
movq %rax, %rcx
movl $0x2, %eax
cmpq %rcx, %rax
jle 0x56b0e
movl $0x2, %eax
movq %rax, 0x30(%rsp)
jmp 0x56b2d
movq 0xc0(%rsp), %rax
movsd 0x25ba(%rip), %xmm0     # 0x590d8
divsd 0x28(%rax), %xmm0
callq 0x57340
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rsp)
callq 0x93b0
movsd %xmm0, 0x48(%rsp)
callq 0x9a20
movq %rax, 0x40(%rsp)
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %rdi
movl $0x8, %esi
callq 0x9a70
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %rdi
shlq %rdi
movl $0x8, %esi
callq 0x9a70
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0xc0(%rsp), %rcx
movq 0x38(%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq 0xc0(%rsp), %rcx
movq 0x38(%rcx), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x56c3e
movq 0xa0(%rsp), %rdx
movq 0xb8(%rsp), %rax
movq 0xa0(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movsd 0x3607(%rip), %xmm0     # 0x5a200
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x56bba
movq $0x0, 0x98(%rsp)
movb $0x1, %al
testb $0x1, %al
jne 0x56c55
jmp 0x57313
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
divq 0x38(%rcx)
cmpq $0x0, %rdx
jne 0x56c8f
movq 0x40(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %rdx
callq 0x9360
movq 0xb8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
divq 0x38(%rcx)
movq 0x28(%rsp), %rax
movq (%rax,%rdx,8), %rax
movq %rax, 0xa0(%rsp)
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x48(%rax)
je 0x56cf3
movq 0xc0(%rsp), %rax
movq 0x48(%rax), %rax
movq 0xa0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x56d03
movsd 0x23dd(%rip), %xmm0     # 0x590d8
movsd %xmm0, 0x20(%rsp)
jmp 0x56d03
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x68(%rsp), %xmm0
jbe 0x56d25
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x56d31
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xc0(%rsp), %rcx
movq 0x90(%rcx), %rax
movq 0x10(%rcx), %rdx
movq 0x30(%rcx), %rdi
movq 0xa0(%rsp), %rsi
callq *%rax
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x58(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movsd 0x18(%rax), %xmm1
movsd 0x20(%rax), %xmm2
mulsd %xmm1, %xmm2
movsd 0x88(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rcx
movq 0x80(%rax), %rax
movsd 0x88(%rsp), %xmm0
movq 0xa0(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm1
callq *%rax
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xb0(%rsp), %rax
movq 0xa0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax,%rcx,8)
movsd 0x88(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movq 0xa0(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0xc0(%rsp), %rax
movsd 0x28(%rax), %xmm1
movq 0x98(%rsp), %rax
movq 0x50(%rsp), %rcx
addq %rcx, %rax
movq %rax, %xmm0
movaps 0x22f4(%rip), %xmm2     # 0x59160
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x22f8(%rip), %xmm2     # 0x59170
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
mulsd %xmm0, %xmm1
movsd 0x2245(%rip), %xmm0     # 0x590d8
divsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
movq 0xc0(%rsp), %rax
mulsd 0x118(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x28(%rax), %xmm0
movsd 0x78(%rsp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x21e9(%rip), %xmm1     # 0x590d8
addsd %xmm1, %xmm0
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xc0(%rsp), %rax
movsd 0x28(%rax), %xmm0
movsd 0x70(%rsp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x21a3(%rip), %xmm1     # 0x590d8
addsd %xmm1, %xmm0
mulsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x80(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x56f5e
jp 0x56f5e
jmp 0x5700e
movq 0xc0(%rsp), %rax
movq 0x98(%rax), %rax
movq 0xc0(%rsp), %rcx
movq 0x30(%rcx), %rdi
movq 0xa0(%rsp), %rsi
movq 0xc0(%rsp), %rcx
movq 0x10(%rcx), %rdx
movsd 0x80(%rsp), %xmm0
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %r8 # imm = 0x8000000000000000
xorq %r8, %rcx
movq %rcx, %xmm0
mulsd 0x78(%rsp), %xmm0
divsd 0x60(%rsp), %xmm0
callq *%rax
movq 0xc0(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd 0x80(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
mulsd 0x70(%rsp), %xmm2
divsd 0x58(%rsp), %xmm2
movq 0xc0(%rsp), %rax
movsd 0x18(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rax)
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
divq 0x68(%rcx)
cmpq $0x0, %rdx
je 0x5704f
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq 0xc0(%rsp), %rcx
cmpq 0xa8(%rcx), %rax
jne 0x572f8
movq $0x0, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x570ab
movsd 0x60(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x90(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x5705b
movsd 0x58(%rsp), %xmm0
movq 0xc0(%rsp), %rax
mulsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
movsd 0x200d(%rip), %xmm0     # 0x590d8
movsd %xmm0, 0x60(%rsp)
movsd 0x1fff(%rip), %xmm0     # 0x590d8
movsd %xmm0, 0x58(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x56050
movq $0x0, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x57183
movq 0xb0(%rsp), %rax
movq 0x90(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xa8(%rsp), %rax
movq 0x90(%rsp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd 0x38(%rsp), %xmm2
movq 0xc0(%rsp), %rax
movsd 0x100(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rax)
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x570f8
movq 0xc0(%rsp), %rax
movsd 0x100(%rax), %xmm0
callq 0x9b20
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %xmm1
movaps 0x1fb4(%rip), %xmm2     # 0x59160
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x1fb8(%rip), %xmm2     # 0x59170
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0xc0(%rsp), %rax
movsd %xmm0, 0x100(%rax)
callq 0x93b0
subsd 0x48(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movsd %xmm0, 0x108(%rax)
movq 0x98(%rsp), %rcx
movq 0xc0(%rsp), %rax
movq %rcx, 0xc0(%rax)
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
divq 0x38(%rcx)
movq %rax, %rcx
movq 0xc0(%rsp), %rax
movq %rcx, 0xc8(%rax)
movq 0xc0(%rsp), %rax
movl $0x1, 0xb8(%rax)
movq 0xc0(%rsp), %rax
movsd 0x100(%rax), %xmm1
movq 0xc0(%rsp), %rax
movsd 0xb0(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x57281
movq 0xc0(%rsp), %rax
movl $0x2, 0xb8(%rax)
jmp 0x572b2
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq 0xc0(%rsp), %rcx
cmpq 0xa8(%rcx), %rax
jne 0x572b0
movq 0xc0(%rsp), %rax
movl $0x3, 0xb8(%rax)
jmp 0x572b2
movq 0xc0(%rsp), %rax
cmpq $0x0, 0x58(%rax)
je 0x572e3
movq 0xc0(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xc0(%rsp), %rdi
movq 0xc0(%rsp), %rcx
movq 0x60(%rcx), %rsi
callq *%rax
movq 0xc0(%rsp), %rax
cmpl $0x1, 0xb8(%rax)
je 0x572f6
jmp 0x57313
jmp 0x572f8
jmp 0x572fa
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x56c4a
movq 0xb0(%rsp), %rdi
callq 0x9620
movq 0xb8(%rsp), %rdi
callq 0x9620
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)