#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 */
int strlen(char const *);
char *strstr(char const *, char const *);
char *C_strrstr(const char *haystack, const char *needle) {
  char *r = NULL;
  if (!needle[0])
    return (char *)haystack + strlen(haystack);
  while (1) {
    char *p = strstr(haystack, needle);
    if (!p)
      return r;
    r = p;
    haystack = p + 1;
  }
}


x86 assembly (clang, -O0):
<C_strrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    116b <C_strrstr+0x4b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <C_strrstr+0x92>
jmp    1170 <C_strrstr+0x50>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <C_strrstr+0x79>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <C_strrstr+0x92>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1170 <C_strrstr+0x50>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<C_strrstr>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
cmpb   $0x0,(%rsi)
je     1159 <C_strrstr+0x39>
mov    %rsi,%r14
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    1140 <C_strrstr+0x20>
jmp    1167 <C_strrstr+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r15,%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 i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %3
br label %31
18: ; preds = %2
br label %19
19: ; preds = %18, %27
%20 = load i8*, i8** %4
%21 = load i8*, i8** %5
%22 = call i8* @strstr(i8* %20, i8* %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = load i8*, i8** %6
store i8* %26, i8** %3
br label %31
27: ; preds = %19
%28 = load i8*, i8** %7
store i8* %28, i8** %6
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8* %30, i8** %4
br label %19
31: ; preds = %25, %12
%32 = load i8*, i8** %3
ret i8* %32
}

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