// File: 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 %rbp
movq %rsp, %rbp
andq $-0x20, %rsp
subq $0x3a0, %rsp            # imm = 0x3A0
movq %rdi, 0x188(%rsp)
movq %rsi, 0x180(%rsp)
movq %rdx, 0x178(%rsp)
movq %rcx, 0x170(%rsp)
movq %r8, 0x168(%rsp)
movq 0x168(%rsp), %rax
cmpq 0x188(%rsp), %rax
je 0x98fb
jmp 0x991a
leaq 0x4ab7(%rip), %rdi      # 0xe3b9
leaq 0x4bdb(%rip), %rsi      # 0xe4e4
movl $0x42, %edx
leaq 0x4bec(%rip), %rcx      # 0xe501
callq 0x2120
movq 0x168(%rsp), %rax
cmpq 0x178(%rsp), %rax
je 0x992e
jmp 0x994d
leaq 0x4b1e(%rip), %rdi      # 0xe453
leaq 0x4ba8(%rip), %rsi      # 0xe4e4
movl $0x43, %edx
leaq 0x4bb9(%rip), %rcx      # 0xe501
callq 0x2120
movq 0x168(%rsp), %rax
movq %rax, 0x160(%rsp)
movq $0x0, 0x158(%rsp)
movq $0x0, 0x150(%rsp)
movq 0x180(%rsp), %rax
shrq $0x3, %rax
shlq $0x3, %rax
movq %rax, 0x148(%rsp)
movq 0x170(%rsp), %rax
shrq $0x3, %rax
shlq $0x3, %rax
movq %rax, 0x140(%rsp)
movq 0x158(%rsp), %rcx
xorl %eax, %eax
cmpq 0x148(%rsp), %rcx
movb %al, 0x1f(%rsp)
jae 0x99d4
movq 0x150(%rsp), %rax
cmpq 0x140(%rsp), %rax
setb %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x99e1
jmp 0x9ddd
movq 0x188(%rsp), %rax
movq 0x158(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rax
vmovdqu (%rax), %ymm0
vmovdqa %ymm0, 0x120(%rsp)
movq 0x178(%rsp), %rax
movq 0x150(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rax
vmovdqu (%rax), %ymm0
vmovdqa %ymm0, 0x100(%rsp)
movq 0x188(%rsp), %rax
movq 0x158(%rsp), %rcx
movl 0x1c(%rax,%rcx,4), %eax
movl %eax, 0xfc(%rsp)
movq 0x178(%rsp), %rax
movq 0x150(%rsp), %rcx
movl 0x1c(%rax,%rcx,4), %eax
movl %eax, 0xf8(%rsp)
movl 0xfc(%rsp), %eax
movl 0xf8(%rsp), %ecx
subl %ecx, %eax
setbe %al
movzbl %al, %eax
movl %eax, %ecx
movq 0x158(%rsp), %rax
leaq (%rax,%rcx,8), %rax
movq %rax, 0x158(%rsp)
movl 0xfc(%rsp), %eax
movl 0xf8(%rsp), %ecx
subl %ecx, %eax
setae %al
movzbl %al, %eax
movl %eax, %ecx
movq 0x150(%rsp), %rax
leaq (%rax,%rcx,8), %rax
movq %rax, 0x150(%rsp)
movl $0x0, 0x1dc(%rsp)
movl $0x7, 0x1d8(%rsp)
movl $0x6, 0x1d4(%rsp)
movl $0x5, 0x1d0(%rsp)
movl $0x4, 0x1cc(%rsp)
movl $0x3, 0x1c8(%rsp)
movl $0x2, 0x1c4(%rsp)
movl $0x1, 0x1c0(%rsp)
movl 0x1c4(%rsp), %r8d
movl 0x1c8(%rsp), %edi
movl 0x1cc(%rsp), %esi
movl 0x1d4(%rsp), %edx
movl 0x1d8(%rsp), %ecx
movl 0x1dc(%rsp), %eax
vmovd 0x1c0(%rsp), %xmm0
vpinsrd $0x1, %r8d, %xmm0, %xmm0
vpinsrd $0x2, %edi, %xmm0, %xmm0
vpinsrd $0x3, %esi, %xmm0, %xmm0
vmovd 0x1d0(%rsp), %xmm1
vpinsrd $0x1, %edx, %xmm1, %xmm1
vpinsrd $0x2, %ecx, %xmm1, %xmm1
vpinsrd $0x3, %eax, %xmm1, %xmm1
vmovdqa %xmm1, 0x1b0(%rsp)
vmovdqa %xmm0, 0x1a0(%rsp)
vmovdqa 0x1a0(%rsp), %ymm0
vmovdqa %ymm0, 0xc0(%rsp)
vmovdqa 0x120(%rsp), %ymm1
vmovdqa 0x100(%rsp), %ymm0
vmovdqa %ymm1, 0x240(%rsp)
vmovdqa %ymm0, 0x220(%rsp)
vmovdqa 0x240(%rsp), %ymm0
vmovdqa 0x220(%rsp), %ymm1
vpcmpeqd %ymm1, %ymm0, %k0
vpmovm2d %k0, %ymm0
vmovdqa %ymm0, 0xa0(%rsp)
movl $0x0, 0x9c(%rsp)
cmpl $0x7, 0x9c(%rsp)
jge 0x9cf0
vmovdqa 0x100(%rsp), %ymm1
vmovdqa 0xc0(%rsp), %ymm0
vmovdqa %ymm1, 0x2c0(%rsp)
vmovdqa %ymm0, 0x2a0(%rsp)
vmovdqa 0x2c0(%rsp), %ymm1
vmovdqa 0x2a0(%rsp), %ymm0
vpermd %ymm1, %ymm0, %ymm0
vmovdqa %ymm0, 0x100(%rsp)
vmovdqa 0x120(%rsp), %ymm1
vmovdqa 0x100(%rsp), %ymm0
vmovdqa %ymm1, 0x200(%rsp)
vmovdqa %ymm0, 0x1e0(%rsp)
vmovdqa 0x200(%rsp), %ymm0
vmovdqa 0x1e0(%rsp), %ymm1
vpcmpeqd %ymm1, %ymm0, %k0
vpmovm2d %k0, %ymm0
vmovdqa %ymm0, 0x60(%rsp)
vmovdqa 0xa0(%rsp), %ymm1
vmovdqa 0x60(%rsp), %ymm0
vmovdqa %ymm1, 0x300(%rsp)
vmovdqa %ymm0, 0x2e0(%rsp)
vmovdqa 0x300(%rsp), %ymm0
vpor 0x2e0(%rsp), %ymm0, %ymm0
vmovdqa %ymm0, 0xa0(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x9c04
vmovdqa 0xa0(%rsp), %ymm0
vmovdqa %ymm0, 0x340(%rsp)
vmovdqa 0x340(%rsp), %ymm0
vmovaps %ymm0, 0x320(%rsp)
vmovaps 0x320(%rsp), %ymm0
vmovmskps %ymm0, %eax
movl %eax, 0x5c(%rsp)
vmovdqa 0x120(%rsp), %ymm1
movslq 0x5c(%rsp), %rcx
leaq 0x8606(%rip), %rax      # 0x12340
shlq $0x5, %rcx
addq %rcx, %rax
vmovdqa (%rax), %ymm0
vmovdqa %ymm1, 0x280(%rsp)
vmovdqa %ymm0, 0x260(%rsp)
vmovdqa 0x280(%rsp), %ymm1
vmovdqa 0x260(%rsp), %ymm0
vpermd %ymm1, %ymm0, %ymm0
vmovdqa %ymm0, 0x20(%rsp)
movq 0x168(%rsp), %rax
vmovdqa 0x20(%rsp), %ymm0
movq %rax, 0x380(%rsp)
vmovdqa %ymm0, 0x360(%rsp)
vmovdqa 0x360(%rsp), %ymm0
movq 0x380(%rsp), %rax
vmovdqu %ymm0, (%rax)
movl 0x5c(%rsp), %eax
movl %eax, 0x38c(%rsp)
movl 0x38c(%rsp), %eax
popcntl %eax, %ecx
movq 0x168(%rsp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x168(%rsp)
jmp 0x99a5
jmp 0x9ddf
movq 0x158(%rsp), %rcx
xorl %eax, %eax
cmpq 0x180(%rsp), %rcx
movb %al, 0x1e(%rsp)
jae 0x9e0e
movq 0x150(%rsp), %rax
cmpq 0x170(%rsp), %rax
setb %al
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x9e1b
jmp 0x9ef5
movq 0x188(%rsp), %rax
movq 0x158(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x178(%rsp), %rcx
movq 0x150(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x9e5c
movq 0x158(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x158(%rsp)
jmp 0x9ef0
movq 0x178(%rsp), %rax
movq 0x150(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x188(%rsp), %rcx
movq 0x158(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x9e9a
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x9eee
movq 0x178(%rsp), %rax
movq 0x150(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0x168(%rsp), %rax
movq %rax, %rdx
addq $0x4, %rdx
movq %rdx, 0x168(%rsp)
movl %ecx, (%rax)
movq 0x158(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x158(%rsp)
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x9ef0
jmp 0x9ddf
movq 0x168(%rsp), %rax
movq 0x160(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movq %rbp, %rsp
popq %rbp
vzeroupper
retq
nopw %cs:(%rax,%rax)