// File: /workspace/github_cpp_makefile_star2_O3/estan[P]tsp/tsp.cpp
// Function: createNeighborsMatrix(Matrix<unsigned int> const&, unsigned long)
Matrix<uint16_t> createNeighborsMatrix(const Matrix<uint32_t>& d, size_t K) {
    size_t N = d.rows();
    size_t M = d.cols() - 1;
    K = min(M, K);
    Matrix<uint16_t> neighbor(N, K);
    vector<uint16_t> row(M); // For sorting.

    for (size_t i = 0; i < N; ++i) {
        // Fill row with 0, 1, ..., i - 1, i + 1, ..., M - 1.
        uint16_t k = 0;
        for (size_t j = 0; j < M; ++j, ++k) {
            row[j] = (i == j) ? ++k : k;
        }
        // Sort K first elements in row by distance to i.
        partial_sort(row.begin(), row.begin() + K, row.end(),
            [&](uint16_t j, uint16_t k) {
                return d[i][j] < d[i][k];
            }
        );
        // Copy first K elements (now sorted) to neighbor matrix.
        copy(row.begin(), row.begin() + K, neighbor[i]);
    }
    return neighbor;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0x18(%rsi), %r13
movq 0x20(%rsi), %rbp
decq %rbp
cmpq %rdx, %rbp
cmovbq %rbp, %r14
movq %r14, %rsi
imulq %r13, %rsi
leaq 0x20(%rsp), %rdx
movw $0x0, (%rdx)
leaq 0x8(%rsp), %rcx
callq 0x35f0
movq %r13, 0x18(%rbx)
movq %rbx, 0x10(%rsp)
movq %r14, 0x20(%rbx)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %rbp, %rsi
callq 0x2b1a
movq $0x0, 0x8(%rsp)
testq %r13, %r13
je 0x2829
leaq -0x2(%r14), %rax
shrq %rax
movq %rax, 0x40(%rsp)
leaq (%r14,%r14), %rax
movq %rax, 0x48(%rsp)
leaq -0x2(,%r14,2), %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x50(%rsp)
movq %r14, 0x18(%rsp)
movq %r13, 0x58(%rsp)
movq 0x20(%rsp), %r13
testq %rbp, %rbp
movq %r12, 0x60(%rsp)
je 0x2709
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %r12
sete %dl
leal (%rax,%rdx), %esi
movw %si, (%r13,%rcx,2)
incq %rcx
addl %edx, %eax
incl %eax
cmpq %rcx, %rbp
jne 0x26e7
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
cmpq $0x2, %r14
movq %r14, %r12
leaq 0x8(%rsp), %r14
jl 0x2741
movq 0x40(%rsp), %rbx
movzwl (%r13,%rbx,2), %ecx
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %r8
movq %r14, %r9
callq 0x2926
addq $-0x1, %rbx
jb 0x2721
leaq (,%r12,2), %rbx
addq %r13, %rbx
cmpq %rbp, %rbx
movq 0x60(%rsp), %r12
jae 0x279e
movslq %r12d, %r14
movzwl (%rbx), %eax
movq 0x20(%r15), %rdx
imulq %r14, %rdx
shlq $0x2, %rdx
addq (%r15), %rdx
movzwl (%r13), %ecx
movl (%rdx,%rax,4), %esi
cmpl (%rdx,%rcx,4), %esi
jae 0x2795
movw %cx, (%rbx)
movzwl %ax, %ecx
movq %r13, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movq %r15, %r8
leaq 0x8(%rsp), %r9
callq 0x2926
addq $0x2, %rbx
cmpq %rbp, %rbx
jb 0x2759
movq 0x18(%rsp), %r14
cmpq $0x2, %r14
leaq 0x8(%rsp), %rbp
jl 0x27e7
movq 0x38(%rsp), %rbx
movzwl (%r13,%rbx), %ecx
movzwl (%r13), %eax
movw %ax, (%r13,%rbx)
movq %rbx, %rdx
sarq %rdx
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %r8
movq %rbp, %r9
callq 0x2926
leaq -0x2(%rbx), %rax
cmpq $0x2, %rbx
movq %rax, %rbx
jg 0x27b3
testq %r14, %r14
movq 0x50(%rsp), %rbp
je 0x2813
movslq %r12d, %rdi
movq 0x10(%rsp), %rax
imulq 0x20(%rax), %rdi
movq 0x20(%rsp), %rsi
addq %rdi, %rdi
addq (%rax), %rdi
movq 0x48(%rsp), %rdx
callq 0x2190
incq %r12
movq %r12, 0x8(%rsp)
movq 0x58(%rsp), %r13
cmpq %r13, %r12
jne 0x26d4
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2838
callq 0x2140
movq 0x10(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x2861
callq 0x2140
movq %rbx, %rdi
callq 0x21b0