#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 *strchr(char const *, int);
int strlen(char const *);
char *strrchr(const char *str, int ch) {
  char c = (char)ch;
  const char *found = NULL;
  if (!c) {
    return (char *)str + strlen(str);
  }
  while ((str = strchr(str, ch))) {
    found = str++;
  }
  return (char *)found;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,-0x15(%rbp)
movq   $0x0,-0x20(%rbp)
cmpb   $0x0,-0x15(%rbp)
jne    116d <strrchr+0x4d>
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,-0x8(%rbp)
jmp    11ac <strrchr+0x8c>
jmp    1172 <strrchr+0x52>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11a4 <strrchr+0x84>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x20(%rbp)
jmp    1172 <strrchr+0x52>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
test   %r14b,%r14b
je     1159 <strrchr+0x39>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    1140 <strrchr+0x20>
jmp    1167 <strrchr+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r14
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
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = trunc i32 %8 to i8
store i8 %9, i8* %6
store i8* null, i8** %7
%10 = load i8, i8* %6
%11 = icmp ne i8 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %3
br label %29
18: ; preds = %2
br label %19
19: ; preds = %24, %18
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = call i8* @strchr(i8* %20, i32 %21)
store i8* %22, i8** %4
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
store i8* %25, i8** %7
br label %19
27: ; preds = %19
%28 = load i8*, i8** %7
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %12
%30 = load i8*, i8** %3
ret i8* %30
}

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