// File: sid1607[P]avx2-merge-sort/merge_sort.cpp
// Function: intra_register_sort(long long vector[4]&, long long vector[4]&)
inline void intra_register_sort(__m256i& a8, __m256i& b8) {
  __m256i mina, maxa, minb, maxb;
  // phase 1
  auto a8_1 = register_shuffle(a8, global_masks.swap_128);
  auto b8_1 = register_shuffle(b8, global_masks.swap_128);

  minmax(a8, a8_1, mina, maxa);
  minmax(b8, b8_1, minb, maxb);

  auto a4 = _mm256_blend_epi32(mina, maxa, 0xf0);
  auto b4 = _mm256_blend_epi32(minb, maxb, 0xf0);

  // phase 2
  auto a4_1 = _mm256_shuffle_epi32(a4, 0x4e);
  auto b4_1 = _mm256_shuffle_epi32(b4, 0x4e);

  minmax(a4, a4_1, mina, maxa);
  minmax(b4, b4_1, minb, maxb);

  auto a2 = _mm256_unpacklo_epi64(mina, maxa);
  auto b2 = _mm256_unpacklo_epi64(minb, maxb);
  // phase 3
  auto a2_1 = _mm256_shuffle_epi32(a2, 0xb1);
  auto b2_1 = _mm256_shuffle_epi32(b2, 0xb1);

  minmax(a2, a2_1, mina, maxa);
  minmax(b2, b2_1, minb, maxb);

  a8 = _mm256_blend_epi32(mina, maxa, 0xaa);
  b8 = _mm256_blend_epi32(minb, maxb, 0xaa);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
andq $-0x20, %rsp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, 0x218(%rsp)
movq %rsi, 0x210(%rsp)
movq 0x218(%rsp), %rdi
leaq 0x73a2(%rip), %rsi      # 0xa1c0
movq %rsi, 0x18(%rsp)
callq 0x30a0
movq 0x18(%rsp), %rsi
vmovaps %ymm0, 0x160(%rsp)
movq 0x210(%rsp), %rdi
callq 0x30a0
vmovaps %ymm0, 0x140(%rsp)
movq 0x218(%rsp), %rdi
leaq 0x160(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
movq %rdx, 0x20(%rsp)
leaq 0x1c0(%rsp), %rcx
movq %rcx, 0x28(%rsp)
vzeroupper
callq 0x30f0
movq 0x210(%rsp), %rdi
leaq 0x140(%rsp), %rsi
leaq 0x1a0(%rsp), %rdx
movq %rdx, 0x30(%rsp)
leaq 0x180(%rsp), %rcx
movq %rcx, 0x38(%rsp)
callq 0x30f0
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
vmovaps 0x1e0(%rsp), %ymm0
vmovaps 0x1c0(%rsp), %ymm1
vpblendd $0xf0, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0,1,2,3],ymm1[4,5,6,7]
vmovaps %ymm0, 0x120(%rsp)
vmovaps 0x1a0(%rsp), %ymm0
vmovaps 0x180(%rsp), %ymm1
vpblendd $0xf0, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0,1,2,3],ymm1[4,5,6,7]
vmovaps %ymm0, 0x100(%rsp)
vmovaps 0x120(%rsp), %ymm0
vpshufd $0x4e, %ymm0, %ymm0     # ymm0 = ymm0[2,3,0,1,6,7,4,5]
vmovaps %ymm0, 0xe0(%rsp)
vmovaps 0x100(%rsp), %ymm0
vpshufd $0x4e, %ymm0, %ymm0     # ymm0 = ymm0[2,3,0,1,6,7,4,5]
vmovaps %ymm0, 0xc0(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
vzeroupper
callq 0x30f0
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
leaq 0x100(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x30f0
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
vmovaps 0x1e0(%rsp), %ymm1
vmovaps 0x1c0(%rsp), %ymm0
vmovaps %ymm1, 0x280(%rsp)
vmovaps %ymm0, 0x260(%rsp)
vmovaps 0x280(%rsp), %ymm0
vmovaps 0x260(%rsp), %ymm1
vpunpcklqdq %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0],ymm1[0],ymm0[2],ymm1[2]
vmovaps %ymm0, 0xa0(%rsp)
vmovaps 0x1a0(%rsp), %ymm1
vmovaps 0x180(%rsp), %ymm0
vmovaps %ymm1, 0x240(%rsp)
vmovaps %ymm0, 0x220(%rsp)
vmovaps 0x240(%rsp), %ymm0
vmovaps 0x220(%rsp), %ymm1
vpunpcklqdq %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0],ymm1[0],ymm0[2],ymm1[2]
vmovaps %ymm0, 0x80(%rsp)
vmovaps 0xa0(%rsp), %ymm0
vpshufd $0xb1, %ymm0, %ymm0     # ymm0 = ymm0[1,0,3,2,5,4,7,6]
vmovaps %ymm0, 0x60(%rsp)
vmovaps 0x80(%rsp), %ymm0
vpshufd $0xb1, %ymm0, %ymm0     # ymm0 = ymm0[1,0,3,2,5,4,7,6]
vmovaps %ymm0, 0x40(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
vzeroupper
callq 0x30f0
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x30f0
vmovaps 0x1e0(%rsp), %ymm0
vmovaps 0x1c0(%rsp), %ymm1
vpblendd $0xaa, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0],ymm1[1],ymm0[2],ymm1[3],ymm0[4],ymm1[5],ymm0[6],ymm1[7]
movq 0x218(%rsp), %rax
vmovaps %ymm0, (%rax)
vmovaps 0x1a0(%rsp), %ymm0
vmovaps 0x180(%rsp), %ymm1
vpblendd $0xaa, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0],ymm1[1],ymm0[2],ymm1[3],ymm0[4],ymm1[5],ymm0[6],ymm1[7]
movq 0x210(%rsp), %rax
vmovdqa %ymm0, (%rax)
movq %rbp, %rsp
popq %rbp
vzeroupper
retq
nopw (%rax,%rax)