// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rdx, %rbp
movq %rsi, %r15
movq 0x18(%rsi), %rsi
movq 0x20(%r15), %rbx
decq %rbx
cmpq %rdx, %rbx
cmovbq %rbx, %rbp
movq %rdi, 0x8(%rsp)
movq %rsi, 0x58(%rsp)
movq %rbp, %rdx
callq 0x19d4
leaq 0x60(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rbx, %rsi
callq 0x1a12
leaq -0x2(%rbp), %rax
shrq %rax
movq %rax, 0x38(%rsp)
leaq (,%rbp,2), %rax
movq %rax, 0x20(%rsp)
leaq -0x2(,%rbp,2), %rax
movq %rax, 0x48(%rsp)
xorl %eax, %eax
leaq 0x10(%rsp), %r13
movq %rbp, 0x50(%rsp)
movq %r15, 0x18(%rsp)
movq %rbx, 0x40(%rsp)
movq %rax, 0x10(%rsp)
cmpq 0x58(%rsp), %rax
jae 0x16df
movq 0x60(%rsp), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rbx
je 0x15df
xorl %edi, %edi
cmpq %rdx, %rax
sete %dil
leal (%rsi,%rdi), %r8d
movw %r8w, (%rcx,%rdx,2)
incq %rdx
addl %edi, %esi
incl %esi
jmp 0x15bf
movq 0x60(%rsp), %r12
movq 0x68(%rsp), %r14
movq %r15, 0x78(%rsp)
movq %r13, 0x80(%rsp)
movq %r15, 0x28(%rsp)
movq %r13, 0x30(%rsp)
cmpq $0x2, %rbp
jl 0x162d
movq %r15, %rbx
movq 0x38(%rsp), %r15
movzwl (%r12,%r15,2), %ecx
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %r8
movq %r13, %r9
callq 0x17cb
addq $-0x1, %r15
jb 0x160e
leaq (%r12,%rbp,2), %rbp
movq %rbp, %r15
movq 0x40(%rsp), %rbx
cmpq %r14, %r15
jae 0x166b
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x179c
testb %al, %al
je 0x1665
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
leaq 0x28(%rsp), %rcx
callq 0x17a7
addq $0x2, %r15
jmp 0x1639
movq 0x48(%rsp), %rax
leaq (%r12,%rax), %r15
movq 0x20(%rsp), %r14
movq 0x50(%rsp), %rbp
cmpq $0x3, %r14
jl 0x16a1
movq %r12, %rdi
movq %r15, %rsi
movq %r15, %rdx
leaq 0x78(%rsp), %rcx
callq 0x17a7
addq $-0x2, %r15
addq $-0x2, %r14
jmp 0x167e
movq 0x10(%rsp), %rax
testq %rbp, %rbp
je 0x16d2
movslq %eax, %rdi
movq 0x8(%rsp), %rax
imulq 0x20(%rax), %rdi
movq 0x60(%rsp), %rsi
addq %rdi, %rdi
addq (%rax), %rdi
movq 0x20(%rsp), %rdx
callq 0x1160
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r15
incq %rax
jmp 0x15a6
leaq 0x60(%rsp), %rdi
callq 0x2066
movq 0x8(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x2066
movq %rbx, %rdi
callq 0x1190