#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 */
char *strstr(char const *, char const *);
char *strstrr(const char *haystack, const char *needle) {
  const char *last = haystack - 1;
  while (1) {
    const char *new_last = strstr(last + 1, needle);
    if (new_last)
      last = new_last;
    else
      break;
  }
  if (last == haystack - 1)
    return NULL;
  else
    return (char *)last;
}


x86 assembly (clang, -O0):
<strstrr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1159 <strstrr+0x49>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    115e <strstrr+0x4e>
jmp    1163 <strstrr+0x53>
jmp    112c <strstrr+0x1c>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1185 <strstrr+0x75>
movq   $0x0,-0x8(%rbp)
jmp    118d <strstrr+0x7d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstrr>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
add    $0xffffffffffffffff,%r14
mov    %r14,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %r15,%rsi
call   1030 <strstr@plt>
test   %rax,%rax
jne    1130 <strstrr+0x20>
xor    %eax,%eax
cmp    %r14,%rbx
cmovne %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strstrr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 -1
store i8* %9, i8** %6
br label %10
10: ; preds = %2, %20
%11 = load i8*, i8** %6
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = load i8*, i8** %5
%14 = call i8* @strstr(i8* %12, i8* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i8*, i8** %7
store i8* %18, i8** %6
br label %20
19: ; preds = %10
br label %21
20: ; preds = %17
br label %10
21: ; preds = %19
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 -1
%25 = icmp eq i8* %22, %24
br i1 %25, label %26, label %27
26: ; preds = %21
store i8* null, i8** %3
br label %29
27: ; preds = %21
%28 = load i8*, i8** %6
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %26
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @strstrr(i8* %0, i8* nocapture %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 -1
br label %4
4: ; preds = %4, %2
%5 = phi i8* [ %3, %2 ], [ %7, %4 ]
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = tail call i8* @strstr(i8* %6, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %4
9: ; preds = %4
%10 = icmp eq i8* %5, %3
%11 = select i1 %10, i8* null, i8* %5
ret i8* %11
}