#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 *revstrstr(const char *haystack, const char *needle, const char *pointer) {
  size_t needle_len = strlen(needle);
  size_t tail_len = strlen(pointer);
  if (tail_len < needle_len)
    pointer += tail_len - needle_len;
  while (pointer >= haystack) {
    if (strncmp(pointer, needle, needle_len) == 0)
      return (char *)pointer;
    pointer--;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<revstrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    116c <revstrstr+0x4c>
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <revstrstr+0x51>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11b8 <revstrstr+0x98>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11a7 <revstrstr+0x87>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c0 <revstrstr+0xa0>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1171 <revstrstr+0x51>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<revstrstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %r13d,%r13d
mov    %r12,%rcx
sub    %rax,%rcx
cmovb  %r13,%rcx
sub    %rcx,%rbx
cmp    %r15,%rbx
jb     1181 <revstrstr+0x61>
xor    %r13d,%r13d
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     117e <revstrstr+0x5e>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jae    1160 <revstrstr+0x40>
jmp    1181 <revstrstr+0x61>
mov    %rbx,%r13
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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