// File: ralgond[P]SortedListIntersection/algo_v1_avx2.cpp
// Function: v1_avx2_intersection(unsigned int const*, unsigned long, unsigned int const*, unsigned long, unsigned int*)
size_t v1_avx2_intersection(const uint32_t *freq, size_t lenFreq, const uint32_t *rare, size_t lenRare, uint32_t *matchOut) {
    assert(lenRare <= lenFreq);
    const uint32_t *matchOrig = matchOut;
    if (lenFreq == 0 || lenRare == 0) return 0;

    const uint64_t kFreqSpace = 2 * 4 * (0 + 1) - 1;
    const uint64_t kRareSpace = 0;

    const uint32_t *stopFreq = &freq[lenFreq] - kFreqSpace;
    const uint32_t *stopRare = &rare[lenRare] - kRareSpace;

    __m256i  Rare;

    __m256i F;

    if (COMPILER_RARELY( (rare >= stopRare) || (freq >= stopFreq) )) goto FINISH_SCALAR;
    uint32_t valRare;
    valRare = rare[0];
    Rare = _mm256_set1_epi32(valRare);

    uint64_t maxFreq;
    maxFreq = freq[2 * 4 - 1];
    F = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(freq));


    if (COMPILER_RARELY(maxFreq < valRare)) goto ADVANCE_FREQ;

ADVANCE_RARE:
    do {
        *matchOut = valRare;
        valRare = rare[1]; // for next iteration
        rare += 1;
        if (COMPILER_RARELY(rare >= stopRare)) {
            rare -= 1;
            goto FINISH_SCALAR;
        }
        F =  _mm256_cmpeq_epi32(F,Rare);
        Rare = _mm256_set1_epi32(valRare);
        if(_mm256_testz_si256(F,F) == 0)
          matchOut ++;
        F = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(freq));

    } while (maxFreq >= valRare);

    uint64_t maxProbe;

ADVANCE_FREQ:
    do {
        const uint64_t kProbe = (0 + 1) * 2 * 4;
        const uint32_t *probeFreq = freq + kProbe;
        maxProbe = freq[(0 + 2) * 2 * 4 - 1];

        if (COMPILER_RARELY(probeFreq >= stopFreq)) {
            goto FINISH_SCALAR;
        }

        freq = probeFreq;

    } while (maxProbe < valRare);

    maxFreq = maxProbe;

    F = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(freq));


    goto ADVANCE_RARE;

    size_t count;
FINISH_SCALAR:
    count = matchOut - matchOrig;

    lenFreq = stopFreq + kFreqSpace - freq;
    lenRare = stopRare + kRareSpace - rare;

    size_t tail = match_scalar(freq, lenFreq, rare, lenRare, matchOut);

    return count + tail;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
andq $-0x20, %rsp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movq %rcx, 0xd8(%rsp)
movq %r8, 0xd0(%rsp)
movq 0xd8(%rsp), %rax
cmpq 0xe8(%rsp), %rax
ja 0xabfb
jmp 0xac1a
leaq 0x3a53(%rip), %rdi      # 0xe655
leaq 0x3a5f(%rip), %rsi      # 0xe668
movl $0x12, %edx
leaq 0x3a64(%rip), %rcx      # 0xe679
callq 0x2120
movq 0xd0(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xe8(%rsp)
je 0xac40
cmpq $0x0, 0xd8(%rsp)
jne 0xac51
movq $0x0, 0xf8(%rsp)
jmp 0xb139
movq $0x7, 0xc0(%rsp)
movq $0x0, 0xb8(%rsp)
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
addq $-0x1c, %rax
movq %rax, 0xb0(%rsp)
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movq 0xe0(%rsp), %rcx
movb $0x1, %al
cmpq 0xa8(%rsp), %rcx
movb %al, 0x27(%rsp)
jae 0xacda
movq 0xf0(%rsp), %rax
cmpq 0xb0(%rsp), %rax
setae %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xace4
jmp 0xace9
jmp 0xb094
movq 0xe0(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x20(%rsp)
movl %eax, 0x1dc(%rsp)
movl %eax, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
movl %eax, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
movl %eax, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
movl %eax, 0x1c0(%rsp)
movl 0x1c4(%rsp), %edx
movl 0x1c8(%rsp), %ecx
movl 0x1cc(%rsp), %eax
movl 0x1d4(%rsp), %r8d
movl 0x1d8(%rsp), %edi
movl 0x1dc(%rsp), %esi
vmovd 0x1d0(%rsp), %xmm0
vpinsrd $0x1, %r8d, %xmm0, %xmm0
vpinsrd $0x2, %edi, %xmm0, %xmm0
vpinsrd $0x3, %esi, %xmm0, %xmm1
vmovd 0x1c0(%rsp), %xmm0
vpinsrd $0x1, %edx, %xmm0, %xmm0
vpinsrd $0x2, %ecx, %xmm0, %xmm0
vpinsrd $0x3, %eax, %xmm0, %xmm2
vmovaps %xmm2, %xmm0
vinserti128 $0x1, %xmm1, %ymm0, %ymm0
vmovdqa %ymm0, 0x1a0(%rsp)
vmovdqa 0x1a0(%rsp), %ymm0
vmovdqa %ymm0, 0x80(%rsp)
movq 0xf0(%rsp), %rax
movl 0x1c(%rax), %eax
movq %rax, 0x50(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rax
vmovdqu (%rax), %ymm0
vmovdqa %ymm0, 0x60(%rsp)
movq 0x50(%rsp), %rax
movl 0x5c(%rsp), %ecx
cmpq %rcx, %rax
jae 0xae10
jmp 0xb00b
jmp 0xae12
jmp 0xae14
movl 0x5c(%rsp), %ecx
movq 0xd0(%rsp), %rax
movl %ecx, (%rax)
movq 0xe0(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x5c(%rsp)
movq 0xe0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jb 0xae70
movq 0xe0(%rsp), %rax
addq $-0x4, %rax
movq %rax, 0xe0(%rsp)
jmp 0xb094
vmovdqa 0x60(%rsp), %ymm1
vmovdqa 0x80(%rsp), %ymm0
vmovdqa %ymm1, 0x140(%rsp)
vmovdqa %ymm0, 0x120(%rsp)
vmovdqa 0x140(%rsp), %ymm0
vmovdqa 0x120(%rsp), %ymm1
vpcmpeqd %ymm1, %ymm0, %ymm0
vmovdqa %ymm0, 0x60(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x100(%rsp)
movl 0x100(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl %eax, 0x22c(%rsp)
movl %eax, 0x228(%rsp)
movl %eax, 0x224(%rsp)
movl %eax, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
movl %eax, 0x218(%rsp)
movl %eax, 0x214(%rsp)
movl %eax, 0x210(%rsp)
movl 0x214(%rsp), %edx
movl 0x218(%rsp), %ecx
movl 0x21c(%rsp), %eax
movl 0x224(%rsp), %r8d
movl 0x228(%rsp), %edi
movl 0x22c(%rsp), %esi
vmovd 0x220(%rsp), %xmm0
vpinsrd $0x1, %r8d, %xmm0, %xmm0
vpinsrd $0x2, %edi, %xmm0, %xmm0
vpinsrd $0x3, %esi, %xmm0, %xmm1
vmovd 0x210(%rsp), %xmm0
vpinsrd $0x1, %edx, %xmm0, %xmm0
vpinsrd $0x2, %ecx, %xmm0, %xmm0
vpinsrd $0x3, %eax, %xmm0, %xmm2
vmovaps %xmm2, %xmm0
vinserti128 $0x1, %xmm1, %ymm0, %ymm0
vmovdqa %ymm0, 0x1e0(%rsp)
vmovdqa 0x1e0(%rsp), %ymm0
vmovdqa %ymm0, 0x80(%rsp)
vmovdqa 0x60(%rsp), %ymm1
vmovdqa 0x60(%rsp), %ymm0
vmovdqa %ymm1, 0x180(%rsp)
vmovdqa %ymm0, 0x160(%rsp)
vmovdqa 0x180(%rsp), %ymm0
vmovdqa 0x160(%rsp), %ymm1
vptest %ymm1, %ymm0
sete %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xafd5
movq 0xd0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xd0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
vmovdqu (%rax), %ymm0
vmovdqa %ymm0, 0x60(%rsp)
movq 0x50(%rsp), %rax
movl 0x5c(%rsp), %ecx
cmpq %rcx, %rax
jae 0xae14
jmp 0xb00b
jmp 0xb00d
movq $0x8, 0x40(%rsp)
movq 0xf0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x38(%rsp)
movq 0xf0(%rsp), %rax
movl 0x3c(%rax), %eax
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jb 0xb048
jmp 0xb094
movq 0x38(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x48(%rsp), %rax
movl 0x5c(%rsp), %ecx
cmpq %rcx, %rax
jb 0xb00d
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
vmovdqu (%rax), %ymm0
vmovdqa %ymm0, 0x60(%rsp)
jmp 0xae12
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movq %rax, 0x30(%rsp)
movq 0xb0(%rsp), %rax
addq $0x1c, %rax
movq 0xf0(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movq %rax, 0xe8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xe0(%rsp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movq %rax, 0xd8(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %r8
vzeroupper
callq 0xaa60
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)