#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 scalar_t__ wmemcmp;
typedef int /*<<< orphan*/ const wchar_t;

/* Variables and functions */
int /*<<< orphan*/ const *wmemchr(int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ const,
                                  int /*<<< orphan*/ const *);
wchar_t *(wmemcends)(const wchar_t *string, size_t string_size,
                     const wchar_t *desired, size_t desired_size,
                     wchar_t stop) {
  wchar_t *end = wmemchr(string, stop, string);
  if (end != NULL)
    string_size = (size_t)(end - string);
  return (wmemcmp)(string + (string_size - desired_size), desired, desired_size)
             ? NULL
             : (string + string_size);
}


x86 assembly (clang, -O0):
<wmemcends>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1030 <wmemchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     115e <wmemcends+0x4e>
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
sar    $0x2,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1174 <wmemcends+0x64>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1187 <wmemcends+0x77>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wmemcends>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %r8d,%esi
mov    %rdi,%rdx
call   1030 <wmemchr@plt>
mov    %rax,%rcx
sub    %rbx,%rcx
shr    $0x2,%rcx
test   %rax,%rax
cmove  %r15,%rcx
xor    %eax,%eax
test   %r14,%r14
lea    (%rbx,%rcx,4),%rcx
cmove  %rcx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @wmemcends(i32* %0, i64 %1, i32* %2, i64 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32*
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i64 %3, i64* %9
store i32 %4, i32* %10
%12 = load i32*, i32** %6
%13 = load i32, i32* %10
%14 = load i32*, i32** %6
%15 = call i32* @wmemchr(i32* %12, i32 %13, i32* %14)
store i32* %15, i32** %11
%16 = load i32*, i32** %11
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %25
18: ; preds = %5
%19 = load i32*, i32** %11
%20 = load i32*, i32** %6
%21 = ptrtoint i32* %19 to i64
%22 = ptrtoint i32* %20 to i64
%23 = sub i64 %21, %22
%24 = sdiv exact i64 %23, 4
store i64 %24, i64* %7
br label %25
25: ; preds = %18, %5
%26 = load i32*, i32** %6
%27 = load i64, i64* %7
%28 = load i64, i64* %9
%29 = sub i64 %27, %28
%30 = getelementptr inbounds i32, i32* %26, i64 %29
%31 = load i32*, i32** %8
%32 = load i64, i64* %9
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %25
br label %39
35: ; preds = %25
%36 = load i32*, i32** %6
%37 = load i64, i64* %7
%38 = getelementptr inbounds i32, i32* %36, i64 %37
br label %39
39: ; preds = %35, %34
%40 = phi i32* [ null, %34 ], [ %38, %35 ]
ret i32* %40
}

LLVM IR (-O2):
define i32* @wmemcends(i32* %0, i64 %1, i32* nocapture readnone %2, i64 %3, i32 %4) {
%6 = tail call i32* @wmemchr(i32* %0, i32 %4, i32* %0)
%7 = icmp eq i32* %6, null
%8 = ptrtoint i32* %6 to i64
%9 = ptrtoint i32* %0 to i64
%10 = sub i64 %8, %9
%11 = ashr exact i64 %10, 2
%12 = select i1 %7, i64 %1, i64 %11
%13 = icmp eq i64 %3, 0
%14 = getelementptr inbounds i32, i32* %0, i64 %12
%15 = select i1 %13, i32* %14, i32* null
ret i32* %15
}