#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 *strrchr(const char *s, int c) {
  const char *end = s + strlen(s);
  do {
    if (*end == (char)c) {
      return (char *)end;
    }
  } while (--end >= s);
  return NULL;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    -0x14(%rbp),%ecx
movsbl %cl,%ecx
cmp    %ecx,%eax
jne    1162 <strrchr+0x52>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1185 <strrchr+0x75>
jmp    1167 <strrchr+0x57>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
cmp    -0x10(%rbp),%rax
jae    1140 <strrchr+0x30>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
add    %rbx,%rax
movsbl %bpl,%ecx
cs nopw 0x0(%rax,%rax,1)
movsbl (%rax),%edx
cmp    %edx,%ecx
je     1142 <strrchr+0x32>
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jae    1130 <strrchr+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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