// File: /workspace/github_c_makefile_star2_O2/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, %r12
leaq 0x1aa35(%rip), %rsi     # 0xa1b81
pushq $0x20
popq %rdi
pushq $0x4
popq %rdx
callq 0x34f59
movq %rax, %rbx
movq %r12, %rdi
movq %r14, 0x30(%rsp)
movq %r14, %rsi
movl %ebp, %edx
callq 0x86cbc
movq %rax, %r14
movq %rax, %rdi
callq 0xf2bf
andl $0x0, (%rbx)
movq %rax, %r13
movl 0x18(%rax), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x871aa
movq 0x10(%r13), %rsi
movsd (%rsi,%rdx,8), %xmm0
ucomisd 0xec41(%rip), %xmm0     # 0x95de0
jbe 0x871a5
incl %eax
movl %eax, (%rbx)
incq %rdx
jmp 0x87189
movslq %eax, %rdi
shlq $0x3, %rdi
leaq 0x1a9c9(%rip), %r15     # 0xa1b81
pushq $0xa
popq %rdx
movq %r15, %rsi
callq 0x34f59
movq %rax, 0x10(%rbx)
movslq (%rbx), %rdi
shlq $0x3, %rdi
pushq $0xb
popq %rdx
movq %r15, %rsi
callq 0x34f59
movsd 0xebff(%rip), %xmm2     # 0x95de0
movq %rax, 0x8(%rbx)
movq %rbx, 0x18(%rsp)
andq $0x0, 0x18(%rbx)
movsd 0xe321(%rip), %xmm0     # 0x95518
movsd %xmm0, 0x8(%rsp)
movsd 0xe30b(%rip), %xmm0     # 0x95510
movapd %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x14(%rsp)
xorl %ebx, %ebx
movslq 0x18(%r13), %rax
cmpq %rax, %r15
jge 0x873ca
movq 0x10(%r13), %rcx
movsd (%rcx,%r15,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x87270
movq %r12, %rdi
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0x2bfe3
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x18(%rsp), %rcx
movq 0x10(%rcx), %rax
movslq %ebx, %rbx
movsd %xmm1, (%rax,%rbx,8)
movq 0x8(%rcx), %rax
movq %r12, (%rax,%rbx,8)
incl %ebx
movl 0x18(%r13), %eax
movq 0x30(%r14), %rcx
movsd (%rcx,%r15,8), %xmm1
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x872aa
movq 0x28(%r14), %rdx
movq (%rdx,%r15,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq 0x10(%r13), %rdx
mulsd (%rdx,%rcx,8), %xmm0
addsd %xmm0, %xmm1
incq %rcx
jmp 0x87286
movq %r12, %rdi
movl %ebp, %esi
movsd %xmm1, (%rsp)
callq 0x2bfe3
movsd (%rsp), %xmm3
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm3
movq 0x10(%r13), %rax
movsd (%rax,%r15,8), %xmm0
movsd 0xeb08(%rip), %xmm2     # 0x95de0
ucomisd %xmm2, %xmm0
jbe 0x8730e
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x12638(%rip), %xmm1    # 0x99928
ucomisd %xmm0, %xmm1
jbe 0x8730e
movq 0x18(%rsp), %rax
addsd 0x18(%rax), %xmm3
movsd %xmm3, 0x18(%rax)
incl 0x14(%rsp)
jmp 0x873bd
ucomisd %xmm0, %xmm2
jbe 0x87369
movsd %xmm3, (%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x2bfe3
cmpl $-0x1, %eax
jne 0x87339
movsd (%rsp), %xmm0
maxsd 0x20(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x2bfe3
cmpl $0x1, %eax
jne 0x873b5
movsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
movsd 0xea81(%rip), %xmm2     # 0x95de0
jbe 0x873bd
movsd %xmm1, 0x8(%rsp)
jmp 0x873bd
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm1
addsd 0x125ad(%rip), %xmm1    # 0x99928
ucomisd %xmm1, %xmm0
jbe 0x873bd
movsd %xmm3, (%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x2bfe3
cmpl $0x1, %eax
jne 0x873a6
movsd (%rsp), %xmm0
maxsd 0x20(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x2bfe3
cmpl $-0x1, %eax
je 0x87348
movsd 0xea23(%rip), %xmm2     # 0x95de0
incq %r15
movq 0x10(%r12), %r12
jmp 0x87218
movl 0x14(%rsp), %eax
testl %eax, %eax
movsd 0x8(%rsp), %xmm0
jle 0x873ec
cvtsi2sd %eax, %xmm0
movq 0x18(%rsp), %rbx
movsd 0x18(%rbx), %xmm1
divsd %xmm0, %xmm1
jmp 0x87403
movapd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
mulsd 0xe922(%rip), %xmm1     # 0x95d20
movq 0x18(%rsp), %rbx
xorpd 0xedc5(%rip), %xmm1     # 0x961d0
movlpd %xmm1, 0x18(%rbx)
movq %r13, %rdi
callq 0xe94d
movq %r14, %rdi
callq 0xe917
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq