#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 */

/* Variables and functions */
int __builtin_stsex(unsigned short *, int);
int test_stsex(unsigned short *p1, unsigned short *p2) {
  int a, b, c, d;
  a = __builtin_stsex(p1, *p2);
  b = __builtin_stsex(p2, 0);
  c = __builtin_stsex(p2 + 1, 0x8000);
  d = __builtin_stsex(p2 + 2, 0x7fff);
  return a + b + c + d;
}


x86 assembly (clang, -O0):
<test_stsex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movzwl (%rax),%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
mov    $0x8000,%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
mov    $0x7fff,%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_stsex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
movzwl (%rsi),%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,%ebp
lea    0x2(%rbx),%rdi
mov    $0x8000,%esi
call   1030 <__builtin_stsex@plt>
mov    %eax,%r15d
add    $0x4,%rbx
mov    %rbx,%rdi
mov    $0x7fff,%esi
call   1030 <__builtin_stsex@plt>
add    %r14d,%ebp
add    %r15d,%ebp
add    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @test_stsex(i16* %0, i16* %1) {
%3 = alloca i16*
%4 = alloca i16*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i16* %0, i16** %3
store i16* %1, i16** %4
%9 = load i16*, i16** %3
%10 = load i16*, i16** %4
%11 = load i16, i16* %10
%12 = zext i16 %11 to i32
%13 = call i32 @__builtin_stsex(i16* %9, i32 %12)
store i32 %13, i32* %5
%14 = load i16*, i16** %4
%15 = call i32 @__builtin_stsex(i16* %14, i32 0)
store i32 %15, i32* %6
%16 = load i16*, i16** %4
%17 = getelementptr inbounds i16, i16* %16, i64 1
%18 = call i32 @__builtin_stsex(i16* %17, i32 32768)
store i32 %18, i32* %7
%19 = load i16*, i16** %4
%20 = getelementptr inbounds i16, i16* %19, i64 2
%21 = call i32 @__builtin_stsex(i16* %20, i32 32767)
store i32 %21, i32* %8
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = add nsw i32 %22, %23
%25 = load i32, i32* %7
%26 = add nsw i32 %24, %25
%27 = load i32, i32* %8
%28 = add nsw i32 %26, %27
ret i32 %28
}

LLVM IR (-O2):
define i32 @test_stsex(i16* %0, i16* %1) {
%3 = load i16, i16* %1
%4 = zext i16 %3 to i32
%5 = tail call i32 @__builtin_stsex(i16* %0, i32 %4)
%6 = tail call i32 @__builtin_stsex(i16* %1, i32 0)
%7 = getelementptr inbounds i16, i16* %1, i64 1
%8 = tail call i32 @__builtin_stsex(i16* %7, i32 32768)
%9 = getelementptr inbounds i16, i16* %1, i64 2
%10 = tail call i32 @__builtin_stsex(i16* %9, i32 32767)
%11 = add nsw i32 %6, %5
%12 = add nsw i32 %11, %8
%13 = add nsw i32 %12, %10
ret i32 %13
}