// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/svmsimplex.c
// Function: solve_binary_svm
Svm_binary_modelptr solve_binary_svm(Instanceptr data, Svm_simplex_parameterptr parameters, int negative)
{
 int i, j, k, correct;
 Quadraticprogramptr program;
 Convexsolutionptr solution;
 Svm_binary_modelptr model;
 Instanceptr tmp;
 double sum, upper_bound = -INT_MAX, lower_bound = +INT_MAX;
 model = safemalloc(sizeof(Svm_binary_model), "solve_binary_svm", 4);
 program = prepare_qp_for_C_svm(data, parameters, negative);
 solution = solve_qp_wolfe_method(program);
 model->support_vector_count = 0;
 for (i = 0; i < solution->variablecount; i++)
   if (solution->variables[i] > DBL_DELTA)
     model->support_vector_count++;
 model->alpha_y = safemalloc(model->support_vector_count * sizeof(double), "solve_binary_svm", 10);
 model->support_vectors = safemalloc(model->support_vector_count * sizeof(Instanceptr), "solve_binary_svm", 11);
 j = 0;
 model->b = 0;
 correct = 0;
 for (i = 0, tmp = data; i < solution->variablecount; i++, tmp = tmp->next)
  {
   if (solution->variables[i] > DBL_DELTA)
    {
     model->alpha_y[j] = solution->variables[i] * svm_classno(tmp, negative);
     model->support_vectors[j] = tmp;
     j++;
    }
   sum = program->c[i];
   for (k = 0; k < solution->variablecount; k++)
     sum += program->Q.values[i][k] * solution->variables[k];
   sum *= svm_classno(tmp, negative);
   if (solution->variables[i] > DBL_DELTA && solution->variables[i] < (parameters->C - DBL_DELTA))
    {
     model->b += sum;
     correct++;
    }
   else
     if (solution->variables[i] < DBL_DELTA)
      {
       if (svm_classno(tmp, negative) == -1 && sum > upper_bound)
         upper_bound = sum;
       if (svm_classno(tmp, negative) == 1 && sum < lower_bound)
         lower_bound = sum;
      }
     else
       if (solution->variables[i] > (parameters->C - DBL_DELTA))
        {
         if (svm_classno(tmp, negative) == 1 && sum > upper_bound)
           upper_bound = sum;
         if (svm_classno(tmp, negative) == -1 && sum < lower_bound)
           lower_bound = sum;
        }
  }
 if (correct > 0)
   model->b /= correct;
 else
   model->b = (upper_bound + lower_bound) / 2;
 model->b *= -1;
 free_convex_solution(solution);
 free_quadratic_program(program);
 return model;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r13
leaq 0x1c4db(%rip), %rsi     # 0xb1c21
movl $0x20, %edi
movl $0x4, %edx
callq 0x3a0e1
movq %rax, %rbx
movq %r13, %rdi
movq %r14, 0x30(%rsp)
movq %r14, %rsi
movl %ebp, %edx
callq 0x9519f
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0xfef0
movq %rax, %r15
movl $0x0, (%rbx)
movslq 0x18(%rax), %rax
xorl %edi, %edi
testq %rax, %rax
jle 0x957b7
movq 0x10(%r15), %rcx
movsd 0x10641(%rip), %xmm0    # 0xa5dd8
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x957a8
incl %edi
movl %edi, (%rbx)
incq %rdx
cmpq %rdx, %rax
jne 0x95799
movslq %edi, %rdi
shlq $0x3, %rdi
leaq 0x1c463(%rip), %r14     # 0xb1c21
movq %r14, %rsi
movl $0xa, %edx
callq 0x3a0e1
movq %rax, 0x10(%rbx)
movslq (%rbx), %rdi
shlq $0x3, %rdi
movq %r14, %rsi
movl $0xb, %edx
callq 0x3a0e1
movq %rax, 0x8(%rbx)
movq $0x0, 0x18(%rbx)
movl 0x18(%r15), %eax
testl %eax, %eax
jle 0x959e2
xorl %r12d, %r12d
movsd 0xfd0a(%rip), %xmm0     # 0xa5510
movsd %xmm0, 0x8(%rsp)
movsd 0xfd04(%rip), %xmm1     # 0xa5518
xorl %r14d, %r14d
movl $0x0, 0x14(%rsp)
movq 0x10(%r15), %rcx
movsd (%rcx,%r12,8), %xmm0
ucomisd 0x105a7(%rip), %xmm0    # 0xa5dd8
movapd %xmm1, 0x20(%rsp)
jbe 0x95874
movq %r13, %rdi
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0x2ff28
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x10(%rbx), %rax
movslq %r14d, %r14
movsd %xmm1, (%rax,%r14,8)
movq 0x8(%rbx), %rax
movq %r13, (%rax,%r14,8)
incl %r14d
movl 0x18(%r15), %eax
movq 0x18(%rsp), %rdx
movq 0x30(%rdx), %rcx
movsd (%rcx,%r12,8), %xmm1
testl %eax, %eax
jle 0x958ad
movq 0x28(%rdx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x10(%r15), %rdx
movl %eax, %eax
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm0
mulsd (%rdx,%rsi,8), %xmm0
addsd %xmm0, %xmm1
incq %rsi
cmpq %rsi, %rax
jne 0x95897
movsd %xmm1, (%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2ff28
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd (%rsp), %xmm2
movq 0x10(%r15), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd 0x104fe(%rip), %xmm0    # 0xa5dd8
jbe 0x95907
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x14112(%rip), %xmm1    # 0xa9a00
ucomisd %xmm0, %xmm1
jbe 0x95907
addsd 0x18(%rbx), %xmm2
movsd %xmm2, 0x18(%rbx)
incl 0x14(%rsp)
jmp 0x959ae
movsd 0x104c9(%rip), %xmm1    # 0xa5dd8
ucomisd %xmm0, %xmm1
jbe 0x9594b
movsd %xmm2, (%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2ff28
cmpl $-0x1, %eax
jne 0x9593a
movsd (%rsp), %xmm0
maxsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2ff28
cmpl $0x1, %eax
je 0x95997
jmp 0x959ae
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x140a3(%rip), %xmm1    # 0xa9a00
ucomisd %xmm1, %xmm0
jbe 0x959ae
movsd %xmm2, (%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2ff28
cmpl $0x1, %eax
jne 0x95988
movsd (%rsp), %xmm0
maxsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2ff28
cmpl $-0x1, %eax
jne 0x959ae
movapd 0x20(%rsp), %xmm1
movsd (%rsp), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x959b4
movapd %xmm0, %xmm1
jmp 0x959b4
movapd 0x20(%rsp), %xmm1
incq %r12
movq 0x10(%r13), %r13
movslq 0x18(%r15), %rax
cmpq %rax, %r12
jl 0x9581f
movl 0x14(%rsp), %eax
testl %eax, %eax
jle 0x959f8
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x18(%rbx), %xmm1
divsd %xmm0, %xmm1
jmp 0x95a06
movsd 0xfb26(%rip), %xmm0     # 0xa5510
movsd %xmm0, 0x8(%rsp)
movsd 0xfb20(%rip), %xmm1     # 0xa5518
addsd 0x8(%rsp), %xmm1
mulsd 0x10312(%rip), %xmm1    # 0xa5d18
xorpd 0x107c2(%rip), %xmm1    # 0xa61d0
movlpd %xmm1, 0x18(%rbx)
movq %r15, %rdi
callq 0xf3e0
movq 0x18(%rsp), %rdi
callq 0xf3aa
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq