#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 *, int);
int strlen(char const *);
char *c_strrstr(const char *s, const char *str) {
  char *p;
  int len = strlen(s);
  for (p = s + len - 1; p >= s; p--) {
    if ((*p == *str) && (memcmp(p, str, strlen(str)) == 0)) {
      return p;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<c_strrstr>:
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    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11c8 <c_strrstr+0xa8>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11b2 <c_strrstr+0x92>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    11b2 <c_strrstr+0x92>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <c_strrstr+0xb0>
jmp    11b7 <c_strrstr+0x97>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    114f <c_strrstr+0x2f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_strrstr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rbx
add    $0xffffffffffffffff,%rbx
jmp    1144 <c_strrstr+0x24>
nopl   0x0(%rax)
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jb     116d <c_strrstr+0x4d>
movzbl (%rbx),%eax
cmp    (%r14),%al
jne    1140 <c_strrstr+0x20>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1140 <c_strrstr+0x20>
jmp    116f <c_strrstr+0x4f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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