#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 *mem_begin(char *);
size_t str_len(char *);
scalar_t__ strncmp(char *, char *, size_t);
char *str_rfind(char *haystack, char *needle) {
  char *haystack_begin = mem_begin(haystack);
  char *str = NULL;
  size_t len_needle = 0;
  if (haystack == NULL)
    return (NULL);
  if (needle == NULL)
    return (NULL);
  len_needle = str_len(needle);
  for (str = haystack; str >= haystack_begin; str--) {
    if (strncmp(str, needle, len_needle) == 0)
      return (str);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<str_rfind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <mem_begin@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <str_rfind+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <str_rfind+0xc6>
cmpq   $0x0,-0x18(%rbp)
jne    118d <str_rfind+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <str_rfind+0xc6>
mov    -0x18(%rbp),%rdi
call   1050 <str_len@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11ee <str_rfind+0xbe>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11d8 <str_rfind+0xa8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f6 <str_rfind+0xc6>
jmp    11dd <str_rfind+0xad>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    11a2 <str_rfind+0x72>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_rfind>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <mem_begin@plt>
xor    %r15d,%r15d
test   %rbx,%rbx
je     1191 <str_rfind+0x61>
test   %r14,%r14
je     1191 <str_rfind+0x61>
mov    %rax,%r12
mov    %r14,%rdi
call   1050 <str_len@plt>
cmp    %rbx,%r12
jbe    1166 <str_rfind+0x36>
xor    %r15d,%r15d
jmp    1191 <str_rfind+0x61>
mov    %rax,%r13
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <strncmp@plt>
test   %rax,%rax
je     118e <str_rfind+0x5e>
add    $0xffffffffffffffff,%rbx
cmp    %r12,%rbx
jae    1170 <str_rfind+0x40>
jmp    1191 <str_rfind+0x61>
mov    %rbx,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @str_rfind(i8* %0, i8* %1) {
%3 = tail call i8* @mem_begin(i8* %0)
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = tail call i64 @str_len(i8* %1)
%9 = icmp ugt i8* %3, %0
br i1 %9, label %17, label %10
10: ; preds = %7, %14
%11 = phi i8* [ %15, %14 ], [ %0, %7 ]
%12 = tail call i64 @strncmp(i8* %11, i8* %1, i64 %8)
%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, %3
br i1 %16, label %17, label %10
17: ; preds = %10, %14, %7, %2
%18 = phi i8* [ null, %2 ], [ null, %7 ], [ %11, %10 ], [ null, %14 ]
ret i8* %18
}