// File: ralgond[P]SortedListIntersection/algo_highlyscalable_SIMD.cpp
// Function: highlyscalable_SIMD_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t 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;

  // trim lengths to be a multiple of 4
  size_t st_a = (s_a / 4) * 4;
  size_t st_b = (s_b / 4) * 4;

  while (i_a < st_a && i_b < st_b) {
    //[ load segments of four 32-bit elements
    __m128i v_a = _mm_loadu_si128((__m128i *)&A[i_a]);
    __m128i v_b = _mm_loadu_si128((__m128i *)&B[i_b]);
    //]

    //[ move pointers
    const uint32_t a_max = A[i_a + 3];
    const uint32_t b_max = B[i_b + 3];
    i_a += (a_max <= b_max) * 4;
    i_b += (a_max >= b_max) * 4;
    //]

    //[ compute mask of common elements
    const uint32_t cyclic_shift = _MM_SHUFFLE(0, 3, 2, 1);
    __m128i cmp_mask1 = _mm_cmpeq_epi32(v_a, v_b); // pairwise comparison
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);    // shuffling
    __m128i cmp_mask2 = _mm_cmpeq_epi32(v_a, v_b); // again...
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);
    __m128i cmp_mask3 = _mm_cmpeq_epi32(v_a, v_b); // and again...
    v_b = _mm_shuffle_epi32(v_b, cyclic_shift);
    __m128i cmp_mask4 = _mm_cmpeq_epi32(v_a, v_b); // and again.
    __m128i cmp_mask = _mm_or_si128(
        _mm_or_si128(cmp_mask1, cmp_mask2),
        _mm_or_si128(cmp_mask3, cmp_mask4)); // OR-ing of comparison masks
    // convert the 128-bit mask to the 4-bit mask
    const int mask = _mm_movemask_ps(_mm_castsi128_ps(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
                                 //]
  }

  // 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 $0x258, %rsp            # imm = 0x258
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movq %rcx, 0xe0(%rsp)
movq %r8, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
cmpq 0xf8(%rsp), %rax
je 0x8af3
jmp 0x8b12
leaq 0x58bf(%rip), %rdi      # 0xe3b9
leaq 0x58c1(%rip), %rsi      # 0xe3c2
movl $0x1d, %edx
leaq 0x58d2(%rip), %rcx      # 0xe3df
callq 0x2120
movq 0xd8(%rsp), %rax
cmpq 0xe8(%rsp), %rax
je 0x8b26
jmp 0x8b45
leaq 0x5926(%rip), %rdi      # 0xe453
leaq 0x588e(%rip), %rsi      # 0xe3c2
movl $0x1e, %edx
leaq 0x589f(%rip), %rcx      # 0xe3df
callq 0x2120
movq 0xd8(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq $0x0, 0xc8(%rsp)
movq $0x0, 0xc0(%rsp)
movq 0xf0(%rsp), %rax
shrq $0x2, %rax
shlq $0x2, %rax
movq %rax, 0xb8(%rsp)
movq 0xe0(%rsp), %rax
shrq $0x2, %rax
shlq $0x2, %rax
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rcx
xorl %eax, %eax
cmpq 0xb8(%rsp), %rcx
movb %al, 0xf(%rsp)
jae 0x8bcc
movq 0xc0(%rsp), %rax
cmpq 0xb0(%rsp), %rax
setb %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x8bd9
jmp 0x8f95
movq 0xf8(%rsp), %rax
movq 0xc8(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0xa0(%rsp)
movq 0xe8(%rsp), %rax
movq 0xc0(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rax
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movq 0xf8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x8c(%rsp)
movq 0xe8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x88(%rsp)
movl 0x8c(%rsp), %eax
movl 0x88(%rsp), %ecx
subl %ecx, %eax
setbe %al
movzbl %al, %eax
movl %eax, %ecx
movq 0xc8(%rsp), %rax
leaq (%rax,%rcx,4), %rax
movq %rax, 0xc8(%rsp)
movl 0x8c(%rsp), %eax
movl 0x88(%rsp), %ecx
subl %ecx, %eax
setae %al
movzbl %al, %eax
movl %eax, %ecx
movq 0xc0(%rsp), %rax
leaq (%rax,%rcx,4), %rax
movq %rax, 0xc0(%rsp)
movl $0x39, 0x84(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0x90(%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), %xmm0
vpshufd $0x39, %xmm0, %xmm0     # xmm0 = xmm0[1,2,3,0]
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0x90(%rsp), %xmm0
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 0x90(%rsp), %xmm0
vpshufd $0x39, %xmm0, %xmm0     # xmm0 = xmm0[1,2,3,0]
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0x90(%rsp), %xmm0
vmovdqa %xmm1, 0x140(%rsp)
vmovdqa %xmm0, 0x130(%rsp)
vmovdqa 0x140(%rsp), %xmm0
vmovdqa 0x130(%rsp), %xmm1
vpcmpeqd %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm0
vpshufd $0x39, %xmm0, %xmm0     # xmm0 = xmm0[1,2,3,0]
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0x90(%rsp), %xmm0
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, 0x40(%rsp)
vmovdqa 0x70(%rsp), %xmm1
vmovdqa 0x60(%rsp), %xmm0
vmovdqa %xmm1, 0x1e0(%rsp)
vmovdqa %xmm0, 0x1d0(%rsp)
vmovdqa 0x1e0(%rsp), %xmm0
vpor 0x1d0(%rsp), %xmm0, %xmm1
vmovdqa 0x50(%rsp), %xmm2
vmovdqa 0x40(%rsp), %xmm0
vmovdqa %xmm2, 0x1c0(%rsp)
vmovdqa %xmm0, 0x1b0(%rsp)
vmovdqa 0x1c0(%rsp), %xmm0
vpor 0x1b0(%rsp), %xmm0, %xmm0
vmovdqa %xmm1, 0x1a0(%rsp)
vmovdqa %xmm0, 0x190(%rsp)
vmovdqa 0x1a0(%rsp), %xmm0
vpor 0x190(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa 0x30(%rsp), %xmm0
vmovdqa %xmm0, 0x200(%rsp)
vmovdqa 0x200(%rsp), %xmm0
vmovaps %xmm0, 0x1f0(%rsp)
vmovaps 0x1f0(%rsp), %xmm0
vmovmskps %xmm0, %eax
movl %eax, 0x2c(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
movslq 0x2c(%rsp), %rcx
leaq 0x932e(%rip), %rax      # 0x12220
shlq $0x4, %rcx
addq %rcx, %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x220(%rsp)
vmovdqa %xmm0, 0x210(%rsp)
vmovdqa 0x220(%rsp), %xmm0
vmovdqa 0x210(%rsp), %xmm1
vpshufb %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movq 0xd8(%rsp), %rax
vmovdqa 0x10(%rsp), %xmm0
movq %rax, 0x248(%rsp)
vmovdqa %xmm0, 0x230(%rsp)
vmovdqa 0x230(%rsp), %xmm0
movq 0x248(%rsp), %rax
vmovdqu %xmm0, (%rax)
movl 0x2c(%rsp), %eax
movl %eax, 0x254(%rsp)
movl 0x254(%rsp), %eax
popcntl %eax, %ecx
movq 0xd8(%rsp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xd8(%rsp)
jmp 0x8b9d
jmp 0x8f97
movq 0xc8(%rsp), %rcx
xorl %eax, %eax
cmpq 0xf0(%rsp), %rcx
movb %al, 0xe(%rsp)
jae 0x8fc6
movq 0xc0(%rsp), %rax
cmpq 0xe0(%rsp), %rax
setb %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x8fd3
jmp 0x90ad
movq 0xf8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xe8(%rsp), %rcx
movq 0xc0(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x9014
movq 0xc8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc8(%rsp)
jmp 0x90a8
movq 0xe8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xf8(%rsp), %rcx
movq 0xc8(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x9052
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
jmp 0x90a6
movq 0xe8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0xd8(%rsp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, 0xd8(%rsp)
movl %ecx, (%rax)
movq 0xc8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc8(%rsp)
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
jmp 0x90a8
jmp 0x8f97
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
addq $0x258, %rsp            # imm = 0x258
retq
nopl (%rax)