#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ strcmp(char const *, char const *);
int /*<<< orphan*/ string_empty(char const *);
size_t string_length(char const *);
char const *strrchr(char const *, char const);
const char *string_rfind(const char *string, const char *substring) {
  if (!string_empty(string) && !string_empty(substring)) {
    if (substring[1] != 0) {
      size_t length = string_length(string);
      size_t find_length = string_length(substring);
      if (length >= find_length) {
        ssize_t i = length - find_length;
        for (; i >= 0; --i)
          if (!strcmp(string + i, substring))
            return string + i;
      }
    } else
      return strrchr(string, substring[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<string_rfind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <string_empty@plt>
cmp    $0x0,%eax
jne    122d <string_rfind+0xed>
mov    -0x18(%rbp),%rdi
call   1030 <string_empty@plt>
cmp    $0x0,%eax
jne    122d <string_rfind+0xed>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     120f <string_rfind+0xcf>
mov    -0x10(%rbp),%rdi
call   1060 <string_length@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <string_length@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     120a <string_rfind+0xca>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jl     1205 <string_rfind+0xc5>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11ef <string_rfind+0xaf>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1235 <string_rfind+0xf5>
jmp    11f4 <string_rfind+0xb4>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    11b9 <string_rfind+0x79>
jmp    120a <string_rfind+0xca>
jmp    1228 <string_rfind+0xe8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1040 <strrchr@plt>
mov    %rax,-0x8(%rbp)
jmp    1235 <string_rfind+0xf5>
jmp    122d <string_rfind+0xed>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_rfind>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <string_empty@plt>
test   %eax,%eax
jne    1164 <string_rfind+0x24>
mov    %r14,%rdi
call   1030 <string_empty@plt>
test   %eax,%eax
je     1174 <string_rfind+0x34>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
cmpb   $0x0,0x1(%r14)
je     11c1 <string_rfind+0x81>
mov    %r12,%rdi
call   1060 <string_length@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1060 <string_length@plt>
xor    %r15d,%r15d
sub    %rax,%r13
jb     1167 <string_rfind+0x27>
js     1167 <string_rfind+0x27>
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
lea    (%r12,%r13,1),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11d6 <string_rfind+0x96>
lea    -0x1(%r13),%rax
test   %r13,%r13
mov    %rax,%r13
jg     11a0 <string_rfind+0x60>
jmp    1167 <string_rfind+0x27>
movsbl (%r14),%esi
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <strrchr@plt>
mov    %rbx,%r15
jmp    1167 <string_rfind+0x27>

LLVM IR (-O0):
define i8* @string_rfind(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @string_empty(i8* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %61, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i32 @string_empty(i8* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %61, label %16
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %54
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i64 @string_length(i8* %23)
store i64 %24, i64* %6
%25 = load i8*, i8** %5
%26 = call i64 @string_length(i8* %25)
store i64 %26, i64* %7
%27 = load i64, i64* %6
%28 = load i64, i64* %7
%29 = icmp uge i64 %27, %28
br i1 %29, label %30, label %53
30: ; preds = %22
%31 = load i64, i64* %6
%32 = load i64, i64* %7
%33 = sub i64 %31, %32
store i64 %33, i64* %8
br label %34
34: ; preds = %49, %30
%35 = load i64, i64* %8
%36 = icmp sge i64 %35, 0
br i1 %36, label %37, label %52
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = load i64, i64* %8
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i8*, i8** %5
%42 = call i32 @strcmp(i8* %40, i8* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %37
%45 = load i8*, i8** %4
%46 = load i64, i64* %8
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8* %47, i8** %3
br label %62
48: ; preds = %37
br label %49
49: ; preds = %48
%50 = load i64, i64* %8
%51 = add nsw i64 %50, -1
store i64 %51, i64* %8
br label %34
52: ; preds = %34
br label %53
53: ; preds = %52, %22
br label %60
54: ; preds = %16
%55 = load i8*, i8** %4
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i64 0
%58 = load i8, i8* %57
%59 = call i8* @strrchr(i8* %55, i8 signext %58)
store i8* %59, i8** %3
br label %62
60: ; preds = %53
br label %61
61: ; preds = %60, %12, %2
store i8* null, i8** %3
br label %62
62: ; preds = %61, %54, %44
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
define i8* @string_rfind(i8* %0, i8* %1) {
%3 = tail call i32 @string_empty(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %30
5: ; preds = %2
%6 = tail call i32 @string_empty(i8* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %8
%13 = tail call i64 @string_length(i8* %0)
%14 = tail call i64 @string_length(i8* %1)
%15 = icmp uge i64 %13, %14
%16 = sub i64 %13, %14
%17 = icmp sgt i64 %16, -1
%18 = and i1 %15, %17
br i1 %18, label %22, label %30
19: ; preds = %22
%20 = add nsw i64 %23, -1
%21 = icmp sgt i64 %23, 0
br i1 %21, label %22, label %30
22: ; preds = %12, %19
%23 = phi i64 [ %20, %19 ], [ %16, %12 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = tail call i32 @strcmp(i8* dereferenceable(1) %24, i8* dereferenceable(1) %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %19
27: ; preds = %8
%28 = load i8, i8* %1
%29 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext %28)
br label %30
30: ; preds = %22, %19, %12, %2, %5, %27
%31 = phi i8* [ %29, %27 ], [ null, %5 ], [ null, %2 ], [ null, %12 ], [ %24, %22 ], [ null, %19 ]
ret i8* %31
}