// File: dsharlet[P]array/include/array/array.h
// Function: decltype(make_shape_from_tuple((decltype(array_to_tuple(internal::declval<std::array<nda::dim<-9l, -9l, -9l>, nda::shape<nda::dim<-9l, -9l, 1l>, nda::dim<-9l, -9l, -9l>, nda::dim<-9l, -9l, -9l>>::rank()>>())))())) nda::internal::dynamic_optimize_shape<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>> const&)
NDARRAY_HOST_DEVICE shape_of_rank<Shape::rank()> dynamic_optimize_shape(const Shape& shape) {
  auto dims = internal::tuple_to_array<dim<>>(shape.dims());

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

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

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

  return shape_of_rank<Shape::rank()>(array_to_tuple(dims));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x1f8(%rbp)
movq %rdi, %rax
movq %rax, -0x1f0(%rbp)
movq %rdi, -0xf8(%rbp)
movq %rsi, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x13bf0
movq %rax, %rsi
leaq -0x148(%rbp), %rdi
callq 0x13bc0
leaq -0x148(%rbp), %rdi
callq 0x13c80
movq %rax, -0x1e8(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x13ca0
movq -0x1e8(%rbp), %rdi
movq %rax, %rsi
callq 0x13c00
leaq -0x148(%rbp), %rdi
callq 0x13cc0
movq %rax, -0x150(%rbp)
movq $0x0, -0x158(%rbp)
movq -0x158(%rbp), %rax
addq $0x1, %rax
cmpq -0x150(%rbp), %rax
jae 0x13aee
movq -0x158(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq %rax, -0x200(%rbp)
movq -0x158(%rbp), %rsi
addq $0x1, %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq -0x200(%rbp), %rcx
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
jne 0x13ad7
movq -0x158(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq %rax, -0x208(%rbp)
movq -0x158(%rbp), %rsi
addq $0x1, %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq -0x208(%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 0x13933
leaq 0x1c8a03(%rip), %rdi    # 0x1dc31e
leaq 0x1c87bf(%rip), %rsi    # 0x1dc0e1
movl $0x6cb, %edx            # imm = 0x6CB
leaq 0x1c8a07(%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 -0x170(%rbp), %rdi
callq 0x10c60
movq -0x158(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq -0x170(%rbp), %rcx
movq %rcx, (%rax)
movq -0x168(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x160(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
addq $0x1, %rax
cmpq -0x150(%rbp), %rax
jae 0x13ac3
movq -0x178(%rbp), %rsi
addq $0x1, %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq %rax, -0x210(%rbp)
movq -0x178(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq -0x210(%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 -0x178(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x178(%rbp)
jmp 0x13a4d
movq -0x150(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x150(%rbp)
jmp 0x13ae9
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x13781
movq -0x150(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x218(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x13cc0
movq %rax, %rcx
movq -0x218(%rbp), %rax
cmpq %rcx, %rax
jae 0x13b87
leaq -0x198(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x1, %edx
movq %rcx, %rsi
callq 0x10c60
movq -0x180(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x13cd0
movq -0x198(%rbp), %rcx
movq %rcx, (%rax)
movq -0x190(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x188(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x180(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x180(%rbp)
jmp 0x13afc
leaq -0x1e0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x13d00
movq -0x1f8(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x13d30
movq -0x1f0(%rbp), %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
nopl (%rax)