#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 */
scalar_t__ memcmp(char *, char const *const, size_t const);
size_t strlen(char const *const);
char *str_ends_with(const char *const haystack, const char *const needle) {
  const size_t stacklen = strlen(haystack);
  const size_t ndllen = strlen(needle);
  if (stacklen < ndllen)
    return NULL;
  char *cmppos = (char *)(haystack + (stacklen - ndllen));
  return (memcmp(cmppos, needle, ndllen) == 0) ? cmppos : NULL;
}


x86 assembly (clang, -O0):
<str_ends_with>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1165 <str_ends_with+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <str_ends_with+0x93>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    11a0 <str_ends_with+0x80>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11ab <str_ends_with+0x8b>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11ab <str_ends_with+0x8b>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_ends_with>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %rax,%rbx
jae    1144 <str_ends_with+0x24>
xor    %eax,%eax
jmp    1161 <str_ends_with+0x41>
add    %rbx,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
cmove  %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_ends_with(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %33
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i64, i64* %6
%20 = load i64, i64* %7
%21 = sub i64 %19, %20
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** %5
%25 = load i64, i64* %7
%26 = call i64 @memcmp(i8* %23, i8* %24, i64 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = load i8*, i8** %8
br label %31
30: ; preds = %17
br label %31
31: ; preds = %30, %28
%32 = phi i8* [ %29, %28 ], [ null, %30 ]
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %16
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @str_ends_with(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ult i64 %3, %4
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = sub i64 %3, %4
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = tail call i64 @memcmp(i8* %8, i8* %1, i64 %4)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i8* %8, i8* null
br label %12
12: ; preds = %2, %6
%13 = phi i8* [ %11, %6 ], [ null, %2 ]
ret i8* %13
}