// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/classification.c
// Function: test_knn
Prediction test_knn(Instanceptr data, void* model, void* parameters, double* posterior)
{
 /*!Last Changed 05.01.2009 added normalize_array*/
 /*!Last Changed 01.11.2005 added posteriors*/
 /*!Last Changed 25.10.2004*/
 /*!Last Changed 21.03.2002*/
 int *ccounts, j, classno, sum;
 Prediction predicted;
 Instanceptr *nearestneighbors, train;
 Knn_parameterptr p;
 p = (Knn_parameterptr) parameters;
 train = (Instanceptr) model;
 ccounts = safemalloc(current_dataset->classno * sizeof(int), "test_knn", 7);
 for (j = 0; j < current_dataset->classno; j++)
   ccounts[j] = 0;
 if (current_dataset->storetype == STORE_KERNEL)
   nearestneighbors = find_all_neighbors(train, data, &(p->nearcount));
 else
   nearestneighbors = find_nearest_neighbors(train, data, p->nearcount);
 predicted.knnloss[0] = 0;
 predicted.knnloss[1] = 0;
 predicted.knnloss[2] = p->nearcount;
 classno = give_classno(data);
 sum = 0;
 for (j = 0; j < p->nearcount; j++)
  {
   if (classno == give_classno(nearestneighbors[j]))
    {
     predicted.knnloss[0] += 1 / (j + 1.0);
     sum--;
     if (sum > 0)
       predicted.knnloss[2]++;
     else
       if (sum < 0)
         predicted.knnloss[2]--;
    }
   else
    {
     predicted.knnloss[0] -= 1 / (j + 1.0);
     sum++;
     if (sum > 0)
       predicted.knnloss[2]++;
     else
       if (sum < 0)
         predicted.knnloss[2]--;
    }
   predicted.knnloss[1] -= sum / (j + 1.0);
   ccounts[give_classno(nearestneighbors[j])]++;
  }
 predicted.classno = find_max_in_list(ccounts, current_dataset->classno);
 normalize_array(ccounts, posterior, current_dataset->classno);
 safe_free(nearestneighbors);
 safe_free(ccounts);
 return predicted;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, 0x30(%rsp)
leaq 0xc4e34(%rip), %rbx     # 0xcd870
movq (%rbx), %rax
movslq 0x10(%rax), %rdi
shlq $0x2, %rdi
leaq 0x8d436(%rip), %rsi     # 0x95e84
pushq $0x7
popq %rdx
callq 0x34f59
movq %rax, %r15
movq (%rbx), %rax
xorl %ecx, %ecx
movslq 0x10(%rax), %rdx
cmpq %rdx, %rcx
jge 0x8a71
andl $0x0, (%r15,%rcx,4)
incq %rcx
jmp 0x8a5e
cmpl $0x3, 0x140713c(%rax)
jne 0x8a8f
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x2d37f
jmp 0x8aa1
movq 0x18(%rsp), %rbx
movl (%rbx), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2d47a
movq %rax, %r13
cvtsi2sdl (%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbp, %rdi
callq 0x2b47e
movl %eax, %ebp
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
movslq (%rbx), %rax
cmpq %rax, %r14
jge 0x8bc9
movsd %xmm0, 0x10(%rsp)
movq (%r13,%r14,8), %rdi
callq 0x2b47e
leal 0x1(%r14), %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x8ca2c(%rip), %xmm1    # 0x95520
divsd %xmm0, %xmm1
cmpl %eax, %ebp
jne 0x8b23
addsd 0x10(%rsp), %xmm1
leal -0x1(%r12), %ebx
cmpl $0x2, %r12d
movsd %xmm1, 0x10(%rsp)
jl 0x8b47
movsd 0x8(%rsp), %xmm1
addsd 0x8c9ff(%rip), %xmm1    # 0x95520
jmp 0x8b81
movsd 0x10(%rsp), %xmm2
subsd %xmm1, %xmm2
leal 0x1(%r12), %ebx
testl %r12d, %r12d
js 0x8b51
movsd 0x8(%rsp), %xmm1
addsd 0x8c9db(%rip), %xmm1    # 0x95520
jmp 0x8b65
cmpl $0x1, %r12d
jne 0x8b73
xorl %ebx, %ebx
jmp 0x8b87
cmpl $-0x1, %r12d
je 0x8bc5
movsd 0x8(%rsp), %xmm1
addsd 0x8d24b(%rip), %xmm1    # 0x95db0
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
jmp 0x8b87
movsd 0x8(%rsp), %xmm1
addsd 0x8d22f(%rip), %xmm1    # 0x95db0
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %ebx, %xmm1
divsd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq (%r13,%r14,8), %rdi
callq 0x2b47e
cltq
incl (%r15,%rax,4)
incq %r14
movsd 0x10(%rsp), %xmm0
movl %ebx, %r12d
movq 0x18(%rsp), %rbx
jmp 0x8ac8
xorl %ebx, %ebx
jmp 0x8b6b
movq 0x30(%rsp), %rbx
movsd %xmm0, 0x20(%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%rbx)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x28(%rbx)
leaq 0xc4c80(%rip), %r14     # 0xcd870
movq (%r14), %rax
movl 0x10(%rax), %esi
movq %r15, %rdi
callq 0x336b5
movl %eax, 0x8(%rbx)
movq (%r14), %rax
movl 0x10(%rax), %edx
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x33b8f
movq %r13, %rdi
callq 0x34ff7
movq %r15, %rdi
callq 0x34ff7
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq