#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);
char *(strrchr)(const char *s, int c) {
  const char *last = NULL;
  if (c == '\0')
    return strchr(s, c);
  while ((s = strchr(s, c)) != NULL) {
    last = s;
    s++;
  }
  return (char *)last;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1146 <strrchr+0x36>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x8(%rbp)
jmp    1186 <strrchr+0x76>
jmp    114b <strrchr+0x3b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     117e <strrchr+0x6e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114b <strrchr+0x3b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
je     1149 <strrchr+0x29>
mov    %esi,%r14d
xor    %eax,%eax
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    1130 <strrchr+0x10>
jmp    1157 <strrchr+0x37>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%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*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call i8* @strchr(i8* %10, i32 %11)
store i8* %12, i8** %3
br label %25
13: ; preds = %2
br label %14
14: ; preds = %19, %13
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i8* @strchr(i8* %15, i32 %16)
store i8* %17, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8*, i8** %4
store i8* %20, i8** %6
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %4
br label %14
23: ; preds = %14
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %25
25: ; preds = %23, %9
%26 = load i8*, i8** %3
ret i8* %26
}

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