// File: /workspace/github_cpp_makefile_star2_O2/dsharlet[P]array/include/array/array.h
// Function: auto nda::internal::dynamic_optimize_copy_shapes<nda::shape<nda::dim<-9l, 4l, -9l>, nda::dim<-9l, 3l, 1l>>, nda::shape<nda::dim<-9l, 4l, -9l>, nda::dim<-9l, 3l, 1l>>, void>(nda::shape<nda::dim<-9l, 4l, -9l>, nda::dim<-9l, 3l, 1l>> const&, nda::shape<nda::dim<-9l, 4l, -9l>, nda::dim<-9l, 3l, 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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movq %rdi, %rbx
movq 0x10(%rsi), %rax
leaq 0x48(%rsp), %r15
movq %rax, -0x18(%r15)
pushq $0x4
popq %rax
movq %rax, -0x10(%r15)
movq 0x20(%rsi), %rcx
movq %rcx, -0x8(%r15)
movq (%rsi), %rcx
movq %rcx, (%r15)
pushq $0x3
popq %rcx
movq %rcx, 0x8(%r15)
pushq $0x1
popq %r13
movq %r13, 0x10(%r15)
movq 0x10(%rdx), %rsi
leaq 0x18(%rsp), %r12
movq %rsi, -0x18(%r12)
movq %rax, -0x10(%r12)
movq 0x20(%rdx), %rax
movq %rax, -0x8(%r12)
movq (%rdx), %rax
movq %rax, (%r12)
movq %rcx, 0x8(%r12)
movq %r13, 0x10(%r12)
leaq 0x90(%rsp), %rdi
callq 0x9992
leaq 0x78(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x30, %rcx
je 0x681d1
movq 0x40(%rsp,%rcx), %rdx
movq %rdx, 0x70(%rsp)
movups 0x30(%rsp,%rcx), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x10(%rsp,%rcx), %rdx
movq %rdx, 0x10(%rax)
movups (%rsp,%rcx), %xmm0
movups %xmm0, (%rax)
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movaps 0x80(%rsp), %xmm2
movups %xmm0, 0x90(%rsp,%rcx,2)
movups %xmm1, 0xa0(%rsp,%rcx,2)
movups %xmm2, 0xb0(%rsp,%rcx,2)
addq $0x18, %rcx
jmp 0x68177
leaq 0xf0(%rsp), %r14
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x7b44
leaq 0x118(%rsp), %rax
pushq $0x2
popq %rcx
xorl %edx, %edx
imulq $0x30, %rdx, %rsi
leaq (%rsp,%rsi), %r8
addq $0x90, %r8
addq %r14, %rsi
leaq 0x2(%rdx), %rdi
leaq 0x1(%rdx), %r10
cmpq %rcx, %r10
jae 0x682ac
movq 0x8(%r8), %r9
cmpq 0x20(%r8), %r9
jne 0x6823f
movq 0x10(%r8), %r11
imulq %r9, %r11
cmpq 0x40(%r8), %r11
jne 0x6823f
movq 0x28(%r8), %r11
imulq %r9, %r11
cmpq 0x58(%r8), %r11
je 0x6824f
addq $0x30, %r8
addq $0x30, %rsi
incq %rdi
movq %r10, %rdx
jmp 0x6820c
movq 0x30(%r8), %r10
imulq %r9, %r10
movq 0x38(%r8), %r11
imulq %r9, %r11
addq %r10, (%r8)
movq %r11, 0x8(%r8)
movq 0x48(%r8), %r10
imulq %r9, %r10
imulq 0x50(%r8), %r9
addq %r10, 0x18(%r8)
movq %r9, 0x20(%r8)
cmpq %rcx, %rdi
jae 0x682a0
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movups %xmm2, -0x10(%rsi)
movups %xmm1, -0x20(%rsi)
movups %xmm0, -0x30(%rsi)
addq $0x30, %rsi
incq %rdi
jmp 0x6827b
decq %rcx
addq $-0x30, %rax
jmp 0x681f6
xorps %xmm0, %xmm0
cmpq $0x2, %rcx
jae 0x682d3
andq $0x0, -0x28(%rax)
movq %r13, -0x20(%rax)
movups %xmm0, -0x18(%rax)
movq %r13, -0x8(%rax)
andq $0x0, (%rax)
incq %rcx
addq $0x30, %rax
jmp 0x682af
xorl %eax, %eax
cmpq $0x30, %rax
je 0x68314
movq 0xa0(%rsp,%rax,2), %rcx
movq %rcx, 0x40(%rsp,%rax)
movups 0x90(%rsp,%rax,2), %xmm0
movups %xmm0, 0x30(%rsp,%rax)
movups 0xa8(%rsp,%rax,2), %xmm0
movups %xmm0, (%rsp,%rax)
movq 0xb8(%rsp,%rax,2), %rcx
movq %rcx, 0x10(%rsp,%rax)
addq $0x18, %rax
jmp 0x682d5
movq 0x10(%r15), %rax
movq %rax, 0x10(%rbx)
movups (%r15), %xmm0
movups %xmm0, (%rbx)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rbx)
movups 0x30(%rsp), %xmm0
movups %xmm0, 0x18(%rbx)
movups (%r12), %xmm0
movups %xmm0, 0x30(%rbx)
movq 0x10(%r12), %rax
movq %rax, 0x40(%rbx)
movups (%rsp), %xmm0
movups %xmm0, 0x48(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x58(%rbx)
movq %rbx, %rax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq