#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 *string, int character) {
  char *strptr = NULL;
  if (!string)
    return ((char *)string);
  strptr = (char *)(string + strlen(string) - 1);
  while (strptr >= string) {
    if (strptr[0] == (char)character)
      return (strptr);
    strptr -= 1;
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113f <strrchr+0x2f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ad <strrchr+0x9d>
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
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11a5 <strrchr+0x95>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    -0x14(%rbp),%ecx
movsbl %cl,%ecx
cmp    %ecx,%eax
jne    1194 <strrchr+0x84>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ad <strrchr+0x9d>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1164 <strrchr+0x54>
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
test   %rdi,%rdi
je     1159 <strrchr+0x49>
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
movsbl %bpl,%edx
lea    (%rax,%rbx,1),%rcx
add    $0xffffffffffffffff,%rcx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %rbx,%rcx
jb     115b <strrchr+0x4b>
movsbl (%rcx),%esi
add    $0xffffffffffffffff,%rcx
cmp    %esi,%edx
jne    1140 <strrchr+0x30>
add    $0x1,%rcx
mov    %rcx,%rax
jmp    115b <strrchr+0x4b>
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
store i8* null, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = load i8*, i8** %4
store i8* %10, i8** %3
br label %37
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -1
store i8* %17, i8** %6
br label %18
18: ; preds = %33, %11
%19 = load i8*, i8** %6
%20 = load i8*, i8** %4
%21 = icmp uge i8* %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = load i32, i32* %5
%28 = trunc i32 %27 to i8
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %26, %29
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %37
33: ; preds = %22
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i64 -1
store i8* %35, i8** %6
br label %18
36: ; preds = %18
store i8* null, i8** %3
br label %37
37: ; preds = %36, %31, %9
%38 = load i8*, i8** %3
ret i8* %38
}

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