#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_stex(unsigned char *, int);
int test_stex(unsigned char *p1, unsigned char *p2) {
  int a, b, c, d;
  a = __builtin_stex(p1, *p2);
  b = __builtin_stex(p2, 0);
  c = __builtin_stex(p2 + 1, 0x80);
  d = __builtin_stex(p2 + 2, 0x7f);
  return a + b + c + d;
}


x86 assembly (clang, -O0):
<test_stex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movzbl (%rax),%esi
call   1030 <__builtin_stex@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <__builtin_stex@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x80,%esi
call   1030 <__builtin_stex@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
mov    $0x7f,%esi
call   1030 <__builtin_stex@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_stex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
movzbl (%rsi),%esi
call   1030 <__builtin_stex@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <__builtin_stex@plt>
mov    %eax,%ebp
lea    0x1(%rbx),%rdi
mov    $0x80,%esi
call   1030 <__builtin_stex@plt>
mov    %eax,%r15d
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x7f,%esi
call   1030 <__builtin_stex@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_stex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = call i32 @__builtin_stex(i8* %9, i32 %12)
store i32 %13, i32* %5
%14 = load i8*, i8** %4
%15 = call i32 @__builtin_stex(i8* %14, i32 0)
store i32 %15, i32* %6
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = call i32 @__builtin_stex(i8* %17, i32 128)
store i32 %18, i32* %7
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 2
%21 = call i32 @__builtin_stex(i8* %20, i32 127)
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_stex(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = zext i8 %3 to i32
%5 = tail call i32 @__builtin_stex(i8* %0, i32 %4)
%6 = tail call i32 @__builtin_stex(i8* %1, i32 0)
%7 = getelementptr inbounds i8, i8* %1, i64 1
%8 = tail call i32 @__builtin_stex(i8* %7, i32 128)
%9 = getelementptr inbounds i8, i8* %1, i64 2
%10 = tail call i32 @__builtin_stex(i8* %9, i32 127)
%11 = add nsw i32 %6, %5
%12 = add nsw i32 %11, %8
%13 = add nsw i32 %12, %10
ret i32 %13
}