// File: ctSkennerton[P]fxtract/util/msutil.c
// Function: memsame
int memsame(void const*_src, void const*_dst, int len)
{
    uint8_t const *src = (uint8_t const*)_src;
    uint8_t const *dst = (uint8_t const*)_dst;
    int ret, srcoff = 15 & (intptr_t)src;

    if (srcoff && len > 31) {
        if ((ret = xm_diff(xm_loud(src), xm_loud(dst))))
            return src - (uint8_t const*)_src + ffs(ret) - 1;

        src += 16 - srcoff;
        dst += 16 - srcoff;
        len -= 16 - srcoff;
    }

    // A special case for srcoff==dstoff => xm_load(dst)
    //  seems to make no difference.
    // Prefetch windows from 128..512 all do about the same.
#   define PRE (512-64)
    for (; len >= 16; src += 16, dst += 16, len -=16) {
        _mm_prefetch(src+PRE, _MM_HINT_NTA);
        _mm_prefetch(dst+PRE, _MM_HINT_NTA);
        if ((ret = xm_diff(xm_load(src), xm_loud(dst))))
            return src - (uint8_t const*)_src + ffs(ret) - 1;
    }

    if ((ret = xm_diff(xm_loud(src), xm_loud(dst)) & ~(-1 << len)))
        return src - (uint8_t const*)_src + ffs(ret) - 1;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
andq $0xf, %rax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x7f1a
cmpl $0x1f, -0x1c(%rbp)
jle 0x7f1a
movq -0x28(%rbp), %rdi
callq 0x7db0
movaps %xmm0, -0x50(%rbp)
movq -0x30(%rbp), %rdi
callq 0x7db0
movaps %xmm0, %xmm1
movaps -0x50(%rbp), %xmm0
callq 0x7d80
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
je 0x7ede
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %edi
callq 0x32f0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x4(%rbp)
jmp 0x803b
movl $0x10, %ecx
subl -0x38(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl $0x10, %ecx
subl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x10, %ecx
subl -0x38(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
jmp 0x7f1c
cmpl $0x10, -0x1c(%rbp)
jl 0x7fc1
movq -0x28(%rbp), %rax
addq $0x1c0, %rax            # imm = 0x1C0
prefetchnta (%rax)
movq -0x30(%rbp), %rax
addq $0x1c0, %rax            # imm = 0x1C0
prefetchnta (%rax)
movq -0x28(%rbp), %rdi
callq 0x7e20
movaps %xmm0, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0x7db0
movaps %xmm0, %xmm1
movaps -0x70(%rbp), %xmm0
callq 0x7d80
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
je 0x7f99
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x34(%rbp), %edi
callq 0x32f0
movl %eax, %ecx
movq -0x78(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x4(%rbp)
jmp 0x803b
jmp 0x7f9b
movq -0x28(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x1c(%rbp)
jmp 0x7f1c
movq -0x28(%rbp), %rdi
callq 0x7db0
movaps %xmm0, -0x90(%rbp)
movq -0x30(%rbp), %rdi
callq 0x7db0
movaps %xmm0, %xmm1
movaps -0x90(%rbp), %xmm0
callq 0x7d80
movl -0x1c(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
shll %cl, %edx
movl %edx, %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
je 0x8034
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0x34(%rbp), %edi
callq 0x32f0
movl %eax, %ecx
movq -0x98(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x4(%rbp)
jmp 0x803b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)