// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %r15
callq 0x290c
movq %rax, %rbx
movq %r15, %rdi
callq 0x29b6
movq %rax, %r12
callq 0x2170
movq %rax, %r15
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
leaq 0x40(%rsp), %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
callq 0x2a7c
callq 0x2170
subq %r15, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2c25(%rip), %xmm0     # 0x5008
leaq 0x2c5a(%rip), %rdi      # 0x5044
movb $0x1, %al
callq 0x2040
movq %r14, %rdi
callq 0x2b3a
movq %rax, %r15
movq %r14, %rdi
callq 0x2c04
movq %rax, %r12
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x32f6
leaq 0x98(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2cd8
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2454
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2130
leaq 0x98(%rsp), %rdi
callq 0x2d46
callq 0x2170
movq %rax, %r13
movq 0x18(%r15), %rax
movq %rax, %rcx
shrq $0x3d, %rcx
shlq $0x3, %rax
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x2030
movq %rax, %r14
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
movq 0x18(%r15), %rcx
leaq (,%rcx,8), %rdx
cmpq %rax, %rcx
movq $-0x1, %rdi
cmovbeq %rdx, %rdi
callq 0x2030
movq (%r12), %rsi
movq 0x18(%r15), %rdx
leaq 0x40(%rsp), %rdi
movq %r14, %rcx
movq %rax, %r8
callq 0x2e0a
callq 0x2170
subq %r13, %rax
cvtsi2sd %rax, %xmm0
divsd 0x2b27(%rip), %xmm0     # 0x5008
leaq 0x2b66(%rip), %rdi      # 0x504e
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x2f5e
movq %rbx, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
callq 0x22ec
leaq 0x98(%rsp), %rdi
callq 0x301a
leaq 0x40(%rsp), %rdi
callq 0x307a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2568
jmp 0x2568
jmp 0x2568
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x2578
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2130
jmp 0x2578
jmp 0x2563
jmp 0x2563
movq %rax, %rbx
jmp 0x2578
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x301a
leaq 0x40(%rsp), %rdi
callq 0x307a
movq %rbx, %rdi
callq 0x21b0