// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, -0x80(%rbp)
callq 0x2a92
movl %eax, %r12d
movl %ebx, %eax
movq %rsp, %rcx
movq %rax, -0x40(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
testl %ebx, %ebx
je 0x3127
movq %rcx, -0x38(%rbp)
xorl %esi, %esi
leaq -0x70(%rbp), %rbx
movq %r13, -0x78(%rbp)
movq %r13, %rdi
movq %rsi, -0x48(%rbp)
callq 0x2a96
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
testl %r12d, %r12d
je 0x3070
movq %rax, %r13
xorl %r14d, %r14d
movq -0x80(%rbp), %rdi
movl %r14d, %esi
callq 0x2a96
movq %rax, %r15
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x10(%rax)
movsd %xmm0, -0x88(%rbp)
movq %r15, %rdi
callq 0x2b5c
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3162
incl %r14d
cmpl %r14d, %r12d
jne 0x3017
movl -0x2c(%rbp), %eax
movl %eax, %r14d
testl %eax, %eax
movq -0x78(%rbp), %r13
je 0x309a
movq -0x70(%rbp), %rax
movzbl 0x8(%rax), %eax
incl -0xb0(%rbp,%rax,4)
movq %rbx, %rdi
callq 0x31b8
decl %r14d
jne 0x307e
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x0, -0xb0(%rbp,%rax,4)
movzbl %cl, %ecx
cmovnel %eax, %ecx
movl $0x0, -0xb0(%rbp,%rax,4)
incq %rax
cmpq $0xa, %rax
jne 0x309e
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rsi
movb %cl, (%rax,%rsi)
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x30e4
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq -0x48(%rbp), %rsi
incq %rsi
cmpq -0x40(%rbp), %rsi
jne 0x2ff1
xorl %r14d, %r14d
xorl %ebx, %ebx
movq -0x38(%rbp), %r12
movzbl (%r12,%r14), %r15d
movq %r13, %rdi
movl %r14d, %esi
callq 0x2a96
movq %rax, %rdi
callq 0x2b5c
xorl %ecx, %ecx
cmpl %r15d, %eax
sete %cl
addl %ecx, %ebx
incq %r14
cmpq %r14, -0x40(%rbp)
jne 0x30fa
jmp 0x3129
xorl %ebx, %ebx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3142
jmp 0x3142
jmp 0x3142
jmp 0x3142
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x315a
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280