// File: /workspace/github_cpp_makefile_star2_O1/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 0x34ac
movq %r13, 0x18(%rbx)
movq %rbx, 0x10(%rsp)
movq %r14, 0x20(%rbx)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %rbp, %rsi
callq 0x2ae8
movq $0x0, 0x8(%rsp)
testq %r13, %r13
je 0x27fc
leaq -0x2(%r14), %rax
shrq $0x3f, %rax
addq %r14, %rax
addq $-0x2, %rax
sarq %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 %r14, 0x18(%rsp)
movq %r13, 0x58(%rsp)
movq %rbp, 0x50(%rsp)
testq %rbp, %rbp
je 0x26c0
movq 0x20(%rsp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %r12
sete %sil
leal (%rcx,%rsi), %edi
movw %di, (%rax,%rdx,2)
incq %rdx
addl %esi, %ecx
incl %ecx
cmpq %rdx, %rbp
jne 0x26a4
movq %r12, 0x60(%rsp)
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
cmpq $0x2, %r14
movq %r14, %r12
leaq 0x8(%rsp), %r14
jl 0x2714
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 0x28f7
movq %rbx, %rax
subq $0x1, %rax
movl $0x0, %ecx
cmovaeq %rax, %rcx
testq %rbx, %rbx
movq %rcx, %rbx
jne 0x26e2
leaq (,%r12,2), %rbx
addq %r13, %rbx
cmpq %rbp, %rbx
movq 0x60(%rsp), %r12
jae 0x2771
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 0x2768
movw %cx, (%rbx)
movzwl %ax, %ecx
movq %r13, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movq %r15, %r8
leaq 0x8(%rsp), %r9
callq 0x28f7
addq $0x2, %rbx
cmpq %rbp, %rbx
jb 0x272c
movq 0x18(%rsp), %r14
cmpq $0x2, %r14
leaq 0x8(%rsp), %rbp
jl 0x27ba
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 0x28f7
leaq -0x2(%rbx), %rax
cmpq $0x2, %rbx
movq %rax, %rbx
jg 0x2786
testq %r14, %r14
movq 0x50(%rsp), %rbp
je 0x27e6
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 0x21a0
incq %r12
movq %r12, 0x8(%rsp)
movq 0x58(%rsp), %r13
cmpq %r13, %r12
jne 0x2696
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x280b
callq 0x2150
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 0x2834
callq 0x2150
movq %rbx, %rdi
callq 0x21c0