// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
callq 0x275e
movq %rax, %r14
movq %rbx, %rdi
callq 0x2804
movq %rax, %r15
callq 0x2150
movq %rax, %rbx
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
leaq 0x40(%rsp), %rdi
movq %r15, 0x18(%rsp)
movq %r15, %rsi
callq 0x28c0
callq 0x2150
subq %rbx, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2c48(%rip), %xmm0     # 0x5008
leaq 0x2c7d(%rip), %rdi      # 0x5044
movb $0x1, %al
callq 0x2040
movq %r12, %rdi
callq 0x2980
movq %rax, %r15
movq %r12, %rdi
callq 0x2a40
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
leaq 0x98(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2b00
movq %r14, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x20a0
leaq 0x98(%rsp), %rdi
callq 0x2b50
callq 0x2150
movq %rax, %r13
movq 0x18(%r15), %r12
movq %r12, %rax
shrq $0x3d, %rax
leaq (,%r12,8), %rcx
xorl %ebx, %ebx
negq %rax
sbbq %rbx, %rbx
orq %rcx, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r14
movq %rbx, %rdi
callq 0x2030
movq 0x8(%rsp), %rcx
movq (%rcx), %rsi
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2c10
callq 0x2150
subq %r13, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2b78(%rip), %xmm0     # 0x5008
leaq 0x2bb7(%rip), %rdi      # 0x504e
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x2cbe
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x22ec
leaq 0x98(%rsp), %rdi
callq 0x2d58
leaq 0x40(%rsp), %rdi
callq 0x2d8a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x250e
jmp 0x250e
jmp 0x250e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x20a0
jmp 0x251e
jmp 0x2509
jmp 0x2509
movq %rax, %rbx
jmp 0x251e
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2d58
leaq 0x40(%rsp), %rdi
callq 0x2d8a
movq %rbx, %rdi
callq 0x21b0