// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/svm.c
// Function: sigmoid_train
void sigmoid_train(int l, double *dec_values, double *labels, double* A, double* B)
{
 double prior1 = 0, prior0 = 0, min_step = 1e-10, sigma = 1e-3, eps = 1e-5, hiTarget, loTarget, *t;
 double fApB, p, q, h11, h22, h21, g1, g2, det, dA, dB, gd, stepsize, newA, newB, newf, d1, d2, fval = 0.0;
 int i, max_iter = 100, iter;
 for (i = 0; i < l; i++)
   if (labels[i] > 0) 
     prior1 += 1;
   else 
     prior0 += 1;
 hiTarget = (prior1 + 1.0) / (prior1 + 2.0);
 loTarget = 1 / (prior0 + 2.0);
 t = safemalloc(l * sizeof(double), "sigmoid_train", 10);
 /* Initial Point and Initial Fun Value*/
 *A = 0.0; 
 *B = log((prior0 + 1.0) / (prior1 + 1.0));
 for (i = 0; i < l; i++)
  {
   if (labels[i] > 0) 
     t[i] = hiTarget;
   else 
     t[i] = loTarget;
   fApB = dec_values[i] * (*A) + (*B);
   if (fApB >= 0)
     fval += t[i] * fApB + log(1 + exp(-fApB));
   else
     fval += (t[i] - 1) * fApB + log(1 + exp(fApB));
  }
 for (iter = 0; iter < max_iter; iter++)
  {
   h11 = sigma; 
   h22 = sigma;
   h21 = 0.0;
   g1 = 0.0;
   g2 = 0.0;
   for (i = 0; i < l; i++)
    {
     fApB = dec_values[i] * (*A) + (*B);
     if (fApB >= 0)
      {
       p = exp(-fApB) / (1.0 + exp(-fApB));
       q = 1.0 / (1.0 + exp(-fApB));
      }
     else
      {
       p = 1.0 / (1.0 + exp(fApB));
       q = exp(fApB) / (1.0 + exp(fApB));
      }
     d2 = p * q;
     h11 += dec_values[i] * dec_values[i] * d2;
     h22 += d2;
     h21 += dec_values[i] * d2;
     d1 = t[i] - p;
     g1 += dec_values[i] * d1;
     g2 += d1;
    }
   if (fabs(g1) < eps && fabs(g2) < eps)
     break;
   /* Finding Newton direction: -inv(H') * g*/
   det = h11 * h22 - h21 * h21;
   dA = -(h22 * g1 - h21 * g2) / det;
   dB = -(-h21 * g1 + h11 * g2) / det;
   gd = g1 * dA + g2 * dB;
   stepsize = 1;   /* Line Search*/
   while (stepsize >= min_step)
    {
     newA = (*A) + stepsize * dA;
     newB = (*B) + stepsize * dB;
     /* New function value*/
     newf = 0.0;
     for (i = 0; i < l; i++)
      {
       fApB = dec_values[i] * newA + newB;
       if (fApB >= 0)
         newf += t[i] * fApB + log(1 + exp(-fApB));
       else
         newf += (t[i] - 1)*fApB + log(1 + exp(fApB));
      }
     /* Check sufficient decrease*/
     if (newf < fval + 0.0001 * stepsize * gd)
      {
       *A = newA;
       *B = newB;
       fval = newf;
       break;
      }
     else
       stepsize = stepsize / 2.0;
    }
   if (stepsize < min_step)
     break;
 }
 safe_free(t);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
xorl %eax, %eax
testl %edi, %edi
movl $0x0, %ebp
cmovgl %edi, %ebp
xorpd %xmm0, %xmm0
movsd 0x10dab(%rip), %xmm5    # 0x95520
xorpd %xmm6, %xmm6
xorpd %xmm1, %xmm1
movq 0x10(%rsp), %rcx
cmpq %rax, %rbp
je 0x847bc
movsd (%rcx,%rax,8), %xmm2
ucomisd %xmm0, %xmm2
ja 0x84796
addsd %xmm5, %xmm6
movapd %xmm1, %xmm3
addsd %xmm5, %xmm3
xorpd %xmm4, %xmm4
cmpltsd %xmm2, %xmm4
andpd %xmm4, %xmm3
andnpd %xmm1, %xmm4
orpd %xmm3, %xmm4
incq %rax
movapd %xmm4, %xmm1
jmp 0x84782
movapd %xmm1, %xmm2
addsd %xmm5, %xmm2
movapd %xmm2, (%rsp)
movsd 0x11877(%rip), %xmm0    # 0x96048
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movapd %xmm2, 0x60(%rsp)
addsd %xmm6, %xmm0
movapd %xmm5, %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movslq %edi, %rdi
shlq $0x3, %rdi
leaq 0x1d1f9(%rip), %rsi     # 0xa19f8
pushq $0xa
popq %rdx
movsd %xmm6, 0x30(%rsp)
callq 0x34f59
andq $0x0, (%r14)
movq %rax, %r12
movsd 0x30(%rsp), %xmm0
addsd 0x10cfe(%rip), %xmm0    # 0x95520
divsd (%rsp), %xmm0
callq 0x4160
movsd %xmm0, (%rbx)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x848fa
xorpd %xmm1, %xmm1
movq 0x10(%rsp), %rax
cmpltsd (%rax,%r13,8), %xmm1
movapd %xmm1, %xmm0
andpd 0x60(%rsp), %xmm0
andnpd 0x50(%rsp), %xmm1
orpd %xmm0, %xmm1
movlpd %xmm1, (%r12,%r13,8)
movsd (%r15,%r13,8), %xmm3
mulsd (%r14), %xmm3
addsd (%rbx), %xmm3
movapd %xmm3, %xmm4
xorpd 0x11945(%rip), %xmm4    # 0x961d0
ucomisd %xmm4, %xmm3
movapd %xmm1, %xmm2
addsd 0x11515(%rip), %xmm2    # 0x95db0
movapd %xmm3, %xmm0
jb 0x848a5
movapd %xmm4, %xmm0
cmpnlesd %xmm3, %xmm4
andpd %xmm4, %xmm2
andnpd %xmm1, %xmm4
orpd %xmm2, %xmm4
movapd %xmm4, 0x30(%rsp)
movapd %xmm3, (%rsp)
callq 0x4060
addsd 0x10c52(%rip), %xmm0    # 0x95520
callq 0x4160
movapd 0x30(%rsp), %xmm1
mulsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
incq %r13
jmp 0x8483d
movq $0x0, 0x48(%rsp)
movsd 0x10c15(%rip), %xmm1    # 0x95520
cmpl $0x64, 0x48(%rsp)
je 0x84c9a
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
xorl %r13d, %r13d
movsd 0x1337f(%rip), %xmm0    # 0x97ca8
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, %xmm5
cmpq %r13, %rbp
je 0x84a72
movapd %xmm5, 0x50(%rsp)
movapd %xmm4, 0x60(%rsp)
movapd %xmm3, 0x30(%rsp)
movsd (%r15,%r13,8), %xmm0
mulsd (%r14), %xmm0
addsd (%rbx), %xmm0
ucomisd 0x113b3(%rip), %xmm0    # 0x95d18
jae 0x8499d
movapd %xmm0, 0x10(%rsp)
callq 0x4060
movsd 0x10ba6(%rip), %xmm1    # 0x95520
addsd %xmm1, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movapd 0x10(%rsp), %xmm0
callq 0x4060
movapd %xmm0, 0x80(%rsp)
jmp 0x849e3
xorpd 0x1182b(%rip), %xmm0    # 0x961d0
movapd %xmm0, 0x10(%rsp)
callq 0x4060
movsd %xmm0, (%rsp)
movapd 0x10(%rsp), %xmm0
callq 0x4060
movsd 0x10b58(%rip), %xmm1    # 0x95520
addsd %xmm1, %xmm0
movsd (%rsp), %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movapd %xmm1, 0x80(%rsp)
movapd 0x10(%rsp), %xmm0
callq 0x4060
movsd 0x10b2a(%rip), %xmm2    # 0x95520
addsd %xmm2, %xmm0
movapd 0x80(%rsp), %xmm6
divsd %xmm0, %xmm6
movsd (%rsp), %xmm3
mulsd %xmm3, %xmm6
movsd (%r15,%r13,8), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm6, %xmm1
movapd 0x50(%rsp), %xmm5
addsd %xmm1, %xmm5
movapd 0x70(%rsp), %xmm1
addsd %xmm6, %xmm1
movapd %xmm1, 0x70(%rsp)
movsd (%r12,%r13,8), %xmm1
subsd %xmm3, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
unpcklpd %xmm1, %xmm6            # xmm6 = xmm6[0],xmm1[0]
mulpd %xmm0, %xmm6
movapd 0x60(%rsp), %xmm4
addpd %xmm6, %xmm4
movapd 0x30(%rsp), %xmm3
addsd %xmm1, %xmm3
incq %r13
movapd %xmm2, %xmm1
jmp 0x84933
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
movapd %xmm0, %xmm6
andpd 0x1175a(%rip), %xmm0    # 0x961e0
movsd 0x1537a(%rip), %xmm2    # 0x99e08
ucomisd %xmm0, %xmm2
jbe 0x84ab2
movapd %xmm3, %xmm0
andpd 0x11740(%rip), %xmm0    # 0x961e0
movsd 0x15360(%rip), %xmm2    # 0x99e08
ucomisd %xmm0, %xmm2
ja 0x84c9a
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
movapd %xmm3, %xmm7
mulsd %xmm5, %xmm7
movapd 0x70(%rsp), %xmm2
mulsd %xmm2, %xmm5
subsd %xmm0, %xmm5
movapd %xmm3, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm6, %xmm2
subsd %xmm0, %xmm2
movapd 0x116e8(%rip), %xmm0    # 0x961d0
xorpd %xmm0, %xmm2
divsd %xmm5, %xmm2
mulsd %xmm6, %xmm4
subsd %xmm4, %xmm7
xorpd %xmm0, %xmm7
divsd %xmm5, %xmm7
movapd %xmm7, 0x90(%rsp)
mulsd %xmm7, %xmm3
movapd %xmm2, 0x70(%rsp)
mulsd %xmm2, %xmm6
addsd %xmm3, %xmm6
movapd %xmm6, 0xa0(%rsp)
movapd %xmm1, %xmm2
ucomisd 0x1cdf0(%rip), %xmm2    # 0xa1920
jb 0x84c7c
movapd %xmm2, %xmm0
mulsd 0x70(%rsp), %xmm0
addsd (%r14), %xmm0
movsd %xmm0, 0x60(%rsp)
movapd %xmm2, %xmm0
mulsd 0x90(%rsp), %xmm0
addsd (%rbx), %xmm0
movsd %xmm0, 0x50(%rsp)
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movsd %xmm2, 0x80(%rsp)
cmpq %r13, %rbp
je 0x84c23
movsd %xmm1, 0x10(%rsp)
movsd (%r15,%r13,8), %xmm2
mulsd 0x60(%rsp), %xmm2
addsd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm1
xorpd 0x11631(%rip), %xmm1    # 0x961d0
ucomisd %xmm1, %xmm2
movsd (%r12,%r13,8), %xmm0
movapd %xmm0, %xmm3
addsd 0x111fb(%rip), %xmm3    # 0x95db0
jb 0x84bbb
movapd %xmm0, %xmm3
movsd %xmm3, 0x30(%rsp)
movapd %xmm1, %xmm0
cmpnlesd %xmm2, %xmm0
movapd %xmm2, %xmm4
movapd %xmm2, (%rsp)
movapd %xmm0, %xmm2
andpd %xmm4, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
callq 0x4060
addsd 0x10930(%rip), %xmm0    # 0x95520
callq 0x4160
movsd 0x30(%rsp), %xmm1
mulsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r13
movsd 0x80(%rsp), %xmm2
jmp 0x84b72
movapd %xmm2, %xmm0
mulsd 0x12c79(%rip), %xmm0    # 0x978a8
mulsd 0xa0(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x84c59
mulsd 0x110d4(%rip), %xmm2    # 0x95d20
movsd 0x108cc(%rip), %xmm1    # 0x95520
jmp 0x84b28
movsd 0x60(%rsp), %xmm0
movsd %xmm0, (%r14)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rbx)
movsd %xmm1, 0x28(%rsp)
movsd 0x108a4(%rip), %xmm1    # 0x95520
movq 0x48(%rsp), %rax
incl %eax
movq %rax, 0x48(%rsp)
movsd 0x1cc90(%rip), %xmm0    # 0xa1920
ucomisd %xmm2, %xmm0
jbe 0x8490b
movq %r12, %rdi
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x34ff7