// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
leaq 0xc9ad1(%rip), %r14     # 0xd2870
movq (%r14), %rax
movslq 0x10(%rax), %rdi
shlq $0x2, %rdi
leaq 0x9a0cb(%rip), %rsi     # 0xa2e7c
movl $0x7, %edx
callq 0x390f5
movq %rax, %r15
movq (%r14), %rax
cmpl $0x0, 0x10(%rax)
jle 0x8ddd
xorl %ecx, %ecx
movl $0x0, (%r15,%rcx,4)
incq %rcx
movslq 0x10(%rax), %rdx
cmpq %rdx, %rcx
jl 0x8dc9
cmpl $0x3, 0x140713c(%rax)
jne 0x8df6
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x30b9c
jmp 0x8e05
movl (%r12), %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x30ca9
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
xorps %xmm0, %xmm0
cvtsi2sdl (%r12), %xmm0
movsd %xmm0, (%rsp)
movsd %xmm0, 0x30(%rbx)
movq %rbp, %rdi
callq 0x2eaca
movl %eax, 0x24(%rsp)
movq %rbx, 0x30(%rsp)
movsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
cmpl $0x0, (%r12)
jle 0x8f71
movq %r12, 0x10(%rsp)
xorps %xmm1, %xmm1
xorl %ebp, %ebp
xorl %r12d, %r12d
movsd %xmm1, 0x8(%rsp)
movq (%r13,%rbp,8), %rdi
callq 0x2eaca
leal 0x1(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0x996a7(%rip), %xmm1    # 0xa2520
divsd %xmm0, %xmm1
cmpl %eax, 0x24(%rsp)
jne 0x8ea9
addsd 0x8(%rsp), %xmm1
leal -0x1(%r12), %ebx
cmpl $0x2, %r12d
movsd %xmm1, 0x8(%rsp)
jl 0x8ed0
movsd (%rsp), %xmm0
addsd 0x99679(%rip), %xmm0    # 0xa2520
jmp 0x8f0b
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movapd %xmm0, %xmm1
leal 0x1(%r12), %ebx
testl %r12d, %r12d
js 0x8eda
movsd (%rsp), %xmm0
addsd 0x99652(%rip), %xmm0    # 0xa2520
jmp 0x8ef1
cmpl $0x1, %r12d
jne 0x8efe
xorl %ebx, %ebx
jmp 0x8f10
cmpl $-0x1, %r12d
je 0x8f6d
movsd (%rsp), %xmm0
addsd 0x99eb7(%rip), %xmm0    # 0xa2da8
movsd %xmm0, (%rsp)
movsd %xmm1, 0x8(%rsp)
jmp 0x8f10
movsd (%rsp), %xmm0
addsd 0x99e9d(%rip), %xmm0    # 0xa2da8
movsd %xmm0, (%rsp)
movq 0x10(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
leaq 0x1(%rbp), %r14
movl %r14d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq (%r13,%rbp,8), %rdi
callq 0x2eaca
cltq
incl (%r15,%rax,4)
movslq (%r12), %rax
movq %r14, %rbp
movl %ebx, %r12d
movsd 0x8(%rsp), %xmm0
movaps %xmm0, %xmm1
cmpq %rax, %r14
jl 0x8e56
jmp 0x8f74
xorl %ebx, %ebx
jmp 0x8ef6
xorps %xmm0, %xmm0
movq 0x30(%rsp), %rbx
movsd %xmm0, 0x20(%rbx)
movsd (%rsp), %xmm0
movsd %xmm0, 0x30(%rbx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x28(%rbx)
leaq 0xc98d6(%rip), %r14     # 0xd2870
movq (%r14), %rax
movl 0x10(%rax), %esi
movq %r15, %rdi
callq 0x37504
movl %eax, 0x8(%rbx)
movq (%r14), %rax
movl 0x10(%rax), %edx
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x37a34
movq %r13, %rdi
callq 0x39194
movq %r15, %rdi
callq 0x39194
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq