// File: /workspace/github_cpp_makefile_star2_O3/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, -0x30(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, -0x78(%rbp)
callq 0x2a8e
movl %eax, %r13d
movl %ebx, %eax
movq %rsp, %r14
movq %rax, -0x38(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
testl %ebx, %ebx
je 0x311b
movl %ebx, -0x2c(%rbp)
xorl %esi, %esi
leaq -0x70(%rbp), %rbx
movq %r14, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movq %rsi, -0x50(%rbp)
callq 0x2a92
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
testl %r13d, %r13d
je 0x305a
movq %rax, %r14
xorl %r15d, %r15d
movq -0x78(%rbp), %rdi
movl %r15d, %esi
callq 0x2a92
movq %rax, %r12
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x10(%rax)
movsd %xmm0, -0x80(%rbp)
movq %r12, %rdi
callq 0x2b60
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movb %al, -0xa8(%rbp)
movq %rbx, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3156
incl %r15d
cmpl %r15d, %r13d
jne 0x3008
movl -0x30(%rbp), %eax
movl %eax, %r14d
testl %eax, %eax
je 0x3080
movq -0x70(%rbp), %rax
movzbl 0x8(%rax), %eax
incl -0xb0(%rbp,%rax,4)
movq %rbx, %rdi
callq 0x31b6
decl %r14d
jne 0x3064
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 0x3084
movq -0x48(%rbp), %r14
movq -0x50(%rbp), %rsi
movb %cl, (%r14,%rsi)
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x30cb
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq -0x50(%rbp), %rsi
incq %rsi
cmpq -0x38(%rbp), %rsi
jne 0x2fe1
cmpl $0x0, -0x2c(%rbp)
je 0x311b
xorl %r14d, %r14d
xorl %ebx, %ebx
movq -0x40(%rbp), %r12
movq -0x38(%rbp), %r13
movq -0x48(%rbp), %rax
movzbl (%rax,%r14), %r15d
movq %r12, %rdi
movl %r14d, %esi
callq 0x2a92
movq %rax, %rdi
callq 0x2b60
xorl %ecx, %ecx
cmpl %r15d, %eax
sete %cl
addl %ecx, %ebx
incq %r14
cmpq %r14, %r13
jne 0x30eb
jmp 0x311d
xorl %ebx, %ebx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3136
jmp 0x3136
jmp 0x3136
jmp 0x3136
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x314e
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x2280