// File: cfiutak1[P]HPyC-KD-Tree/main.cpp
// Function: runNearestNeighbors(TrainingFileProcessor&, QueryFileProcessor&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void runNearestNeighbors(TrainingFileProcessor& training_file_processor, QueryFileProcessor& query_file_processor, std::string result_file_name) {
    TrainingFileData* training_file_data = training_file_processor.readTrainingFileHeader();
    alignas(32) float* training_points = training_file_processor.readPoints1D();

    auto build_start = std::chrono::steady_clock::now();
    hpyc::KDTree<float> tree(training_points, training_file_data->num_points, training_file_data->num_dimensions);
    auto build_end = std::chrono::steady_clock::now();

    std::chrono::duration<double> build_diff = (build_end - build_start);
    printf("build %f\n", build_diff.count());

    QueryFileData* query_file_data = query_file_processor.readQueryFileHeader();
    alignas(32) float** query_point = query_file_processor.readPointsRowCol();

    ResultsFileWriter writer(result_file_name, training_file_data, query_file_data);
    writer.writeFileHeader();

    auto query_start = std::chrono::steady_clock::now();

    std::size_t* indices = new std::size_t[query_file_data->num_neighbors];
    double* distances = new double[query_file_data->num_neighbors];

    tree.nearestNeighborsSearch(query_point[0], query_file_data->num_neighbors, indices, distances);

    auto query_end = std::chrono::steady_clock::now();
    std::chrono::duration<double> query_diff = (query_end - query_start);
    printf("query %f\n", query_diff.count());

    writer.writeQueryResults(tree, indices);


    cleanup(training_file_data, query_file_data, training_points, query_point);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
andq $-0x40, %rsp
subq $0x400, %rsp            # imm = 0x400
movq %rdx, 0x70(%rsp)
movq %rdi, 0x3e8(%rsp)
movq %rsi, 0x3e0(%rsp)
movq %rdx, 0x3d8(%rsp)
movq 0x3e8(%rsp), %rdi
callq 0x2c40
movq %rax, 0x3d0(%rsp)
movq 0x3e8(%rsp), %rdi
callq 0x2d20
movq %rax, 0x3c0(%rsp)
callq 0x2220
movq %rax, 0x3b8(%rsp)
movq 0x3c0(%rsp), %rsi
movq 0x3d0(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rcx
leaq 0x340(%rsp), %rdi
callq 0x2e10
callq 0x2220
movq %rax, 0x338(%rsp)
leaq 0x338(%rsp), %rdi
leaq 0x3b8(%rsp), %rsi
callq 0x2f30
movq %rax, 0x78(%rsp)
jmp 0x2581
movq 0x78(%rsp), %rax
movq %rax, 0x328(%rsp)
leaq 0x330(%rsp), %rdi
leaq 0x328(%rsp), %rsi
callq 0x2f80
jmp 0x25a5
leaq 0x330(%rsp), %rdi
callq 0x2fc0
movsd %xmm0, 0x68(%rsp)
jmp 0x25ba
movsd 0x68(%rsp), %xmm0
leaq 0x5a7d(%rip), %rdi      # 0x8044
movb $0x1, %al
callq 0x2050
jmp 0x25d0
movq 0x3e0(%rsp), %rdi
callq 0x2fe0
movq %rax, 0x60(%rsp)
jmp 0x25e4
movq 0x60(%rsp), %rax
movq %rax, 0x310(%rsp)
movq 0x3e0(%rsp), %rdi
callq 0x30e0
movq %rax, 0x58(%rsp)
jmp 0x2605
movq 0x70(%rsp), %rsi
movq 0x58(%rsp), %rax
movq %rax, 0x300(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x20c0
jmp 0x2626
movq 0x3d0(%rsp), %rdx
movq 0x310(%rsp), %rcx
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x31f0
jmp 0x264d
leaq 0xb0(%rsp), %rdi
callq 0x20f0
leaq 0xd0(%rsp), %rdi
callq 0x32a0
jmp 0x2669
callq 0x2220
movq %rax, 0xa8(%rsp)
movq 0x310(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, 0x50(%rsp)
jmp 0x26a4
movq 0x50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x310(%rsp), %rax
movq 0x18(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, 0x48(%rsp)
jmp 0x26df
movq 0x48(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x300(%rsp), %rax
movq (%rax), %rsi
movq 0x310(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0xa0(%rsp), %rcx
movq 0x98(%rsp), %r8
leaq 0x340(%rsp), %rdi
callq 0x3390
jmp 0x2722
callq 0x2220
movq %rax, 0x90(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x2f30
movq %rax, 0x40(%rsp)
jmp 0x274b
movq 0x40(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2f80
jmp 0x276f
leaq 0x88(%rsp), %rdi
callq 0x2fc0
movsd %xmm0, 0x38(%rsp)
jmp 0x2784
movsd 0x38(%rsp), %xmm0
leaq 0x58bd(%rip), %rdi      # 0x804e
movb $0x1, %al
callq 0x2050
jmp 0x279a
movq 0xa0(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
leaq 0x340(%rsp), %rsi
callq 0x34a0
jmp 0x27b9
movq 0x3d0(%rsp), %rdi
movq 0x310(%rsp), %rsi
movq 0x3c0(%rsp), %rdx
movq 0x300(%rsp), %rcx
callq 0x2400
leaq 0xd0(%rsp), %rdi
callq 0x3570
leaq 0x340(%rsp), %rdi
callq 0x35c0
movq %rbp, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
jmp 0x2857
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x20f0
jmp 0x2857
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x320(%rsp)
movl %eax, 0x31c(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x3570
leaq 0x340(%rsp), %rdi
callq 0x35c0
movq 0x320(%rsp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)