// File: ralgond[P]SortedListIntersection/algo_highlyscalable_SIMD.cpp
// Function: lemire_highlyscalable_SIMD_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t lemire_highlyscalable_SIMD_intersection(const uint32_t *A, const size_t s_a,
                                            const uint32_t *B, const size_t s_b,
                                            uint32_t *out) {
  assert(out != A);
  assert(out != B);
  const uint32_t *const initout(out);
  size_t i_a = 0, i_b = 0;
  const static uint32_t cyclic_shift1 = _MM_SHUFFLE(0, 3, 2, 1);
  const static uint32_t cyclic_shift2 = _MM_SHUFFLE(1, 0, 3, 2);
  const static uint32_t cyclic_shift3 = _MM_SHUFFLE(2, 1, 0, 3);

  // trim lengths to be a multiple of 4
  size_t st_a = (s_a / 4) * 4;
  size_t st_b = (s_b / 4) * 4;
  if (i_a < st_a && i_b < st_b) {
    __m128i v_a, v_b;
    v_a = MM_LOAD_SI_128((__m128i *)&A[i_a]);
    v_b = MM_LOAD_SI_128((__m128i *)&B[i_b]);
    while (true) {
      const __m128i cmp_mask1 =
          _mm_cmpeq_epi32(v_a, v_b); // pairwise comparison
      const __m128i cmp_mask2 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift1)); // again...
      __m128i cmp_mask = _mm_or_si128(cmp_mask1, cmp_mask2);
      const __m128i cmp_mask3 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift2)); // and again...
      cmp_mask = _mm_or_si128(cmp_mask, cmp_mask3);
      const __m128i cmp_mask4 = _mm_cmpeq_epi32(
          v_a, _mm_shuffle_epi32(v_b, cyclic_shift3)); // and again.
      cmp_mask = _mm_or_si128(cmp_mask, cmp_mask4);
      // convert the 128-bit mask to the 4-bit mask
      const int mask = _mm_movemask_ps(*reinterpret_cast<__m128 *>(&cmp_mask));
      // copy out common elements
      const __m128i p = _mm_shuffle_epi8(v_a, shuffle_mask[mask]);

      _mm_storeu_si128((__m128i *)out, p);
      out +=
          _mm_popcnt_u32(mask); // a number of elements is a weight of the mask

      const uint32_t a_max = A[i_a + 3];
      if (a_max <= B[i_b + 3]) {
        i_a += 4;
        if (i_a >= st_a)
          break;
        v_a = MM_LOAD_SI_128((__m128i *)&A[i_a]);
      }
      if (a_max >= B[i_b + 3]) {
        i_b += 4;
        if (i_b >= st_b)
          break;
        v_b = MM_LOAD_SI_128((__m128i *)&B[i_b]);
      }
    }
  }

  // intersect the tail using scalar intersection
  while (i_a < s_a && i_b < s_b) {
    if (A[i_a] < B[i_b]) {
      i_a++;
    } else if (B[i_b] < A[i_a]) {
      i_b++;
    } else {
      *out++ = B[i_b];
      i_a++;
      i_b++;
    }
  }

  return out - initout;
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movq %r8, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
cmpq 0xe8(%rsp), %rax
je 0x9113
jmp 0x9132
leaq 0x529f(%rip), %rdi      # 0xe3b9
leaq 0x52a1(%rip), %rsi      # 0xe3c2
movl $0x67, %edx
leaq 0x532f(%rip), %rcx      # 0xe45c
callq 0x2120
movq 0xc8(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x9146
jmp 0x9165
leaq 0x5306(%rip), %rdi      # 0xe453
leaq 0x526e(%rip), %rsi      # 0xe3c2
movl $0x68, %edx
leaq 0x52fc(%rip), %rcx      # 0xe45c
callq 0x2120
movq 0xc8(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq $0x0, 0xb8(%rsp)
movq $0x0, 0xb0(%rsp)
movq 0xe0(%rsp), %rax
shrq $0x2, %rax
shlq $0x2, %rax
movq %rax, 0xa8(%rsp)
movq 0xd0(%rsp), %rax
shrq $0x2, %rax
shlq $0x2, %rax
movq %rax, 0xa0(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jae 0x95f3
movq 0xb0(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jae 0x95f3
movq 0xe8(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x80(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vmovdqa %xmm1, 0x180(%rsp)
vmovdqa %xmm0, 0x170(%rsp)
vmovdqa 0x180(%rsp), %xmm0
vmovdqa 0x170(%rsp), %xmm1
vpcmpeqd %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vpshufd $0x39, %xmm0, %xmm0     # xmm0 = xmm0[1,2,3,0]
vmovdqa %xmm1, 0x160(%rsp)
vmovdqa %xmm0, 0x150(%rsp)
vmovdqa 0x160(%rsp), %xmm0
vmovdqa 0x150(%rsp), %xmm1
vpcmpeqd %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa 0x70(%rsp), %xmm1
vmovdqa 0x60(%rsp), %xmm0
vmovdqa %xmm1, 0x1e0(%rsp)
vmovdqa %xmm0, 0x1d0(%rsp)
vmovdqa 0x1e0(%rsp), %xmm0
vmovdqa 0x1d0(%rsp), %xmm1
vpor %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vpshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
vmovdqa %xmm1, 0x140(%rsp)
vmovdqa %xmm0, 0x130(%rsp)
vmovdqa 0x140(%rsp), %xmm0
vmovdqa 0x130(%rsp), %xmm1
vpcmpeqd %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x40(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x40(%rsp), %xmm0
vmovdqa %xmm1, 0x1c0(%rsp)
vmovdqa %xmm0, 0x1b0(%rsp)
vmovdqa 0x1c0(%rsp), %xmm0
vmovdqa 0x1b0(%rsp), %xmm1
vpor %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vpshufd $0x93, %xmm0, %xmm0     # xmm0 = xmm0[3,0,1,2]
vmovdqa %xmm1, 0x120(%rsp)
vmovdqa %xmm0, 0x110(%rsp)
vmovdqa 0x120(%rsp), %xmm0
vmovdqa 0x110(%rsp), %xmm1
vpcmpeqd %xmm1, %xmm0, %k0
vpmovm2d %k0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x30(%rsp), %xmm0
vmovdqa %xmm1, 0x1a0(%rsp)
vmovdqa %xmm0, 0x190(%rsp)
vmovdqa 0x1a0(%rsp), %xmm0
vpor 0x190(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovaps 0x50(%rsp), %xmm0
vmovaps %xmm0, 0x1f0(%rsp)
vmovaps 0x1f0(%rsp), %xmm0
vmovmskps %xmm0, %eax
movl %eax, 0x2c(%rsp)
vmovdqa 0x90(%rsp), %xmm1
movslq 0x2c(%rsp), %rcx
leaq 0x8dd9(%rip), %rax      # 0x12220
shlq $0x4, %rcx
addq %rcx, %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x210(%rsp)
vmovdqa %xmm0, 0x200(%rsp)
vmovdqa 0x210(%rsp), %xmm0
vmovdqa 0x200(%rsp), %xmm1
vpshufb %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movq 0xc8(%rsp), %rax
vmovdqa 0x10(%rsp), %xmm0
movq %rax, 0x238(%rsp)
vmovdqa %xmm0, 0x220(%rsp)
vmovdqa 0x220(%rsp), %xmm0
movq 0x238(%rsp), %rax
vmovdqu %xmm0, (%rax)
movl 0x2c(%rsp), %eax
movl %eax, 0x244(%rsp)
movl 0x244(%rsp), %eax
popcntl %eax, %ecx
movq 0xc8(%rsp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xc8(%rsp)
movq 0xe8(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0xd8(%rsp), %rcx
movq 0xb0(%rsp), %rdx
cmpl 0xc(%rcx,%rdx,4), %eax
ja 0x9576
movq 0xb8(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jb 0x9542
jmp 0x95f1
movq 0xe8(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movl 0xc(%rsp), %eax
movq 0xd8(%rsp), %rcx
movq 0xb0(%rsp), %rdx
cmpl 0xc(%rcx,%rdx,4), %eax
jb 0x95ec
movq 0xb0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jb 0x95b8
jmp 0x95f1
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x80(%rsp)
jmp 0x9251
jmp 0x95f3
jmp 0x95f5
movq 0xb8(%rsp), %rcx
xorl %eax, %eax
cmpq 0xe0(%rsp), %rcx
movb %al, 0xb(%rsp)
jae 0x9624
movq 0xb0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
setb %al
movb %al, 0xb(%rsp)
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x9631
jmp 0x970b
movq 0xe8(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xd8(%rsp), %rcx
movq 0xb0(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x9672
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x9706
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xe8(%rsp), %rcx
movq 0xb8(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x96b0
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x9704
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0xc8(%rsp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, 0xc8(%rsp)
movl %ecx, (%rax)
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x9706
jmp 0x95f5
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
addq $0x248, %rsp            # imm = 0x248
retq
nopw (%rax,%rax)