#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 const);
extern char *strrstr(const char *str, const char *substr) {
  const size_t length = strlen(substr);
  const char *p;
  for (p = str + strlen(str) - length; p >= str; --p)
    if (strncmp(p, substr, length) == 0)
      return (char *)p;
  return NULL;
}


x86 assembly (clang, -O0):
<strrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
xor    %ecx,%ecx
sub    -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11b1 <strrstr+0x91>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119b <strrstr+0x7b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <strrstr+0x99>
jmp    11a0 <strrstr+0x80>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1165 <strrstr+0x45>
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
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    %r15,%rbx
sub    %r12,%rbx
cmp    %r15,%rbx
jb     116c <strrstr+0x4c>
nop
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     116e <strrstr+0x4e>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jae    1150 <strrstr+0x30>
xor    %ebx,%ebx
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 i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %6
%10 = load i8*, i8** %4
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i64, i64* %6
%15 = sub i64 0, %14
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8* %16, i8** %7
br label %17
17: ; preds = %30, %2
%18 = load i8*, i8** %7
%19 = load i8*, i8** %4
%20 = icmp uge i8* %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = load i8*, i8** %7
%23 = load i8*, i8** %5
%24 = load i64, i64* %6
%25 = call i64 @strncmp(i8* %22, i8* %23, i64 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i8*, i8** %7
store i8* %28, i8** %3
br label %34
29: ; preds = %21
br label %30
30: ; preds = %29
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 -1
store i8* %32, i8** %7
br label %17
33: ; preds = %17
store i8* null, i8** %3
br label %34
34: ; preds = %33, %27
%35 = load i8*, i8** %3
ret i8* %35
}

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