// File: /workspace/github_cpp_makefile_star2_O3/ralgond[P]SortedListIntersection/algo_highlyscalable_avx2.cpp
// Function: highlyscalable_avx2_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t highlyscalable_avx2_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 8
  size_t st_a = (s_a / 8) * 8;
  size_t st_b = (s_b / 8) * 8;

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

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

    //[ compute mask of common elements
    const __m256i rotate_arg = _mm256_set_epi32(0,7,6,5,4,3,2,1);
    __m256i cmp_mask = _mm256_cmpeq_epi32(v_a, v_b); // pairwise comparison

    for (int i = 0; i < 7; i++) {
	    v_b = _mm256_permutevar8x32_epi32(v_b, rotate_arg);    // shuffling
	    __m256i cmp_mask1 = _mm256_cmpeq_epi32(v_a, v_b); // again...
	    cmp_mask = _mm256_or_si256(cmp_mask, cmp_mask1);
    }

    // convert the 256-bit mask to the 8-bit mask
    const int mask = _mm256_movemask_ps(_mm256_castsi256_ps(cmp_mask));
    //cout<<"mask: "<<mask<<endl;

    //[ copy out common elements
    const __m256i p = _mm256_permutevar8x32_epi32(v_a, shuffle_mask32[mask]);
    _mm256_storeu_si256((__m256i *)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:
pushq %r14
pushq %rbx
pushq %rax
cmpq %rdi, %r8
je 0x412b
cmpq %rdx, %r8
je 0x414a
movq %rsi, %r11
andq $-0x8, %r11
movq %rcx, %rbx
sete %al
andq $-0x8, %rbx
sete %r9b
orb %al, %r9b
jne 0x40d0
vpmovsxbd 0x25a9(%rip), %xmm1 # 0x65c8
vpxor %xmm0, %xmm0, %xmm0
leaq 0x5156(%rip), %r14      # 0x9180
movq %r8, %rax
vmovq %xmm0, %r9
vpextrq $0x1, %xmm0, %r10
vpbroadcastq 0x18(%rdi,%r9,4), %xmm5
vpbroadcastq 0x18(%rdx,%r10,4), %xmm4
vmovdqu (%rdi,%r9,4), %ymm2
vmovdqu (%rdx,%r10,4), %ymm3
movl $0x7, %r9d
vmovdqa %xmm5, %xmm6
vpermt2d %xmm4, %xmm1, %xmm6
vpermt2d %xmm5, %xmm1, %xmm4
vpcmpeqd %ymm3, %ymm2, %ymm5
vpcmpleud %xmm4, %xmm6, %k1
vpbroadcastq 0x2543(%rip), %xmm4 {%k1} {z} # 0x65c0
valignd $0x1, %ymm3, %ymm3, %ymm3 # ymm3 = ymm3[1,2,3,4,5,6,7,0]
decl %r9d
vpcmpeqd %ymm3, %ymm2, %ymm6
vpor %ymm6, %ymm5, %ymm5
jne 0x407d
vmovmskps %ymm5, %r9d
vpaddq %xmm0, %xmm4, %xmm0
popcntl %r9d, %r10d
shll $0x5, %r9d
vmovdqa (%r9,%r14), %ymm3
vpextrq $0x1, %xmm0, %r9
vpermd %ymm2, %ymm3, %ymm2
vmovdqu %ymm2, (%rax)
leaq (%rax,%r10,4), %rax
vmovq %xmm0, %r10
cmpq %r11, %r10
jae 0x40d9
cmpq %rbx, %r9
jb 0x402d
jmp 0x40d9
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r8, %rax
cmpq %rsi, %r10
setb %r11b
cmpq %rcx, %r9
setb %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0x4119
movl (%rdi,%r10,4), %ebx
movl (%rdx,%r9,4), %r11d
cmpl %r11d, %ebx
jae 0x4100
incq %r10
jmp 0x410f
ja 0x410c
movl %r11d, (%rax)
addq $0x4, %rax
incq %r10
incq %r9
cmpq %rsi, %r10
jae 0x4119
cmpq %rcx, %r9
jb 0x40ee
subq %r8, %rax
sarq $0x2, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
vzeroupper
retq
leaq 0x235e(%rip), %rdi      # 0x6490
leaq 0x2493(%rip), %rsi      # 0x65cc
leaq 0x24a9(%rip), %rcx      # 0x65e9
movl $0x42, %edx
callq 0x20b0
leaq 0x23d9(%rip), %rdi      # 0x652a
leaq 0x2474(%rip), %rsi      # 0x65cc
leaq 0x248a(%rip), %rcx      # 0x65e9
movl $0x43, %edx
callq 0x20b0