#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 *my_strrstr(char const *s1, char const *s2) {
  register char *last;
  register char *current;
  last = NULL;
  if (*s2 != '\0') {
    current = strstr(s1, s2);
    while (current != NULL) {
      last = current;
      current = strstr(last + 1, s2);
    }
  }
  return last;
}


x86 assembly (clang, -O0):
<my_strrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     117b <my_strrstr+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1176 <my_strrstr+0x66>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x20(%rbp)
jmp    1149 <my_strrstr+0x39>
jmp    117b <my_strrstr+0x6b>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strrstr>:
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rsi)
je     1146 <my_strrstr+0x36>
mov    %rsi,%r14
call   1030 <strstr@plt>
test   %rax,%rax
je     1146 <my_strrstr+0x36>
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %r14,%rsi
call   1030 <strstr@plt>
test   %rax,%rax
jne    1130 <my_strrstr+0x20>
jmp    1148 <my_strrstr+0x38>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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