#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__ strncasecmp(char *, char *, size_t);
char *str_caseless_rfind(char *haystack, char *needle) {
  char *haystack_begin = mem_begin(haystack);
  char *str = NULL;
  size_t needle_len = 0;
  needle_len = str_len(needle);
  for (str = haystack; str >= haystack_begin; str--) {
    if (strncasecmp(str, needle, needle_len) == 0)
      return (str);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<str_caseless_rfind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <mem_begin@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
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     11be <str_caseless_rfind+0x8e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11a8 <str_caseless_rfind+0x78>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c6 <str_caseless_rfind+0x96>
jmp    11ad <str_caseless_rfind+0x7d>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <str_caseless_rfind+0x42>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_caseless_rfind>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <mem_begin@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <str_len@plt>
cmp    %rbx,%r15
ja     117c <str_caseless_rfind+0x4c>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     117e <str_caseless_rfind+0x4e>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jae    1160 <str_caseless_rfind+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @str_caseless_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** %5
%12 = call i64 @str_len(i8* %11)
store i64 %12, i64* %8
%13 = load i8*, i8** %4
store i8* %13, i8** %7
br label %14
14: ; preds = %27, %2
%15 = load i8*, i8** %7
%16 = load i8*, i8** %6
%17 = icmp uge i8* %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = load i8*, i8** %5
%21 = load i64, i64* %8
%22 = call i64 @strncasecmp(i8* %19, i8* %20, i64 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i8*, i8** %7
store i8* %25, i8** %3
br label %31
26: ; preds = %18
br label %27
27: ; preds = %26
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %7
br label %14
30: ; preds = %14
store i8* null, i8** %3
br label %31
31: ; preds = %30, %24
%32 = load i8*, i8** %3
ret i8* %32
}

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