// File: 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<-9l, -9l, -9l>>, nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<-9l, -9l, -9l>>, void>(nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<-9l, -9l, -9l>> const&, nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<-9l, -9l, -9l>> 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 %rbp
movq %rsp, %rbp
subq $0x590, %rsp            # imm = 0x590
movq %rdi, -0x530(%rbp)
movq %rdi, %rax
movq %rax, -0x528(%rbp)
movq %rdi, -0x208(%rbp)
movq %rsi, -0x210(%rbp)
movq %rdx, -0x218(%rbp)
movq $0x3, -0x220(%rbp)
movq -0x210(%rbp), %rdi
callq 0x13bf0
movq %rax, %rsi
leaq -0x268(%rbp), %rdi
callq 0x13bc0
movq -0x218(%rbp), %rdi
callq 0x13bf0
movq %rax, %rsi
leaq -0x2b0(%rbp), %rdi
callq 0x13bc0
leaq -0x340(%rbp), %rdi
callq 0x15bc0
movq $0x0, -0x348(%rbp)
cmpq $0x3, -0x348(%rbp)
jae 0x1526b
movq -0x348(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x13cd0
movq (%rax), %rcx
movq %rcx, -0x378(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x370(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x368(%rbp)
movq -0x348(%rbp), %rsi
leaq -0x2b0(%rbp), %rdi
callq 0x13cd0
movq (%rax), %rcx
movq %rcx, -0x360(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x358(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x350(%rbp)
movq -0x348(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, %rdi
leaq -0x378(%rbp), %rsi
movl $0x30, %edx
callq 0x31c0
movq -0x348(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x348(%rbp)
jmp 0x151b9
leaq -0x340(%rbp), %rdi
callq 0x15cc0
movq %rax, -0x538(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x15ce0
movq -0x538(%rbp), %rdi
movq %rax, %rsi
callq 0x15c40
leaq -0x340(%rbp), %rdi
callq 0x15d10
movq %rax, -0x380(%rbp)
movq $0x0, -0x388(%rbp)
movq -0x388(%rbp), %rax
addq $0x1, %rax
cmpq -0x380(%rbp), %rax
jae 0x159b8
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x1f8(%rbp)
movq -0x1f8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x540(%rbp)
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, %rcx
movq -0x540(%rbp), %rax
addq $0x18, %rcx
movq %rcx, -0x200(%rbp)
movq -0x200(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x1e8(%rbp)
movq -0x1e8(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x159a1
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x548(%rbp)
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x548(%rbp), %rcx
movq %rcx, -0x198(%rbp)
movq %rax, -0x1a0(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq (%rax), %rax
movq -0x198(%rbp), %rcx
movq %rcx, -0x190(%rbp)
movq -0x190(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x168(%rbp)
movq -0x168(%rbp), %rcx
imulq (%rcx), %rax
movq -0x1a0(%rbp), %rcx
movq %rcx, -0x188(%rbp)
movq -0x188(%rbp), %rcx
addq $0x10, %rcx
movq %rcx, -0x170(%rbp)
movq -0x170(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x159a1
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
addq $0x18, %rax
movq %rax, -0x550(%rbp)
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x550(%rbp), %rcx
addq $0x18, %rax
movq %rcx, -0x1d8(%rbp)
movq %rax, -0x1e0(%rbp)
movq -0x1d8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movq (%rax), %rax
movq -0x1d8(%rbp), %rcx
movq %rcx, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rcx
imulq (%rcx), %rax
movq -0x1e0(%rbp), %rcx
movq %rcx, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rcx
addq $0x10, %rcx
movq %rcx, -0x1b0(%rbp)
movq -0x1b0(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x159a1
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x558(%rbp)
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x558(%rbp), %rcx
movq %rcx, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq %rcx, -0x38(%rbp)
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x8(%rbp)
movq -0x8(%rbp), %rcx
imulq (%rcx), %rax
movq -0x40(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rcx
addq $0x10, %rcx
movq %rcx, -0x10(%rbp)
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0x155c3
leaq 0x1c6d73(%rip), %rdi    # 0x1dc31e
leaq 0x1c6b2f(%rip), %rsi    # 0x1dc0e1
movl $0x6cb, %edx            # imm = 0x6CB
leaq 0x1c6d77(%rip), %rcx    # 0x1dc335
callq 0x3130
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movq -0x80(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x68(%rbp)
movq -0x68(%rbp), %rcx
imulq (%rcx), %rax
addq %rax, %rsi
movq -0xa8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rdx
movq -0xb0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
imulq (%rax), %rdx
movq -0xa8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rcx
leaq -0x3a0(%rbp), %rdi
callq 0x10c60
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x3a0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x398(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x390(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
addq $0x18, %rax
movq %rax, -0x560(%rbp)
movq -0x388(%rbp), %rsi
addq $0x1, %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x560(%rbp), %rcx
addq $0x18, %rax
movq %rcx, -0x158(%rbp)
movq %rax, -0x160(%rbp)
movq -0x158(%rbp), %rcx
movq -0x160(%rbp), %rax
movq %rcx, -0xe8(%rbp)
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movq -0xe8(%rbp), %rcx
movq %rcx, -0xe0(%rbp)
movq -0xe0(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0xb8(%rbp)
movq -0xb8(%rbp), %rcx
imulq (%rcx), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movq -0xd8(%rbp), %rcx
addq $0x10, %rcx
movq %rcx, -0xc0(%rbp)
movq -0xc0(%rbp), %rcx
cmpq (%rcx), %rax
je 0x157d5
leaq 0x1c6b61(%rip), %rdi    # 0x1dc31e
leaq 0x1c691d(%rip), %rsi    # 0x1dc0e1
movl $0x6cb, %edx            # imm = 0x6CB
leaq 0x1c6b65(%rip), %rcx    # 0x1dc335
callq 0x3130
movq -0x158(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq (%rax), %rsi
movq -0x160(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq (%rax), %rax
movq -0x158(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movq -0x130(%rbp), %rcx
addq $0x8, %rcx
movq %rcx, -0x118(%rbp)
movq -0x118(%rbp), %rcx
imulq (%rcx), %rax
addq %rax, %rsi
movq -0x158(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq (%rax), %rdx
movq -0x160(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
imulq (%rax), %rdx
movq -0x158(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rax
movq (%rax), %rcx
leaq -0x3b8(%rbp), %rdi
callq 0x10c60
movq -0x388(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x3b8(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x3b0(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x3a8(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x388(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3c0(%rbp)
movq -0x3c0(%rbp), %rax
addq $0x1, %rax
cmpq -0x380(%rbp), %rax
jae 0x1598d
movq -0x3c0(%rbp), %rsi
addq $0x1, %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x568(%rbp)
movq -0x3c0(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq -0x568(%rbp), %rsi
movq %rax, %rdi
movl $0x30, %edx
callq 0x31c0
movq -0x3c0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3c0(%rbp)
jmp 0x15920
movq -0x380(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x380(%rbp)
jmp 0x159b3
movq -0x388(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x388(%rbp)
jmp 0x152b7
movq -0x380(%rbp), %rax
movq %rax, -0x3c8(%rbp)
movq -0x3c8(%rbp), %rax
movq %rax, -0x570(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x15d10
movq %rax, %rcx
movq -0x570(%rbp), %rax
cmpq %rcx, %rax
jae 0x15a61
leaq -0x3f8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x1, %edx
movq %rcx, %rsi
callq 0x10c60
leaq -0x3f8(%rbp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x1, %edx
movq %rcx, %rsi
callq 0x10c60
movq -0x3c8(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, %rdi
leaq -0x3f8(%rbp), %rsi
movl $0x30, %edx
callq 0x31c0
movq -0x3c8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3c8(%rbp)
jmp 0x159c6
movq $0x0, -0x400(%rbp)
movq -0x400(%rbp), %rax
movq %rax, -0x578(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x15d10
movq %rax, %rcx
movq -0x578(%rbp), %rax
cmpq %rcx, %rax
jae 0x15b45
movq -0x400(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x588(%rbp)
movq -0x400(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x13cd0
movq -0x588(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq -0x400(%rbp), %rsi
leaq -0x340(%rbp), %rdi
callq 0x15c10
movq %rax, -0x580(%rbp)
movq -0x400(%rbp), %rsi
leaq -0x2b0(%rbp), %rdi
callq 0x13cd0
movq -0x580(%rbp), %rcx
movq 0x18(%rcx), %rdx
movq %rdx, (%rax)
movq 0x20(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x28(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq -0x400(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x400(%rbp)
jmp 0x15a6c
leaq -0x490(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x13d00
leaq -0x448(%rbp), %rdi
leaq -0x490(%rbp), %rsi
callq 0x13d30
leaq -0x520(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x13d00
leaq -0x4d8(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x13d30
movq -0x530(%rbp), %rdi
leaq -0x448(%rbp), %rsi
leaq -0x4d8(%rbp), %rdx
callq 0x15d20
movq -0x528(%rbp), %rax
addq $0x590, %rsp            # imm = 0x590
popq %rbp
retq
nopl (%rax,%rax)