#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 */
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char const *, size_t);
char *strnrstr(const char *restrict haystack, const char *restrict needle,
               size_t len) {
  char *last = 0;
  const char *next = haystack;
  size_t needleLen = strlen(needle);
  for (; len >= needleLen; --len, ++next) {
    if (strncmp(needle, next, needleLen) == 0) {
      last = (char *)next;
    }
  }
  return last;
}


x86 assembly (clang, -O0):
<strnrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     11a4 <strnrstr+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1182 <strnrstr+0x62>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <strnrstr+0x67>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1151 <strnrstr+0x31>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnrstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    %r15,%rax
jbe    1144 <strnrstr+0x24>
xor    %r12d,%r12d
jmp    1172 <strnrstr+0x52>
mov    %rax,%r13
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
cmove  %rbx,%r12
add    $0xffffffffffffffff,%r15
add    $0x1,%rbx
cmp    %r13,%r15
jae    1150 <strnrstr+0x30>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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