// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r13
leaq 0x1bfa0(%rip), %rsi     # 0xaeba9
movl $0x20, %edi
movl $0x4, %edx
callq 0x390f5
movq %rax, %rbx
movq %r13, %rdi
movq %r14, 0x40(%rsp)
movq %r14, %rsi
movl %ebp, %edx
callq 0x92771
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0xfedb
movq %rax, %r15
movl $0x0, (%rbx)
movslq 0x18(%rax), %rax
testq %rax, %rax
jle 0x92c6f
movq 0x10(%r15), %rcx
xorl %edx, %edx
movsd 0x1017e(%rip), %xmm0    # 0xa2dd8
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x92c67
incl (%rbx)
incq %rdx
cmpq %rdx, %rax
jne 0x92c5a
movslq (%rbx), %rdi
shlq $0x3, %rdi
leaq 0x1bf2c(%rip), %r14     # 0xaeba9
movq %r14, %rsi
movl $0xa, %edx
callq 0x390f5
movq %rax, 0x10(%rbx)
movslq (%rbx), %rdi
shlq $0x3, %rdi
movq %r14, %rsi
movl $0xb, %edx
callq 0x390f5
movq %rax, 0x8(%rbx)
movq $0x0, 0x18(%rbx)
cmpl $0x0, 0x18(%r15)
jle 0x92e90
xorl %r12d, %r12d
movsd 0xf84c(%rip), %xmm1     # 0xa2510
movsd 0xf84c(%rip), %xmm0     # 0xa2518
movsd %xmm0, 0x18(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x14(%rsp)
movq 0x10(%r15), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd 0x100e9(%rip), %xmm0    # 0xa2dd8
movapd %xmm1, 0x20(%rsp)
jbe 0x92d30
movq %r13, %rdi
movl %ebp, %esi
movsd %xmm0, 0x8(%rsp)
callq 0x2f5e9
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x8(%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
movq 0x38(%rsp), %rcx
movq 0x30(%rcx), %rax
movsd (%rax,%r12,8), %xmm1
movslq 0x18(%r15), %rax
testq %rax, %rax
jle 0x92d6c
movq 0x28(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x10(%r15), %rdx
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm0
mulsd (%rdx,%rsi,8), %xmm0
addsd %xmm0, %xmm1
incq %rsi
cmpq %rsi, %rax
jne 0x92d56
movsd %xmm1, 0x8(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f5e9
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd 0x8(%rsp), %xmm2
movq 0x10(%r15), %rax
movsd (%rax,%r12,8), %xmm0
ucomisd 0x1003d(%rip), %xmm0    # 0xa2dd8
jbe 0x92dc8
movq 0x40(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x13be1(%rip), %xmm1    # 0xa6990
ucomisd %xmm0, %xmm1
jbe 0x92dc8
addsd 0x18(%rbx), %xmm2
movsd %xmm2, 0x18(%rbx)
incl 0x14(%rsp)
jmp 0x92e74
movsd 0x10008(%rip), %xmm1    # 0xa2dd8
ucomisd %xmm0, %xmm1
jbe 0x92e0e
movsd %xmm2, 0x8(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f5e9
cmpl $-0x1, %eax
jne 0x92dfd
movsd 0x8(%rsp), %xmm0
maxsd 0x20(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f5e9
cmpl $0x1, %eax
je 0x92e5c
jmp 0x92e74
movq 0x40(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x13b70(%rip), %xmm1    # 0xa6990
ucomisd %xmm1, %xmm0
jbe 0x92e74
movsd %xmm2, 0x8(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f5e9
cmpl $0x1, %eax
jne 0x92e4d
movsd 0x8(%rsp), %xmm0
maxsd 0x20(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2f5e9
cmpl $-0x1, %eax
jne 0x92e74
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x92e74
movsd %xmm1, 0x18(%rsp)
movapd 0x20(%rsp), %xmm1
incq %r12
movq 0x10(%r13), %r13
movslq 0x18(%r15), %rax
cmpq %rax, %r12
jl 0x92cdd
jmp 0x92eae
movsd 0xf680(%rip), %xmm0     # 0xa2518
movsd %xmm0, 0x18(%rsp)
movsd 0xf66a(%rip), %xmm1     # 0xa2510
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
testl %eax, %eax
jle 0x92ec8
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x18(%rbx), %xmm1
divsd %xmm0, %xmm1
jmp 0x92ed6
addsd 0x18(%rsp), %xmm1
mulsd 0xfe42(%rip), %xmm1     # 0xa2d18
xorpd 0x102f2(%rip), %xmm1    # 0xa31d0
movlpd %xmm1, 0x18(%rbx)
movq %r15, %rdi
callq 0xf3c8
movq 0x38(%rsp), %rdi
callq 0xf392
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq