// File: /workspace/github_cpp_makefile_star2_O3/dsharlet[P]array/include/array/array.h
// Function: auto nda::internal::dynamic_optimize_copy_shapes<nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<0l, 1l, 1l>>, nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<0l, 1l, 1l>>, void>(nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<0l, 1l, 1l>> const&, nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<0l, 1l, 1l>> const&)
NDARRAY_HOST_DEVICE auto dynamic_optimize_copy_shapes(const ShapeSrc& src, const ShapeDst& dst) {
  constexpr size_t rank = ShapeSrc::rank();
  static_assert(rank == ShapeDst::rank(), "copy shapes must have same rank.");
  auto src_dims = internal::tuple_to_array<dim<>>(src.dims());
  auto dst_dims = internal::tuple_to_array<dim<>>(dst.dims());

  std::array<copy_dims, rank> dims;
  for (size_t i = 0; i < rank; i++) {
    dims[i] = {src_dims[i], dst_dims[i]};
  }

  // Sort the dims by the dst stride.
  bubble_sort(dims.begin(), dims.end());

  // Find dimensions that are contiguous and fuse them.
  size_t new_rank = dims.size();
  for (size_t i = 0; i + 1 < new_rank;) {
    if (dims[i].src.extent() == dims[i].dst.extent() && can_fuse(dims[i].src, dims[i + 1].src) &&
        can_fuse(dims[i].dst, dims[i + 1].dst)) {
      dims[i].src = fuse(dims[i].src, dims[i + 1].src);
      dims[i].dst = fuse(dims[i].dst, dims[i + 1].dst);
      for (size_t j = i + 1; j + 1 < new_rank; j++) {
        dims[j] = dims[j + 1];
      }
      new_rank--;
    } else {
      i++;
    }
  }

  // Unfortunately, we can't make the rank of the resulting shape dynamic. Fill
  // the end of the array with size 1 dimensions.
  for (size_t i = new_rank; i < dims.size(); i++) {
    dims[i] = {dim<>(0, 1, 0), dim<>(0, 1, 0)};
  }

  for (size_t i = 0; i < dims.size(); i++) {
    src_dims[i] = dims[i].src;
    dst_dims[i] = dims[i].dst;
  }

  return std::make_pair(
      shape_of_rank<rank>(array_to_tuple(src_dims)), shape_of_rank<rank>(array_to_tuple(dst_dims)));
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rax
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
movups 0x20(%rsi), %xmm0
leaq 0xa8(%rsp), %rcx
movaps %xmm0, -0x18(%rcx)
movl $0x1, %r8d
movq %r8, -0x8(%rcx)
movups 0x8(%rsi), %xmm0
movups %xmm0, (%rcx)
movq 0x18(%rsi), %rsi
movq %rsi, 0x10(%rcx)
xorl %r9d, %r9d
movq %r9, 0x18(%rcx)
movq %r8, 0x20(%rcx)
movq %r8, 0x28(%rcx)
movups 0x20(%rdx), %xmm0
leaq 0x58(%rsp), %rsi
movaps %xmm0, -0x18(%rsi)
movq %r8, -0x8(%rsi)
movups 0x8(%rdx), %xmm0
movups %xmm0, (%rsi)
movq 0x18(%rdx), %rdx
movq %rdx, 0x10(%rsi)
movq %r9, 0x18(%rsi)
movq %r8, 0x20(%rsi)
movq %r8, 0x28(%rsi)
movl $0x28, %edx
xorps %xmm0, %xmm0
movups %xmm0, -0x78(%rsp,%rdx)
movq %rdi, -0x68(%rsp,%rdx)
movups %xmm0, -0x60(%rsp,%rdx)
movq %rdi, -0x50(%rsp,%rdx)
addq $0x30, %rdx
cmpq $0xb8, %rdx
jne 0x63dd0
leaq -0x68(%rsp), %rdx
xorl %edi, %edi
movq 0xa0(%rsp,%rdi), %r8
movq %r8, -0x70(%rsp)
movups 0x90(%rsp,%rdi), %xmm0
movaps %xmm0, -0x80(%rsp)
movq 0x50(%rsp,%rdi), %r8
movq %r8, 0x10(%rdx)
movups 0x40(%rsp,%rdi), %xmm0
movups %xmm0, (%rdx)
movaps -0x80(%rsp), %xmm0
movaps -0x70(%rsp), %xmm1
movaps -0x60(%rsp), %xmm2
movups %xmm0, -0x50(%rsp,%rdi,2)
movups %xmm1, -0x40(%rsp,%rdi,2)
movups %xmm2, -0x30(%rsp,%rdi,2)
addq $0x18, %rdi
cmpq $0x48, %rdi
jne 0x63df8
xorl %edx, %edx
leaq (%rsp,%rdx), %rdi
addq $-0x50, %rdi
movq %rdx, %r8
movq -0x28(%rsp,%r8), %r9
cmpq -0x28(%rsp,%rdx), %r9
jge 0x63ebc
leaq (%rsp,%r8), %r9
addq $-0x50, %r9
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups 0x20(%rdi), %xmm2
movaps %xmm2, -0x60(%rsp)
movaps %xmm1, -0x70(%rsp)
movaps %xmm0, -0x80(%rsp)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rdi)
movups %xmm1, 0x10(%rdi)
movups %xmm0, (%rdi)
movaps -0x80(%rsp), %xmm0
movaps -0x70(%rsp), %xmm1
movaps -0x60(%rsp), %xmm2
movups %xmm2, 0x20(%r9)
movups %xmm1, 0x10(%r9)
movups %xmm0, (%r9)
addq $0x30, %r8
cmpq $0x90, %r8
jne 0x63e58
addq $0x30, %rdx
cmpq $0x90, %rdx
jne 0x63e4d
movl $0x3, %edx
movl $0x1, %r8d
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
shlq $0x4, %r9
movq -0x48(%rsp,%r9), %r10
cmpq -0x30(%rsp,%r9), %r10
jne 0x63f98
leaq (%rsp,%r9), %r11
addq $-0x50, %r11
leaq (%r8,%r8,2), %r9
shlq $0x4, %r9
movq 0x10(%r11), %rbx
imulq %r10, %rbx
cmpq -0x40(%rsp,%r9), %rbx
jne 0x63f98
addq %rsp, %r9
addq $-0x50, %r9
movq 0x28(%r11), %rbx
imulq %r10, %rbx
cmpq 0x28(%r9), %rbx
jne 0x63f98
movq (%r9), %rbx
imulq %r10, %rbx
movq 0x8(%r9), %r14
imulq %r10, %r14
addq %rbx, (%r11)
movq %r14, 0x8(%r11)
movq 0x18(%r9), %rbx
imulq %r10, %rbx
imulq 0x20(%r9), %r10
addq %rbx, 0x18(%r11)
movq %r10, 0x20(%r11)
leaq 0x1(%r8), %r10
cmpq %rdx, %r10
jae 0x63f93
notq %r8
addq %rdx, %r8
movups 0x30(%r9), %xmm0
movups 0x40(%r9), %xmm1
movups 0x50(%r9), %xmm2
movups %xmm2, 0x20(%r9)
movups %xmm1, 0x10(%r9)
movups %xmm0, (%r9)
addq $0x30, %r9
decq %r8
jne 0x63f6d
decq %rdx
jmp 0x63f9b
movq %r8, %rdi
leaq 0x1(%rdi), %r8
cmpq %rdx, %r8
jb 0x63ee7
cmpq $0x2, %rdx
ja 0x63fe9
leaq (%rdx,%rdx,2), %rdi
addq $-0x3, %rdx
shlq $0x4, %rdi
addq %rsp, %rdi
addq $-0x28, %rdi
xorl %r8d, %r8d
movl $0x1, %r9d
xorps %xmm0, %xmm0
movq %r8, -0x28(%rdi)
movq %r9, -0x20(%rdi)
movups %xmm0, -0x18(%rdi)
movq %r9, -0x8(%rdi)
movq %r8, (%rdi)
addq $0x30, %rdi
incq %rdx
jne 0x63fcd
xorl %edx, %edx
movq -0x40(%rsp,%rdx,2), %rdi
movq %rdi, 0xa0(%rsp,%rdx)
movups -0x50(%rsp,%rdx,2), %xmm0
movups %xmm0, 0x90(%rsp,%rdx)
movups -0x38(%rsp,%rdx,2), %xmm0
movups %xmm0, 0x40(%rsp,%rdx)
movq -0x28(%rsp,%rdx,2), %rdi
movq %rdi, 0x50(%rsp,%rdx)
addq $0x18, %rdx
cmpq $0x48, %rdx
jne 0x63feb
movq 0x28(%rcx), %rdx
movq %rdx, 0x10(%rax)
movups 0x18(%rcx), %xmm0
movups %xmm0, (%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x28(%rax)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rax)
movq 0xa0(%rsp), %rcx
movq %rcx, 0x40(%rax)
movaps 0x90(%rsp), %xmm0
movups %xmm0, 0x30(%rax)
movq 0x28(%rsi), %rcx
movq %rcx, 0x58(%rax)
movups 0x18(%rsi), %xmm0
movups %xmm0, 0x48(%rax)
movups (%rsi), %xmm0
movups %xmm0, 0x60(%rax)
movq 0x10(%rsi), %rcx
movq %rcx, 0x70(%rax)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0x78(%rax)
movq 0x50(%rsp), %rcx
movq %rcx, 0x88(%rax)
addq $0xd8, %rsp
popq %rbx
popq %r14
retq