#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 *);
int strncmp(char const *, char const *, size_t const);
char *strrnstr(const char *haystack, const char *needle,
               const size_t haystack_len) {
  const size_t needle_len = strlen(needle);
  if (needle_len > haystack_len) {
    return NULL;
  }
  for (const char *str = haystack + haystack_len - needle_len; str >= haystack;
       str--) {
    int ret = strncmp(str, needle, needle_len);
    if (ret == 0) {
      return (char *)str;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strrnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    115c <strrnstr+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <strrnstr+0xa8>
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
xor    %ecx,%ecx
sub    -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11c0 <strrnstr+0xa0>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11aa <strrnstr+0x8a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c8 <strrnstr+0xa8>
jmp    11af <strrnstr+0x8f>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1171 <strrnstr+0x51>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrnstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
xor    %r14d,%r14d
cmp    %rbx,%rax
ja     1180 <strrnstr+0x60>
mov    %rax,%r13
add    %r12,%rbx
sub    %rax,%rbx
cmp    %r12,%rbx
jb     1180 <strrnstr+0x60>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
je     117d <strrnstr+0x5d>
add    $0xffffffffffffffff,%rbx
cmp    %r12,%rbx
jae    1160 <strrnstr+0x40>
jmp    1180 <strrnstr+0x60>
mov    %rbx,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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