// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xd8(%rbp)
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3630
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3920
decq %rax
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x3940
movq -0xd8(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x3980
movq -0x28(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x3a20
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0x48(%rbp), %rdi
callq 0x3a40
jmp 0x2895
leaq -0x49(%rbp), %rdi
callq 0x3ab0
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x2a80
movw $0x0, -0x6a(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x2953
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x28ef
movw -0x6a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x6a(%rbp)
movw %ax, -0xe2(%rbp)
jmp 0x28fa
movw -0x6a(%rbp), %ax
movw %ax, -0xe2(%rbp)
movw -0xe2(%rbp), %ax
movw %ax, -0xe4(%rbp)
movq -0x78(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x3ad0
movw -0xe4(%rbp), %cx
movw %cx, (%rax)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movw -0x6a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x6a(%rbp)
jmp 0x28c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x3ab0
jmp 0x2aaf
leaq -0x48(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x3af0
movq -0xf0(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x3af0
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x3b20
movq -0xf0(%rbp), %rdi
movq %rax, -0x88(%rbp)
callq 0x3b60
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0xa0(%rbp), %r8
callq 0x2ad0
jmp 0x29e1
leaq -0x48(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x3af0
movq -0x100(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x3af0
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x3b20
movq -0xd8(%rbp), %rdi
movq %rax, -0xb8(%rbp)
movl -0x68(%rbp), %esi
callq 0x3bf0
movq %rax, -0xf8(%rbp)
jmp 0x2a3a
movq -0xf8(%rbp), %rdx
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x3b90
jmp 0x2a56
jmp 0x2a58
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x28a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x3c20
jmp 0x2aaf
movb $0x1, -0x29(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x3c20
testb $0x1, -0x29(%rbp)
jne 0x2a9f
movq -0xd8(%rbp), %rdi
callq 0x3c80
movq -0xe0(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x3c80
movq -0x58(%rbp), %rdi
callq 0x21f0
nopw %cs:(%rax,%rax)