#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 /*<<< orphan*/ assert(int /*<<< orphan*/);
char *strchr(char const *, int);
char *strrchr(const char *cs, int c) {
  char *prev;
  char *rv;
  assert(cs != NULL);
  prev = strchr(cs, c);
  if ((prev == NULL) || (c == '\0')) {
    return prev;
  }
  do {
    rv = prev;
    prev = strchr(prev + 1, c);
  } while (prev != NULL);
  return rv;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1166 <strrchr+0x46>
cmpl   $0x0,-0x14(%rbp)
jne    1173 <strrchr+0x53>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <strrchr+0x87>
jmp    1178 <strrchr+0x58>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1178 <strrchr+0x58>
mov    -0x28(%rbp),%rax
mov    %rax,-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
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %ebp,%ebp
je     1165 <strrchr+0x45>
test   %rax,%rax
je     1165 <strrchr+0x45>
nopl   0x0(%rax)
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1150 <strrchr+0x30>
jmp    1168 <strrchr+0x48>
mov    %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 i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = call i8* @strchr(i8* %12, i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17, %2
%21 = load i8*, i8** %6
store i8* %21, i8** %3
br label %34
22: ; preds = %17
br label %23
23: ; preds = %29, %22
%24 = load i8*, i8** %6
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i32, i32* %5
%28 = call i8* @strchr(i8* %26, i32 %27)
store i8* %28, i8** %6
br label %29
29: ; preds = %23
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %23, label %32
32: ; preds = %29
%33 = load i8*, i8** %7
store i8* %33, i8** %3
br label %34
34: ; preds = %32, %20
%35 = load i8*, i8** %3
ret i8* %35
}

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