// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
andq $-0x40, %rsp
subq $0x300, %rsp            # imm = 0x300
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r14
callq 0x28dc
movq %rax, %rbx
movq %r14, %rdi
callq 0x2986
movq %rax, %r15
callq 0x2160
movq %rax, %r14
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
leaq 0x40(%rsp), %rdi
movq %r15, 0x18(%rsp)
movq %r15, %rsi
callq 0x2a52
callq 0x2160
subq %r14, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2c4e(%rip), %xmm0     # 0x5008
leaq 0x2c83(%rip), %rdi      # 0x5044
movb $0x1, %al
callq 0x2040
movq %r12, %rdi
callq 0x2b10
movq %rax, 0x8(%rsp)
movq %r12, %rdi
callq 0x2bda
movq %rax, 0x10(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x32d8
leaq 0x98(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
callq 0x2cae
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2431
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
leaq 0x98(%rsp), %rdi
callq 0x2d1c
callq 0x2160
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r12
movq %r12, %rax
shrq $0x3d, %rax
leaq (,%r12,8), %rcx
xorl %r15d, %r15d
negq %rax
sbbq %r15, %r15
orq %rcx, %r15
movq %r15, %rdi
callq 0x2030
movq %rax, %r14
movq %r15, %rdi
callq 0x2030
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2de0
callq 0x2160
subq %r13, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2b5b(%rip), %xmm0     # 0x5008
leaq 0x2b9a(%rip), %rdi      # 0x504e
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x2f34
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x22dc
leaq 0x98(%rsp), %rdi
callq 0x2ffc
leaq 0x40(%rsp), %rdi
callq 0x305c
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2538
jmp 0x2538
jmp 0x2538
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2548
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
jmp 0x2548
jmp 0x2533
jmp 0x2533
movq %rax, %rbx
jmp 0x2548
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2ffc
leaq 0x40(%rsp), %rdi
callq 0x305c
movq %rbx, %rdi
callq 0x21a0