// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rax
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
movq 0x10(%rsi), %r8
leaq 0x60(%rsp), %rcx
movq %r8, -0x18(%rcx)
movl $0x4, %r8d
movq %r8, -0x10(%rcx)
movq %rdi, -0x8(%rcx)
movq 0x20(%rsi), %r9
movq %r9, -0x8(%rcx)
movq (%rsi), %rsi
movq %rsi, (%rcx)
movl $0x3, %r9d
movq %r9, 0x8(%rcx)
movl $0x1, %r10d
movq %r10, 0x10(%rcx)
movq 0x10(%rdx), %r11
leaq 0x30(%rsp), %rsi
movq %r11, -0x18(%rsi)
movq %r8, -0x10(%rsi)
movq %rdi, -0x8(%rsi)
movq 0x20(%rdx), %r8
movq %r8, -0x8(%rsi)
movq (%rdx), %rdx
movq %rdx, (%rsi)
movq %r9, 0x8(%rsi)
movq %r10, 0x10(%rsi)
movl $0x28, %edx
xorps %xmm0, %xmm0
movups %xmm0, -0x70(%rsp,%rdx)
movq %rdi, -0x60(%rsp,%rdx)
movups %xmm0, -0x58(%rsp,%rdx)
movq %rdi, -0x48(%rsp,%rdx)
addq $0x30, %rdx
cmpq $0x88, %rdx
jne 0x7a156
leaq -0x68(%rsp), %rdx
xorl %edi, %edi
movq 0x58(%rsp,%rdi), %r8
movq %r8, -0x70(%rsp)
movups 0x48(%rsp,%rdi), %xmm0
movaps %xmm0, -0x80(%rsp)
movq 0x28(%rsp,%rdi), %r8
movq %r8, 0x10(%rdx)
movups 0x18(%rsp,%rdi), %xmm0
movups %xmm0, (%rdx)
movaps -0x80(%rsp), %xmm0
movaps -0x70(%rsp), %xmm1
movaps -0x60(%rsp), %xmm2
movups %xmm0, -0x48(%rsp,%rdi,2)
movups %xmm1, -0x38(%rsp,%rdi,2)
movups %xmm2, -0x28(%rsp,%rdi,2)
addq $0x18, %rdi
cmpq $0x18, %rdi
je 0x7a17e
xorl %edx, %edx
leaq (%rsp,%rdx), %rdi
addq $-0x48, %rdi
movq %rdx, %r8
movq -0x20(%rsp,%r8), %r9
cmpq -0x20(%rsp,%rdx), %r9
jge 0x7a23c
leaq (%rsp,%r8), %r9
addq $-0x48, %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 $0x60, %r8
jne 0x7a1d8
addq $0x30, %rdx
cmpq $0x60, %rdx
jne 0x7a1cd
movl $0x2, %edx
movl $0x1, %r8d
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
shlq $0x4, %r9
movq -0x40(%rsp,%r9), %rbx
movq -0x28(%rsp,%r9), %r10
cmpq %r10, %rbx
jne 0x7a312
leaq (%rsp,%r9), %r11
addq $-0x48, %r11
leaq (%r8,%r8,2), %r9
shlq $0x4, %r9
movq 0x10(%r11), %r14
imulq %rbx, %r14
cmpq -0x38(%rsp,%r9), %r14
jne 0x7a312
addq %rsp, %r9
addq $-0x48, %r9
movq 0x28(%r11), %r14
imulq %r10, %r14
cmpq 0x28(%r9), %r14
jne 0x7a312
movq (%r9), %r14
imulq %rbx, %r14
imulq 0x8(%r9), %rbx
addq %r14, (%r11)
movq %rbx, 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 0x7a30d
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 0x7a2e7
decq %rdx
jmp 0x7a315
movq %r8, %rdi
leaq 0x1(%rdi), %r8
cmpq %rdx, %r8
jb 0x7a261
cmpq $0x1, %rdx
ja 0x7a362
leaq (%rdx,%rdx,2), %rdi
decq %rdx
shlq $0x4, %rdi
addq %rsp, %rdi
addq $-0x20, %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
je 0x7a346
xorl %edx, %edx
movq -0x38(%rsp,%rdx,2), %rdi
movq %rdi, 0x58(%rsp,%rdx)
movups -0x48(%rsp,%rdx,2), %xmm0
movups %xmm0, 0x48(%rsp,%rdx)
movups -0x30(%rsp,%rdx,2), %xmm0
movups %xmm0, 0x18(%rsp,%rdx)
movq -0x20(%rsp,%rdx,2), %rdi
movq %rdi, 0x28(%rsp,%rdx)
addq $0x18, %rdx
cmpq $0x18, %rdx
je 0x7a364
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movups (%rcx), %xmm0
movups %xmm0, (%rax)
movq 0x58(%rsp), %rcx
movq %rcx, 0x28(%rax)
movups 0x48(%rsp), %xmm0
movups %xmm0, 0x18(%rax)
movups (%rsi), %xmm0
movups %xmm0, 0x30(%rax)
movq 0x10(%rsi), %rcx
movq %rcx, 0x40(%rax)
movups 0x18(%rsp), %xmm0
movups %xmm0, 0x48(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x58(%rax)
addq $0x78, %rsp
popq %rbx
popq %r14
retq
nop