#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ __m128i;

/* Variables and functions */
int /*<<< orphan*/ _mm_add_epi32(int /*<<< orphan*/ const,
                                 int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_madd_epi16(int /*<<< orphan*/ const,
                                  int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_or_si128(int /*<<< orphan*/ const,
                                int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_setzero_si128();
int /*<<< orphan*/ _mm_subs_epu8(int /*<<< orphan*/ const,
                                 int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_unpackhi_epi8(int /*<<< orphan*/ const,
                                     int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_unpacklo_epi8(int /*<<< orphan*/ const,
                                     int /*<<< orphan*/ const);
void SubtractAndSquare_SSE2(const __m128i a, const __m128i b,
                            __m128i *const sum) {
  const __m128i a_b = _mm_subs_epu8(a, b);
  const __m128i b_a = _mm_subs_epu8(b, a);
  const __m128i abs_a_b = _mm_or_si128(a_b, b_a);
  const __m128i zero = _mm_setzero_si128();
  const __m128i C0 = _mm_unpacklo_epi8(abs_a_b, zero);
  const __m128i C1 = _mm_unpackhi_epi8(abs_a_b, zero);
  const __m128i sum1 = _mm_madd_epi16(C0, C0);
  const __m128i sum2 = _mm_madd_epi16(C1, C1);
  *sum = _mm_add_epi32(sum1, sum2);
}


x86 assembly (clang, -O0):
<SubtractAndSquare_SSE2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1080 <_mm_subs_epu8@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1080 <_mm_subs_epu8@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <_mm_or_si128@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <_mm_setzero_si128@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1070 <_mm_madd_epi16@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1070 <_mm_madd_epi16@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1060 <_mm_add_epi32@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SubtractAndSquare_SSE2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
call   1080 <_mm_subs_epu8@plt>
mov    %eax,%r15d
mov    %ebp,%edi
mov    %ebx,%esi
call   1080 <_mm_subs_epu8@plt>
mov    %r15d,%edi
mov    %eax,%esi
call   1090 <_mm_or_si128@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <_mm_setzero_si128@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %eax,%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,%ebx
mov    %r15d,%edi
mov    %ebp,%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %ebx,%esi
call   1070 <_mm_madd_epi16@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %ebp,%esi
call   1070 <_mm_madd_epi16@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <_mm_add_epi32@plt>
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @SubtractAndSquare_SSE2(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = call i32 @_mm_subs_epu8(i32 %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = call i32 @_mm_subs_epu8(i32 %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = call i32 @_mm_or_si128(i32 %21, i32 %22)
store i32 %23, i32* %9
%24 = call i32 (...) @_mm_setzero_si128()
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = load i32, i32* %10
%27 = call i32 @_mm_unpacklo_epi8(i32 %25, i32 %26)
store i32 %27, i32* %11
%28 = load i32, i32* %9
%29 = load i32, i32* %10
%30 = call i32 @_mm_unpackhi_epi8(i32 %28, i32 %29)
store i32 %30, i32* %12
%31 = load i32, i32* %11
%32 = load i32, i32* %11
%33 = call i32 @_mm_madd_epi16(i32 %31, i32 %32)
store i32 %33, i32* %13
%34 = load i32, i32* %12
%35 = load i32, i32* %12
%36 = call i32 @_mm_madd_epi16(i32 %34, i32 %35)
store i32 %36, i32* %14
%37 = load i32, i32* %13
%38 = load i32, i32* %14
%39 = call i32 @_mm_add_epi32(i32 %37, i32 %38)
%40 = load i32*, i32** %6
store i32 %39, i32* %40
ret void
}

LLVM IR (-O2):
define void @SubtractAndSquare_SSE2(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @_mm_subs_epu8(i32 %0, i32 %1)
%5 = tail call i32 @_mm_subs_epu8(i32 %1, i32 %0)
%6 = tail call i32 @_mm_or_si128(i32 %4, i32 %5)
%7 = tail call i32 (...) @_mm_setzero_si128()
%8 = tail call i32 @_mm_unpacklo_epi8(i32 %6, i32 %7)
%9 = tail call i32 @_mm_unpackhi_epi8(i32 %6, i32 %7)
%10 = tail call i32 @_mm_madd_epi16(i32 %8, i32 %8)
%11 = tail call i32 @_mm_madd_epi16(i32 %9, i32 %9)
%12 = tail call i32 @_mm_add_epi32(i32 %10, i32 %11)
store i32 %12, i32* %2
ret void
}