#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) {
  int len = strlen(s) - 1;
  for (; len >= 0; len--)
    if (s[len] == c)
      return (char *)&s[len];
  return NULL;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1174 <strrchr+0x64>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    -0x14(%rbp),%eax
jne    1161 <strrchr+0x51>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    117c <strrchr+0x6c>
jmp    1166 <strrchr+0x56>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    112e <strrchr+0x1e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%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>
mov    %eax,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ecx,%ecx
jle    1149 <strrchr+0x39>
movsbl -0x1(%rbx,%rcx,1),%edx
add    $0xffffffffffffffff,%rcx
cmp    %ebp,%edx
jne    1130 <strrchr+0x20>
mov    %ecx,%eax
add    %rax,%rbx
mov    %rbx,%rax
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 i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %6
br label %10
10: ; preds = %28, %2
%11 = load i32, i32* %6
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = load i32, i32* %5
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8* %26, i8** %3
br label %32
27: ; preds = %13
br label %28
28: ; preds = %27
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
br label %10
31: ; preds = %10
store i8* null, i8** %3
br label %32
32: ; preds = %31, %22
%33 = load i8*, i8** %3
ret i8* %33
}

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