// File: /workspace/github_cpp_makefile_star2_O2/arinaschwartz[P]KNN-MNIST/knn.cpp
// Function: KNN(MNISTDataset&, MNISTDataset&, unsigned int, unsigned int, bool)
uint32 KNN(MNISTDataset &trainSet, MNISTDataset &testSet, uint32 k, uint32 max, bool verbose)
{    
    uint32 training_length = trainSet.getNumImages();
    const MNISTImage* test_image;
    const MNISTImage* train_image;
    uint8 classifications[max];
    uint32 numCorrect = 0;

    
    for(uint32 i = 0; i < max; i++){
        test_image = testSet.getImage(i);
        priority_queue<distLabel, vector<distLabel>, greater<distLabel> > imgQueue;
        for(uint32 j = 0; j < training_length; j++){
            train_image = trainSet.getImage(j);
            double current_distance = (*test_image).distance(train_image);
            uint8 current_label = (*train_image).getLabel();
            distLabel current_image;
            current_image.first = current_distance;
            current_image.second = current_label;
            imgQueue.push(current_image);
        }
        //Counts label frequency, stores in array by index
        uint32 labelFreq[10];
        for(uint32 iter = 0; iter < k; iter++){
            distLabel neighbor = imgQueue.top();
            labelFreq[neighbor.second] += 1;
            imgQueue.pop();
        }
        uint8 prevailing_label = 0;
        uint8 highest_frequency = 0;
        //Picks the most frequent label
        for(uint8 check_label = 0; check_label < 10; check_label++){
            if(labelFreq[check_label] > highest_frequency){
                prevailing_label = check_label;
            }
            labelFreq[check_label] = 0;
        }
            
        classifications[i] = prevailing_label;
    }

    for(uint32 i = 0; i < max; i++){
        
        if(classifications[i] == (*testSet.getImage(i)).getLabel()){
            numCorrect++;
        }
    }

    return numCorrect;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %ecx, %ebx
movl %edx, -0x2c(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, -0x58(%rbp)
callq 0x293e
movl %eax, %r12d
movl %ebx, %eax
movq %rsp, %rcx
movq %rax, -0x48(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
xorl %ebx, %ebx
leaq -0x80(%rbp), %r14
cmpq -0x48(%rbp), %rbx
je 0x2e70
movq -0x40(%rbp), %rdi
movq %rbx, -0x50(%rbp)
movl %ebx, %esi
callq 0x2942
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
andq $0x0, -0x70(%rbp)
xorl %ebx, %ebx
cmpl %ebx, %r12d
je 0x2e0f
movq -0x58(%rbp), %rdi
movl %ebx, %esi
callq 0x2942
movq %rax, %r15
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x10(%rax)
movsd %xmm0, -0x60(%rbp)
movq %r15, %rdi
callq 0x2984
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
movq %r14, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2ed6
incl %ebx
jmp 0x2dbc
movl -0x2c(%rbp), %ebx
subl $0x1, %ebx
jb 0x2e30
movq -0x80(%rbp), %rax
movzbl 0x8(%rax), %eax
incl -0xb0(%rbp,%rax,4)
movq %r14, %rdi
callq 0x2eec
jmp 0x2e12
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x50(%rbp), %rbx
cmpq $0xa, %rax
je 0x2e59
cmpl $0x0, -0xb0(%rbp,%rax,4)
movzbl %cl, %ecx
cmovnel %eax, %ecx
andl $0x0, -0xb0(%rbp,%rax,4)
incq %rax
jmp 0x2e38
movq -0x38(%rbp), %rax
movb %cl, (%rax,%rbx)
movq %r14, %rdi
callq 0x2f0e
incq %rbx
jmp 0x2d92
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq %rbx, -0x48(%rbp)
je 0x2ea7
movq -0x38(%rbp), %rax
movzbl (%rax,%rbx), %r15d
movq -0x40(%rbp), %rdi
movl %ebx, %esi
callq 0x2942
movq %rax, %rdi
callq 0x2984
xorl %ecx, %ecx
cmpl %r15d, %eax
sete %cl
addl %ecx, %r14d
incq %rbx
jmp 0x2e75
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2ec1
jmp 0x2ec1
jmp 0x2ec1
jmp 0x2ec1
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2f0e
movq %rbx, %rdi
callq 0x2260
nop