#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 size_t ssize_t;

/* Variables and functions */
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char const *, size_t);
const char *strrstr(const char *haystack, const char *needle) {
  if (haystack == NULL || needle == NULL) {
    return NULL;
  }
  size_t haystack_len = strlen(haystack);
  size_t needle_len = strlen(needle);
  if (needle_len > haystack_len) {
    return NULL;
  }
  ssize_t i = haystack_len - needle_len;
  while (i >= 0) {
    if (!strncmp(&haystack[i], needle, needle_len)) {
      return &haystack[i];
    }
    i--;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <strrstr+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1153 <strrstr+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <strrstr+0xc7>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1188 <strrstr+0x68>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <strrstr+0xc7>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jb     11df <strrstr+0xbf>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11ce <strrstr+0xae>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e7 <strrstr+0xc7>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1194 <strrstr+0x74>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     117d <strrstr+0x5d>
mov    %rsi,%r14
test   %rsi,%rsi
je     117d <strrstr+0x5d>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %rax,%r12
jae    1153 <strrstr+0x33>
xor    %eax,%eax
jmp    117d <strrstr+0x5d>
mov    %rax,%r15
add    %r12,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    1160 <strrstr+0x40>
add    $0x1,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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