#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_and_si128(int /*<<< orphan*/, int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_avg_epu16(int /*<<< orphan*/ const,
                                 int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_set1_epi16(int);
int /*<<< orphan*/ _mm_subs_epu16(int /*<<< orphan*/ const, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_xor_si128(int /*<<< orphan*/ const,
                                 int /*<<< orphan*/ const);
__m128i avg3_epu16(const __m128i *x, const __m128i *y, const __m128i *z) {
  const __m128i one = _mm_set1_epi16(1);
  const __m128i a = _mm_avg_epu16(*x, *z);
  const __m128i b =
      _mm_subs_epu16(a, _mm_and_si128(_mm_xor_si128(*x, *z), one));
  return _mm_avg_epu16(b, *y);
}


x86 assembly (clang, -O0):
<avg3_epu16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   1030 <_mm_set1_epi16@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1060 <_mm_avg_epu16@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <_mm_xor_si128@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%esi
call   1070 <_mm_and_si128@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   1040 <_mm_subs_epu16@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1060 <_mm_avg_epu16@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<avg3_epu16>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%edi
call   1030 <_mm_set1_epi16@plt>
mov    %eax,%r12d
mov    (%rbx),%edi
mov    (%r15),%esi
call   1060 <_mm_avg_epu16@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
mov    (%r15),%esi
call   1050 <_mm_xor_si128@plt>
mov    %eax,%edi
mov    %r12d,%esi
call   1070 <_mm_and_si128@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <_mm_subs_epu16@plt>
mov    (%r14),%esi
mov    %eax,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <_mm_avg_epu16@plt>

LLVM IR (-O0):
define i32 @avg3_epu16(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = call i32 @_mm_set1_epi16(i32 1)
store i32 %10, i32* %7
%11 = load i32*, i32** %4
%12 = load i32, i32* %11
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
%15 = call i32 @_mm_avg_epu16(i32 %12, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = load i32*, i32** %6
%20 = load i32, i32* %19
%21 = call i32 @_mm_xor_si128(i32 %18, i32 %20)
%22 = load i32, i32* %7
%23 = call i32 @_mm_and_si128(i32 %21, i32 %22)
%24 = call i32 @_mm_subs_epu16(i32 %16, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = load i32*, i32** %5
%27 = load i32, i32* %26
%28 = call i32 @_mm_avg_epu16(i32 %25, i32 %27)
ret i32 %28
}

LLVM IR (-O2):
define i32 @avg3_epu16(i32* nocapture %0, i32* nocapture %1, i32* nocapture %2) {
%4 = tail call i32 @_mm_set1_epi16(i32 1)
%5 = load i32, i32* %0
%6 = load i32, i32* %2
%7 = tail call i32 @_mm_avg_epu16(i32 %5, i32 %6)
%8 = load i32, i32* %0
%9 = load i32, i32* %2
%10 = tail call i32 @_mm_xor_si128(i32 %8, i32 %9)
%11 = tail call i32 @_mm_and_si128(i32 %10, i32 %4)
%12 = tail call i32 @_mm_subs_epu16(i32 %7, i32 %11)
%13 = load i32, i32* %1
%14 = tail call i32 @_mm_avg_epu16(i32 %12, i32 %13)
ret i32 %14
}