#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*/ const __m128i;

/* Variables and functions */
int /*<<< orphan*/ const _mm_add_epi32(int /*<<< orphan*/ const,
                                       int /*<<< orphan*/ const);
int /*<<< orphan*/ const _mm_madd_epi16(int /*<<< orphan*/ const,
                                        int /*<<< orphan*/ const);
int /*<<< orphan*/ const _mm_setzero_si128();
int /*<<< orphan*/ const _mm_subs_epi16(int /*<<< orphan*/ const,
                                        int /*<<< orphan*/ const);
int /*<<< orphan*/ const _mm_unpackhi_epi8(int /*<<< orphan*/ const,
                                           int /*<<< orphan*/ const);
int /*<<< orphan*/ const _mm_unpacklo_epi8(int /*<<< orphan*/ const,
                                           int /*<<< orphan*/ const);
__m128i SubtractAndAccumulate(const __m128i a, const __m128i b) {
  const __m128i zero = _mm_setzero_si128();
  const __m128i A0 = _mm_unpacklo_epi8(a, zero);
  const __m128i B0 = _mm_unpacklo_epi8(b, zero);
  const __m128i A1 = _mm_unpackhi_epi8(a, zero);
  const __m128i B1 = _mm_unpackhi_epi8(b, zero);
  const __m128i C0 = _mm_subs_epi16(A0, B0);
  const __m128i C1 = _mm_subs_epi16(A1, B1);
  const __m128i D0 = _mm_madd_epi16(C0, C0);
  const __m128i D1 = _mm_madd_epi16(C1, C1);
  const __m128i sum = _mm_add_epi32(D0, D1);
  return sum;
}


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

x86 assembly (clang, -O2):
<SubtractAndAccumulate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <_mm_setzero_si128@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,%r15d
mov    %r14d,%edi
mov    %ebx,%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,%r12d
mov    %ebp,%edi
mov    %ebx,%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,%r13d
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,%ebx
mov    %r15d,%edi
mov    %r12d,%esi
call   1070 <_mm_subs_epi16@plt>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %ebx,%esi
call   1070 <_mm_subs_epi16@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %ebp,%esi
call   1080 <_mm_madd_epi16@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %ebx,%esi
call   1080 <_mm_madd_epi16@plt>
mov    %ebp,%edi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <_mm_add_epi32@plt>

LLVM IR (-O0):
define i32 @SubtractAndAccumulate(i32 %0, i32 %1) {
%3 = alloca i32
%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* %3
store i32 %1, i32* %4
%15 = call i32 (...) @_mm_setzero_si128()
store i32 %15, i32* %5
%16 = load i32, i32* %3
%17 = load i32, i32* %5
%18 = call i32 @_mm_unpacklo_epi8(i32 %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = call i32 @_mm_unpacklo_epi8(i32 %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %3
%23 = load i32, i32* %5
%24 = call i32 @_mm_unpackhi_epi8(i32 %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = call i32 @_mm_unpackhi_epi8(i32 %25, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i32 @_mm_subs_epi16(i32 %28, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = call i32 @_mm_subs_epi16(i32 %31, i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %10
%35 = load i32, i32* %10
%36 = call i32 @_mm_madd_epi16(i32 %34, i32 %35)
store i32 %36, i32* %12
%37 = load i32, i32* %11
%38 = load i32, i32* %11
%39 = call i32 @_mm_madd_epi16(i32 %37, i32 %38)
store i32 %39, i32* %13
%40 = load i32, i32* %12
%41 = load i32, i32* %13
%42 = call i32 @_mm_add_epi32(i32 %40, i32 %41)
store i32 %42, i32* %14
%43 = load i32, i32* %14
ret i32 %43
}

LLVM IR (-O2):
define i32 @SubtractAndAccumulate(i32 %0, i32 %1) {
%3 = tail call i32 (...) @_mm_setzero_si128()
%4 = tail call i32 @_mm_unpacklo_epi8(i32 %0, i32 %3)
%5 = tail call i32 @_mm_unpacklo_epi8(i32 %1, i32 %3)
%6 = tail call i32 @_mm_unpackhi_epi8(i32 %0, i32 %3)
%7 = tail call i32 @_mm_unpackhi_epi8(i32 %1, i32 %3)
%8 = tail call i32 @_mm_subs_epi16(i32 %4, i32 %5)
%9 = tail call i32 @_mm_subs_epi16(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)
ret i32 %12
}